aboutsummaryrefslogtreecommitdiff
path: root/src/stable
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable')
-rw-r--r--src/stable/event.h87
1 files changed, 84 insertions, 3 deletions
diff --git a/src/stable/event.h b/src/stable/event.h
index db4f846..f12dc7d 100644
--- a/src/stable/event.h
+++ b/src/stable/event.h
@@ -16,12 +16,24 @@
16namespace Bu 16namespace Bu
17{ 17{
18 /** 18 /**
19 * Represents a true/false state that controls thread synchronization. This
20 * is primarilly intended to control the synchronization state of
21 * multithreaded services. For example, telling all threads when to exit.
22 *
23 * An Event is either set or unset. If the Event is unset then it can be
24 * waited on for something to happen. As soon as the Event is set all
25 * waiting threads are released and new requests to wait are ignored until
26 * the Event is cleared again.
27 *
28 * Threads can also be woken up without setting the Event, which may be
29 * handy in certain circumstances.
19 *@ingroup Threading 30 *@ingroup Threading
20 */ 31 */
21 class Event 32 class Event
22 { 33 {
23 public: 34 public:
24 Event() 35 Event() :
36 bIsSet( false )
25 { 37 {
26 } 38 }
27 39
@@ -29,20 +41,50 @@ namespace Bu
29 { 41 {
30 } 42 }
31 43
32 void wait() 44 /**
45 * Wait indefinitely for the Event to trigger. If the event is already
46 * set, then return immediately. It's important to note that this may
47 * return at any time, not only when the Event is set, so examining the
48 * return value is important.
49 *@returns the set status of the Event.
50 */
51 bool wait()
33 { 52 {
34 cBlock.lock(); 53 cBlock.lock();
54 if( bIsSet )
55 {
56 cBlock.unlock();
57 return true;
58 }
35 cBlock.wait(); 59 cBlock.wait();
60 bool bRet = bIsSet;
36 cBlock.unlock(); 61 cBlock.unlock();
62 return bIsSet;
37 } 63 }
38 64
39 void wait( int nSec, int nUSec ) 65 /**
66 * Wait for up to nSec seconds and nUSec nanoseconds for the event to
67 * trigger. If the Event is already set then return immediately.
68 *@returns the set status of the Event.
69 */
70 bool wait( int nSec, int nUSec )
40 { 71 {
41 cBlock.lock(); 72 cBlock.lock();
73 if( bIsSet )
74 {
75 cBlock.unlock();
76 return true;
77 }
42 cBlock.wait( nSec, nUSec ); 78 cBlock.wait( nSec, nUSec );
79 bool bRet = bIsSet;
43 cBlock.unlock(); 80 cBlock.unlock();
81 return bIsSet;
44 } 82 }
45 83
84 /**
85 * Allow one of the waiting threads to unlock without updating the set
86 * state of the Event.
87 */
46 void unblockOne() 88 void unblockOne()
47 { 89 {
48 cBlock.lock(); 90 cBlock.lock();
@@ -50,6 +92,10 @@ namespace Bu
50 cBlock.unlock(); 92 cBlock.unlock();
51 } 93 }
52 94
95 /**
96 * Allow all waiting threads to unlock and proceed without updating the
97 * set state of the Event.
98 */
53 void unblockAll() 99 void unblockAll()
54 { 100 {
55 cBlock.lock(); 101 cBlock.lock();
@@ -57,8 +103,43 @@ namespace Bu
57 cBlock.unlock(); 103 cBlock.unlock();
58 } 104 }
59 105
106 /**
107 * Find out if the Event is in the set state or not.
108 *@returns True if set, false otherwise.
109 */
110 bool isSet()
111 {
112 cBlock.lock();
113 bool bRet = bIsSet;
114 cBlock.unlock();
115 return bRet;
116 }
117
118 /**
119 * Sets the Event's state to true and triggers all waiting threads.
120 */
121 void set()
122 {
123 cBlock.lock();
124 bIsSet = true;
125 cBlock.broadcast();
126 cBlock.unlock();
127 }
128
129 /**
130 * Sets the Event's state to false. This does NOT trigger any waiting
131 * threads.
132 */
133 void clear()
134 {
135 cBlock.lock();
136 bIsSet = false;
137 cBlock.unlock();
138 }
139
60 private: 140 private:
61 Condition cBlock; /**< The condition for blocking dequeues. */ 141 Condition cBlock; /**< The condition for blocking dequeues. */
142 bool bIsSet;
62 }; 143 };
63} 144}
64 145