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
00098
00099
00100
00101 #ifndef _PCRITICALSECTION
00102 #define _PCRITICALSECTION
00103
00104 #include <ptlib/psync.h>
00105
00106 #if P_HAS_ATOMIC_INT
00107 #if P_NEEDS_GNU_CXX_NAMESPACE
00108 #define EXCHANGE_AND_ADD(v,i) __gnu_cxx::__exchange_and_add(v,i)
00109 #else
00110 #define EXCHANGE_AND_ADD(v,i) __exchange_and_add(v,i)
00111 #endif
00112 #endif
00113
00120 #ifdef _WIN32
00121
00122 class PCriticalSection : public PSync
00123 {
00124 PCLASSINFO(PCriticalSection, PSync);
00125
00126 public:
00131 PCriticalSection();
00132 PCriticalSection(const PCriticalSection &);
00133
00136 ~PCriticalSection();
00138
00143 void Wait();
00144 inline void Enter()
00145 { Wait(); }
00146
00149 void Signal();
00150 inline void Leave()
00151 { Signal(); }
00152
00155 PObject * Clone() const
00156 { return new PCriticalSection(); }
00157
00159
00160 private:
00161 PCriticalSection & operator=(const PCriticalSection &) { return *this; }
00162
00163 #include "msos/ptlib/critsec.h"
00164
00165 };
00166
00167 #endif
00168
00169 typedef PWaitAndSignal PEnterAndLeave;
00170
00179 class PAtomicInteger
00180 {
00181 #if defined(_WIN32) || defined(DOC_PLUS_PLUS)
00182 public:
00185 inline PAtomicInteger(
00186 long v = 0
00187 )
00188 : value(v) { }
00189
00197 BOOL IsZero() const { return value == 0; }
00198
00204 inline long operator++() { return InterlockedIncrement(&value); }
00205
00211 inline long operator--() { return InterlockedDecrement(&value); }
00212
00216 inline operator long () const { return value; }
00217
00221 inline void SetValue(
00222 long v
00223 )
00224 { value = v; }
00225 protected:
00226 long value;
00227 #elif defined(_STLP_INTERNAL_THREADS_H) && defined(_STLP_ATOMIC_EXCHANGE)
00228 public:
00229 inline PAtomicInteger(__stl_atomic_t v = 0)
00230 : value(v) { }
00231 BOOL IsZero() const { return value == 0; }
00232 inline int operator++() { return _STLP_ATOMIC_INCREMENT(&value); }
00233 inline int unsigned operator--() { return _STLP_ATOMIC_DECREMENT(&value); }
00234 inline operator int () const { return value; }
00235 inline void SetValue(int v) { value = v; }
00236 protected:
00237 __stl_atomic_t value;
00238 #elif !defined(_STLP_INTERNAL_THREADS_H) && P_HAS_ATOMIC_INT
00239 public:
00240 inline PAtomicInteger(int v = 0)
00241 : value(v) { }
00242 BOOL IsZero() const { return value == 0; }
00243 inline int operator++() { return EXCHANGE_AND_ADD(&value, 1) + 1; }
00244 inline int unsigned operator--() { return EXCHANGE_AND_ADD(&value, -1) - 1; }
00245 inline operator int () const { return value; }
00246 inline void SetValue(int v) { value = v; }
00247 protected:
00248 _Atomic_word value;
00249 #else
00250 protected:
00251 PCriticalSection critSec;
00252 public:
00253 inline PAtomicInteger(int v = 0)
00254 : value(v) { }
00255 BOOL IsZero() const { return value == 0; }
00256 inline int operator++() { PWaitAndSignal m(critSec); value++; return value;}
00257 inline int operator--() { PWaitAndSignal m(critSec); value--; return value;}
00258 inline operator int () const { return value; }
00259 inline void SetValue(int v) { value = v; }
00260 protected:
00261 int value;
00262 #endif
00263 private:
00264 PAtomicInteger & operator=(const PAtomicInteger & ref) { value = (int)ref; return *this; }
00265 };
00266
00267 #endif
00268
00269