ucommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00031 // we do this twice because of some bizarre issue in just this file that 00032 // otherwise breaks doxygen and lists all items outside the namespace... 00033 #include <ucommon/platform.h> 00034 00035 #ifndef _UCOMMON_ACCESS_H_ 00036 #define _UCOMMON_ACCESS_H_ 00037 00038 #ifndef _UCOMMON_CPR_H_ 00039 #include <ucommon/cpr.h> 00040 #endif 00041 00042 NAMESPACE_UCOMMON 00043 00050 class __EXPORT ExclusiveProtocol 00051 { 00052 protected: 00053 virtual ~ExclusiveProtocol(); 00054 00055 public: 00059 virtual void Exlock(void) = 0; 00060 00064 virtual void Unlock(void) = 0; 00065 00069 inline void Lock(void) 00070 {Exlock();}; 00071 }; 00072 00079 class __EXPORT SharedProtocol 00080 { 00081 protected: 00082 virtual ~SharedProtocol(); 00083 00084 public: 00088 virtual void Shlock(void) = 0; 00089 00093 virtual void Unlock(void) = 0; 00094 00101 virtual void Share(void); 00102 00110 virtual void Exclusive(void); 00111 00115 inline void Lock(void) 00116 {Shlock();}; 00117 }; 00118 00126 class __EXPORT exclusive_lock 00127 { 00128 private: 00129 ExclusiveProtocol *lock; 00130 00131 public: 00136 exclusive_lock(ExclusiveProtocol *object); 00137 00141 ~exclusive_lock(); 00142 00147 inline bool operator!() const 00148 {return lock == NULL;}; 00149 00154 inline operator bool() const 00155 {return lock != NULL;}; 00156 00162 void release(void); 00163 }; 00164 00172 class __EXPORT shared_lock 00173 { 00174 private: 00175 SharedProtocol *lock; 00176 int state; 00177 bool modify; 00178 00179 public: 00184 shared_lock(SharedProtocol *object); 00185 00189 ~shared_lock(); 00190 00195 inline bool operator!() const 00196 {return lock == NULL;}; 00197 00202 inline operator bool() const 00203 {return lock != NULL;}; 00204 00210 void release(void); 00211 00215 void exclusive(void); 00216 00220 void share(void); 00221 }; 00222 00227 inline void lock(ExclusiveProtocol *object) 00228 {object->Exlock();} 00229 00234 inline void unlock(ExclusiveProtocol *object) 00235 {object->Unlock();} 00236 00241 inline void access(SharedProtocol *object) 00242 {object->Shlock();} 00243 00248 inline void release(SharedProtocol *object) 00249 {object->Unlock();} 00250 00255 inline void exclusive(SharedProtocol *object) 00256 {object->Exclusive();} 00257 00262 inline void share(SharedProtocol *object) 00263 {object->Share();} 00264 00268 typedef exclusive_lock exlock_t; 00269 00273 typedef shared_lock shlock_t; 00274 00279 inline void release(exlock_t &reference) 00280 {reference.release();} 00281 00286 inline void release(shlock_t &reference) 00287 {reference.release();} 00288 00289 // Special macros to allow member functions of an object with a protocol 00290 // to create self locking states while the member functions are called by 00291 // placing an exclusive_lock or shared_lock smart object on their stack 00292 // frame to reference their self. 00293 00294 #define exclusive_object() exlock_t __autolock__ = this 00295 #define protected_object() shlock_t __autolock__ = this 00296 #define exclusive_access(x) exlock_t __autolock__ = &x 00297 #define protected_access(x) shlock_t __autolock__ = &x 00298 00299 END_NAMESPACE 00300 00301 #endif