Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
readwritelock.cc
Go to the documentation of this file.
1 
18 #include "ion/base/readwritelock.h"
19 
20 namespace ion {
21 namespace base {
22 
25  : reader_count_(0), writer_count_(0), room_empty_(1) {}
26 
28 
32  if (writer_count_) {
33  turnstile_.Lock();
34  turnstile_.Unlock();
35  }
38  if (++reader_count_ == 1)
39  room_empty_.Wait();
40 }
41 
44  if (--reader_count_ == 0)
45  room_empty_.Post();
46 }
47 
49  ++writer_count_;
50  turnstile_.Lock();
51  room_empty_.Wait();
52 }
53 
56  turnstile_.Unlock();
57  room_empty_.Post();
58  --writer_count_;
59 }
60 
61 } // namespace base
62 } // namespace ion
bool Post()
Wakes a single thread that is Wait()ing, or the next thread to call Wait().
Definition: semaphore.cc:85
bool Wait()
Blocks the calling thread until another thread calls Post().
Definition: semaphore.cc:205
void UnlockForRead()
Unlocks the ReadWriteLock for reading, which will allow writers to obtain a lock once the last reader...
void LockForWrite()
Locks the ReadWriteLock for writing.
void Unlock()
Unlocks the Mutex.
Definition: mutex.cc:145
void UnlockForWrite()
Unlocks the ReadWriteLock for writing.
void Lock()
Locks the Mutex.
Definition: mutex.cc:92
ReadWriteLock()
The semaphore is initialized to 1 so that the first Wait() succeeds.
void LockForRead()
Locks the ReadWriteLock for reading.