summaryrefslogtreecommitdiff
path: root/src/unstable/readwritemutex.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2013-02-21 03:55:56 +0000
committerMike Buland <eichlan@xagasoft.com>2013-02-21 03:55:56 +0000
commit9dd51c94844bc7e5f8b1552511d31ed2c9bac05c (patch)
tree3dd505ddea45f47f2a413eba05074a9605afbe6c /src/unstable/readwritemutex.h
parent123434f227a963cf26f9637136bf759a824f930a (diff)
downloadlibbu++-9dd51c94844bc7e5f8b1552511d31ed2c9bac05c.tar.gz
libbu++-9dd51c94844bc7e5f8b1552511d31ed2c9bac05c.tar.bz2
libbu++-9dd51c94844bc7e5f8b1552511d31ed2c9bac05c.tar.xz
libbu++-9dd51c94844bc7e5f8b1552511d31ed2c9bac05c.zip
Added the Bu::ReadWriteMutex, which is super awesome. Also made the
Bu::RandomBase::rand functions visible in the Bu::RandomCmwc class.
Diffstat (limited to 'src/unstable/readwritemutex.h')
-rw-r--r--src/unstable/readwritemutex.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/unstable/readwritemutex.h b/src/unstable/readwritemutex.h
new file mode 100644
index 0000000..9e07047
--- /dev/null
+++ b/src/unstable/readwritemutex.h
@@ -0,0 +1,75 @@
1#ifndef BU_READ_WRITE_MUTEX_H
2#define BU_READ_WRITE_MUTEX_H
3
4#include "bu/mutex.h"
5#include "bu/condition.h"
6
7namespace Bu
8{
9 /**
10 * Mutex designed for situations where overlapped reading is safe, but
11 * overlapped writing isn't. There are many, many good examples of this
12 * including most data structures, streams, etc. etc.
13 *
14 * Use this just like a normal mutex except that you use the
15 * lockRead/unlockRead and lockWrite/unlockWrite functions depending on
16 * weather the section of code your locking is reading data or changing
17 * data.
18 *
19 * This particular mutex is designed so that while a read operation is
20 * happening other read operations can also happen, but no write operations
21 * can occur. While a write is happening, no other write or read operation
22 * can continue. There is an extra feature to ensure writes get a chance
23 * to complete, when a lockWrite is issued, all current read operations
24 * continue, but future read operations block until the write is complete.
25 */
26 class ReadWriteMutex
27 {
28 public:
29 ReadWriteMutex();
30 virtual ~ReadWriteMutex();
31
32 /**
33 * Lock the mutex for reading. Multiple code sections can hold a read
34 * lock at the same time, but write locks will wait for all read locks
35 * to be released before continuing. Read locks will also wait if
36 * there is an active write lock.
37 *
38 * It is very important to not make any changes to your data within
39 * a read lock.
40 */
41 void lockRead();
42
43 /**
44 * Release a read lock.
45 */
46 void unlockRead();
47
48 /**
49 * Lock the mutex for writing. Only one code section can have a write
50 * lock at any given time. No code sections can be in a locked read
51 * section while a write lock is held. When a write lock is requested
52 * all following read locks will block until the write operation is
53 * started, ensuring writes always get a chance to execute.
54 *
55 * Within a write locked code section feel free to change your data
56 * and read your data. It is imparative to spend as little time as
57 * possible in a write-locked section.
58 */
59 void lockWrite();
60
61 /**
62 * Release a write lock.
63 */
64 void unlockWrite();
65
66 private:
67 Bu::Mutex mRead;
68 int iCounter;
69 Bu::Mutex mWrite;
70 Bu::Condition cWrite;
71 bool bWantWrite;
72 };
73};
74
75#endif