1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
#include <iostream>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "rwmutex.h"
using namespace std;
#ifdef HAVE_LIBPTHREAD
// pthreads-based implementation
RWMutex::RWMutex() : PTMutex(), lockcount(0), exclusive(0)
{
for(int i=0;i<RWMUTEX_THREADS_MAX;++i)
{
counttable[i].id=0;
counttable[i].count=0;
}
pthread_cond_init(&cond,0);
}
RWMutex::~RWMutex()
{
}
// ObtainMutex - obtains the mutex in exclusive mode.
// Succeeds if there are no locks currently held, or if all currently held
// locks are held by the current thread.
// Once the lock is held, no other threads will be able to obtain a lock,
// either exclusive or shared. The thread holding the lock can, however,
// obtain further locks of either exclusive or shared type.
// This differs from pthreads rwlocks - their behaviour is undefined if a
// thread holding a write-lock attempts to obtain a read-lock or vice versa.
// If a thread holds a shared lock, then obtains an exclusive lock, and
// another shared lock, the lock will remain in exclusive mode until the
// inner two locks have been released, then revert to shared mode.
void RWMutex::ObtainMutex()
{
// static int id=0;
// int tid=id++;
PTMutex::ObtainMutex();
// cerr << tid << ": Obtaining exclusive" << endl;
while(!CheckExclusive())
{
// cerr << tid << ": Obtain exclusive failed - waiting..." << endl;
// cerr << "Exclusive count: " << exclusive << endl;
pthread_cond_wait(&cond,&mutex);
}
// cerr << tid << ": Obtain exclusive succeeded - incrementing..." << endl;
Increment();
if(exclusive==0)
++exclusive;
// Dump();
PTMutex::ReleaseMutex();
}
// ObtainMutexShared - obtains the mutex in non-exclusive mode.
// Succeeds if there are no exclusive locks currently held
// Once the lock is held, other threads will be able to obtain shared locks,
// but not exclusive locks. The thread holding the lock can, however, obtain
// a subsequent exclusive lock. The lock will revert to shared mode when
// the second lock is released.
// This differs from pthreads rwlocks - their behaviour is undefined if a
// thread holding a write-lock attempts to obtain a read-lock or vice versa.
void RWMutex::ObtainMutexShared()
{
// static int id=0;
// int tid=id++;
PTMutex::ObtainMutex();
// cerr << tid << ": Obtaining shared" << endl;
while((exclusive!=0) && !CheckExclusive())
{
// cerr << tid << ": Obtain shared failed - waiting..." << endl;
// Dump();
// We must wait until the exclusive flag is clear;
pthread_cond_wait(&cond,&mutex);
}
// cerr << tid << ": Obtain shared succeeded - incrementing..." << endl;
Increment();
// Dump();
PTMutex::ReleaseMutex();
}
bool RWMutex::AttemptMutex()
{
bool result=false;
PTMutex::ObtainMutex();
if(CheckExclusive())
{
// cerr << "Obtained exclusive lock - lock count: " << lockcount << endl;
Increment();
if(exclusive==0)
++exclusive;
// Dump();
result=true;
}
PTMutex::ReleaseMutex();
return(result);
}
bool RWMutex::AttemptMutexShared()
{
bool result=false;
PTMutex::ObtainMutex();
if((exclusive==0) || (CheckExclusive()))
{
Increment();
// Dump();
result=true;
}
PTMutex::ReleaseMutex();
return(result);
}
void RWMutex::ReleaseMutex()
{
PTMutex::ObtainMutex();
Decrement();
// Dump();
pthread_cond_broadcast(&cond);
PTMutex::ReleaseMutex();
}
bool RWMutex::CheckExclusive()
{
if(lockcount==0)
return(true);
// if(exclusive==0);
// return(true);
// If the lockcount is greater than zero, then we have to
// step through the thread table making sure that the
// current thread is the only one holding locks.
pthread_t current=pthread_self();
cerr << "Current thread: " << current << endl;
for(int i=0;i<RWMUTEX_THREADS_MAX;++i)
{
// If any thread other than this one has count>0 return false;
if(counttable[i].id!=current && counttable[i].count!=0)
return(false);
// If the current thread's count == the global lockcount, return true.
if(counttable[i].id==current && counttable[i].count==lockcount)
return(true);
}
// If we reached here, then the global lockcount is greater than zero, but
// the thread table is inconsistent. Succeed grudgingly.
cerr << "RWMutex: inconsistent locking data." << endl;
return(true);
}
void RWMutex::Increment()
{
++lockcount;
if(exclusive)
++exclusive;
// Find the current thread in the table and increment its count
pthread_t current=pthread_self();
// cerr << "Increment - searching for thread: " << current << endl;
for(int i=0;i<RWMUTEX_THREADS_MAX;++i)
{
if(counttable[i].id==current)
{
// cerr << "found" << endl;
++counttable[i].count;
return;
}
}
// If none found, add a new entry, and set the count to 1;
// cerr << "Increment - searching for free slot" << endl;
while(1)
{
for(int i=0;i<RWMUTEX_THREADS_MAX;++i)
{
if(counttable[i].id==0)
{
// cerr << "found" << endl;
counttable[i].id=current;
counttable[i].count=1;
return;
}
}
// If there were no free slots, complain.
cerr << "RWMutex: thread table full - waiting..." << endl;
pthread_cond_wait(&cond,&mutex);
cerr << "RWMutex - trying again to find a free slot... " << endl;
}
}
void RWMutex::Decrement()
{
--lockcount;
if(exclusive)
--exclusive;
pthread_t current=pthread_self();
// cerr << "Decrement - searching for thread: " << current << endl;
for(int i=0;i<RWMUTEX_THREADS_MAX;++i)
{
if(counttable[i].id==current)
{
--counttable[i].count;
if(counttable[i].count==0)
counttable[i].id=0;
return;
}
}
// If thread was not found, complain.
// cerr << "RWMutex: thread not found in table." << endl;
}
void RWMutex::Dump()
{
cerr << "Locks held: " << lockcount << endl;
cerr << "Exclusive count: " << exclusive << endl;
for(int i=0;i<RWMUTEX_THREADS_MAX;++i)
{
if(counttable[i].id)
cerr << "Thread: " << counttable[i].id << ", count: " << counttable[i].count << endl;
}
}
#else
// Dummy implementation. Obtaining the rwlock always succeeds.
RWMutex::RWMutex()
{
cerr << "Warning - building a dummy rwlock" << endl;
}
RWMutex::~RWMutex()
{
}
void RWMutex::ObtainMutex()
{
cerr << "Warning - obtaining a dummy rwlock" << endl;
}
bool RWMutex::AttemptMutex()
{
cerr << "Warning - attempting a dummy rwlock" << endl;
return(true);
}
void RWMutex::ReleaseMutex()
{
cerr << "Warning - releasing a dummy rwlock" << endl;
}
#endif
|