Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
readwritelock.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_READWRITELOCK_H_
19 #define ION_BASE_READWRITELOCK_H_
20 
21 #include "ion/port/atomic.h"
22 #include "ion/port/mutex.h"
23 #include "ion/port/semaphore.h"
24 
25 namespace ion {
26 namespace base {
27 
51 class ION_API ReadWriteLock {
52  public:
53  ReadWriteLock();
54  ~ReadWriteLock();
55 
59  void LockForRead();
62  void UnlockForRead();
65  void LockForWrite();
68  void UnlockForWrite();
69 
71  int GetReaderCount() const { return reader_count_; }
73  int GetWriterCount() const { return writer_count_; }
74 
75  private:
76  std::atomic<int> reader_count_;
77  std::atomic<int> writer_count_;
78  port::Semaphore room_empty_;
79  port::Mutex turnstile_;
80 
81  DISALLOW_COPY_AND_ASSIGN(ReadWriteLock);
82 };
83 
86 class ION_API ReadLock {
87  public:
89  explicit ReadLock(ReadWriteLock* lock) : lock_(*lock) {}
90  ~ReadLock() {}
93  void Lock() { lock_.LockForRead(); }
96  void Unlock() { lock_.UnlockForRead(); }
101  bool IsLocked() const { return lock_.GetReaderCount() > 0; }
102 
103  private:
104  ReadWriteLock& lock_;
105 
106  DISALLOW_IMPLICIT_CONSTRUCTORS(ReadLock);
107 };
108 
111 class ION_API WriteLock {
112  public:
114  explicit WriteLock(ReadWriteLock* lock) : lock_(*lock) {}
118  void Lock() { lock_.LockForWrite(); }
121  void Unlock() { lock_.UnlockForWrite(); }
124  bool IsLocked() const { return lock_.GetWriterCount() > 0; }
125 
126  private:
127  ReadWriteLock& lock_;
128 
129  DISALLOW_IMPLICIT_CONSTRUCTORS(WriteLock);
130 };
131 
132 } // namespace base
133 } // namespace ion
134 
135 #endif // ION_BASE_READWRITELOCK_H_
void Lock()
"Locks" the lock for reading, which may or may not block.
Definition: readwritelock.h:93
A Semaphore enables threads and process synchronization.
Definition: semaphore.h:40
void Unlock()
"Unlocks" the lock for reading, which may allow other writers to proceed.
Definition: readwritelock.h:96
int GetWriterCount() const
Returns the number of writers in this lock.
Definition: readwritelock.h:73
ReadLock(ReadWriteLock *lock)
The passed pointer must be non-NULL.
Definition: readwritelock.h:89
bool IsLocked() const
Returns whether there are any readers in the lock.
The ReadWriteLock class defines a non-promotable lock that is very fast when only readers try to obta...
Definition: readwritelock.h:51
void Lock()
"Locks" the lock for writing, which blocks if there are any readers or writers holding the lock...
A WriteLock obtains a write lock, but has a similar interface to a Mutex and can be used with a Write...
void Unlock()
"Unlocks" the lock for writing, which may allow other readers or writers to proceed.
int GetReaderCount() const
Returns the number of readers in this lock.
Definition: readwritelock.h:71
A ReadLock obtains a read lock, but has a similar interface to a Mutex and can be used with a ReadGua...
Definition: readwritelock.h:86
WriteLock(ReadWriteLock *lock)
The passed pointer must be non-NULL.
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
bool IsLocked() const
Returns whether there are any writers in the lock.