rw_lock.h
Go to the documentation of this file.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 #ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_RW_LOCK_H
00044 #define LASS_GUARDIAN_OF_INCLUSION_UTIL_RW_LOCK_H
00045
00046 #include "util_common.h"
00047 #include "atomic.h"
00048 #include "thread.h"
00049
00050 namespace lass
00051 {
00052 namespace util
00053 {
00054
00055 namespace impl
00056 {
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 class RWLock : NonCopyable
00074 {
00075
00076 public:
00077 RWLock(util::CallTraits<size_t>::TParam iMaxReaders);
00078 ~RWLock();
00079
00080 void lockr();
00081 void lockw();
00082 void unlockr();
00083 void unlockw();
00084
00085 const LockResult tryLockr();
00086 const LockResult tryLockw();
00087
00088 private:
00089 size_t maxReaders_;
00090 int spinLock_;
00091 int writersTrying_;
00092 };
00093
00094
00095 RWLock::RWLock(util::CallTraits<size_t>::TParam iMaxReaders)
00096 {
00097 maxReaders_ = iMaxReaders;
00098 spinLock_ = maxReaders_;
00099 writersTrying_ = 0;
00100 }
00101
00102 RWLock::~RWLock()
00103 {
00104 LASS_ENFORCE(spinLock_==maxReaders_);
00105 }
00106
00107 void RWLock::lockr()
00108 {
00109 int newSpinLock;
00110 int oldSpinLock;
00111 do
00112 {
00113 oldSpinLock = spinLock_;
00114 LASS_ASSERT(oldSpinLock>=0);
00115 newSpinLock = oldSpinLock-1;
00116 } while (writersTrying_!=0 || oldSpinLock==0 || !lass::util::atomicCompareAndSwap(spinLock_,oldSpinLock,newSpinLock));
00117 }
00118
00119 void RWLock::lockw()
00120 {
00121 lass::util::atomicIncrement(writersTrying_);
00122 do
00123 {
00124 } while (!lass::util::atomicCompareAndSwap<int>(spinLock_,maxReaders_,0));
00125 }
00126
00127 void RWLock::unlockw()
00128 {
00129 LASS_ENFORCE(spinLock_==0);
00130 do
00131 {
00132 } while (!lass::util::atomicCompareAndSwap<int>(spinLock_,0,maxReaders_));
00133 lass::util::atomicDecrement(writersTrying_);
00134 }
00135
00136 void RWLock::unlockr()
00137 {
00138 lass::util::atomicIncrement(spinLock_);
00139 LASS_ASSERT(spinLock_<=maxReaders_);
00140 }
00141
00142
00143 const LockResult RWLock::tryLockr()
00144 {
00145 int oldSpinLock;
00146 int newSpinLock;
00147 do
00148 {
00149 oldSpinLock = spinLock_;
00150 LASS_ASSERT(spinLock_ >= 0);
00151 if (spinLock_ == 0)
00152 {
00153 return lockBusy;
00154 }
00155 newSpinLock = oldSpinLock - 1;
00156 }
00157 while (!atomicCompareAndSwap(spinLock_, oldSpinLock, newSpinLock));
00158 return lockSuccess;
00159 }
00160
00161
00162 const LockResult RWLock::tryLockw()
00163 {
00164 lass::util::atomicIncrement(writersTrying_);
00165 do
00166 {
00167 LASS_ASSERT(spinLock_ >= 0);
00168 if (spinLock_ != maxReaders_)
00169 {
00170 lass::util::atomicDecrement(writersTrying_);
00171 return lockBusy;
00172 }
00173 }
00174 while (!atomicCompareAndSwap<int>(spinLock_, maxReaders_, 0));
00175 return lockSuccess;
00176 }
00177
00178 }
00179 }
00180
00181
00182 #endif
00183
00184