00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 #ifndef _PCRITICALSECTION
00098 #define _PCRITICALSECTION
00099
00100 #include <ptlib/psync.h>
00101
00102 #if P_HAS_ATOMIC_INT
00103 #if P_NEEDS_GNU_CXX_NAMESPACE
00104 #define EXCHANGE_AND_ADD(v,i) __gnu_cxx::__exchange_and_add(v,i)
00105 #else
00106 #define EXCHANGE_AND_ADD(v,i) __exchange_and_add(v,i)
00107 #endif
00108 #endif
00109
00116 class PCriticalSection : public PSync
00117 {
00118 PCLASSINFO(PCriticalSection, PSync);
00119
00120 public:
00125 PCriticalSection();
00126 PCriticalSection(const PCriticalSection &);
00127
00130 ~PCriticalSection();
00132
00137 void Wait();
00138 inline void Enter()
00139 { Wait(); }
00140
00143 void Signal();
00144 inline void Leave()
00145 { Signal(); }
00146
00149 PObject * Clone() const
00150 { return new PCriticalSection(); }
00151
00153
00154 private:
00155 PCriticalSection & operator=(const PCriticalSection &) { return *this; }
00156
00157
00158 #ifdef _WIN32
00159 #include "msos/ptlib/critsec.h"
00160 #else
00161 #include "unix/ptlib/critsec.h"
00162 #endif
00163 };
00164
00165 typedef PWaitAndSignal PEnterAndLeave;
00166
00175 class PAtomicInteger
00176 {
00177 #if defined(_WIN32) || defined(DOC_PLUS_PLUS)
00178 public:
00181 inline PAtomicInteger(
00182 long v = 0
00183 )
00184 : value(v) { }
00185
00193 BOOL IsZero() const { return value == 0; }
00194
00200 inline long operator++() { return InterlockedIncrement(&value); }
00201
00207 inline long operator--() { return InterlockedDecrement(&value); }
00208
00212 inline operator long () const { return value; }
00213
00217 inline void SetValue(
00218 long v
00219 )
00220 { value = v; }
00221 protected:
00222 long value;
00223 #elif defined(_STLP_INTERNAL_THREADS_H) && defined(_STLP_ATOMIC_EXCHANGE)
00224 public:
00225 inline PAtomicInteger(__stl_atomic_t v = 0)
00226 : value(v) { }
00227 BOOL IsZero() const { return value == 0; }
00228 inline int operator++() { return _STLP_ATOMIC_INCREMENT(&value); }
00229 inline int unsigned operator--() { return _STLP_ATOMIC_DECREMENT(&value); }
00230 inline operator int () const { return value; }
00231 inline void SetValue(int v) { value = v; }
00232 protected:
00233 __stl_atomic_t value;
00234 #elif !defined(_STLP_INTERNAL_THREADS_H) && P_HAS_ATOMIC_INT
00235 public:
00236 inline PAtomicInteger(int v = 0)
00237 : value(v) { }
00238 BOOL IsZero() const { return value == 0; }
00239 inline int operator++() { return EXCHANGE_AND_ADD(&value, 1) + 1; }
00240 inline int unsigned operator--() { return EXCHANGE_AND_ADD(&value, -1) - 1; }
00241 inline operator int () const { return value; }
00242 inline void SetValue(int v) { value = v; }
00243 protected:
00244 _Atomic_word value;
00245 #else
00246 protected:
00247 PCriticalSection critSec;
00248 public:
00249 inline PAtomicInteger(int v = 0)
00250 : value(v) { }
00251 BOOL IsZero() const { return value == 0; }
00252 inline int operator++() { PWaitAndSignal m(critSec); value++; return value;}
00253 inline int operator--() { PWaitAndSignal m(critSec); value--; return value;}
00254 inline operator int () const { return value; }
00255 inline void SetValue(int v) { value = v; }
00256 protected:
00257 int value;
00258 #endif
00259 private:
00260 PAtomicInteger & operator=(const PAtomicInteger & ref) { value = (int)ref; return *this; }
00261 };
00262
00263 #endif
00264
00265