00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00032 #ifndef _UCOMMON_ACCESS_H_
00033 #define _UCOMMON_ACCESS_H_
00034
00035 #ifndef _UCOMMON_CONFIG_H_
00036 #include <ucommon/platform.h>
00037 #endif
00038
00039 NAMESPACE_UCOMMON
00040
00047 class __EXPORT Exclusive
00048 {
00049 protected:
00050 virtual ~Exclusive();
00051
00052 public:
00056 virtual void Exlock(void) = 0;
00057
00061 virtual void Unlock(void) = 0;
00062
00066 inline void Lock(void)
00067 {Exlock();};
00068 };
00069
00076 class __EXPORT Shared
00077 {
00078 protected:
00079 virtual ~Shared();
00080
00081 public:
00085 virtual void Shlock(void) = 0;
00086
00090 virtual void Unlock(void) = 0;
00091
00098 virtual void Share(void);
00099
00107 virtual void Exclusive(void);
00108
00112 inline void Lock(void)
00113 {Shlock();};
00114 };
00115
00123 class __EXPORT exclusive_lock
00124 {
00125 private:
00126 Exclusive *lock;
00127
00128 public:
00133 exclusive_lock(Exclusive *object);
00134
00138 ~exclusive_lock();
00139
00144 bool operator!()
00145 {return lock == NULL;};
00146
00151 operator bool()
00152 {return lock != NULL;};
00153
00159 void release(void);
00160 };
00161
00169 class __EXPORT shared_lock
00170 {
00171 private:
00172 Shared *lock;
00173 int state;
00174 bool modify;
00175
00176 public:
00181 shared_lock(Shared *object);
00182
00186 ~shared_lock();
00187
00192 bool operator!()
00193 {return lock == NULL;};
00194
00199 operator bool()
00200 {return lock != NULL;};
00201
00207 void release(void);
00208
00212 void exclusive(void);
00213
00217 void share(void);
00218 };
00219
00224 inline void lock(Exclusive *object)
00225 {object->Exlock();}
00226
00231 inline void unlock(Exclusive *object)
00232 {object->Unlock();}
00233
00238 inline void access(Shared *object)
00239 {object->Shlock();}
00240
00245 inline void release(Shared *object)
00246 {object->Unlock();}
00247
00252 inline void exclusive(Shared *object)
00253 {object->Exclusive();}
00254
00259 inline void share(Shared *object)
00260 {object->Share();}
00261
00265 typedef exclusive_lock exlock_t;
00266
00270 typedef shared_lock shlock_t;
00271
00276 inline void release(exlock_t &reference)
00277 {reference.release();}
00278
00283 inline void release(shlock_t &reference)
00284 {reference.release();}
00285
00286
00287
00288
00289
00290
00291 #define exclusive_object() exlock_t __autolock__ = this
00292 #define protected_object() shlock_t __autolock__ = this
00293 #define exclusive_access(x) exlock_t __autolock__ = &x
00294 #define protected_access(x) shlock_t __autolock__ = &x
00295
00296 END_NAMESPACE
00297
00298 #endif