aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/stable/event.h87
-rw-r--r--src/stable/optparser.cpp2
-rw-r--r--src/stable/thread.cpp2
-rw-r--r--src/stable/thread.h7
4 files changed, 92 insertions, 6 deletions
diff --git a/src/stable/event.h b/src/stable/event.h
index db4f846..5f3c819 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 bRet;
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 bRet;
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
diff --git a/src/stable/optparser.cpp b/src/stable/optparser.cpp
index 0f9b2b1..c0f5732 100644
--- a/src/stable/optparser.cpp
+++ b/src/stable/optparser.cpp
@@ -77,7 +77,7 @@ void Bu::OptParser::parse( int argc, char **argv )
77 { 77 {
78 pOpt->pProxy->setValueFromStr( sExtraParam ); 78 pOpt->pProxy->setValueFromStr( sExtraParam );
79 } 79 }
80 else if( argv[j+1] != '\0' ) 80 else if( argv[j+1] != 0 )
81 { 81 {
82 pOpt->pProxy->setValueFromStr( argv[j+1] ); 82 pOpt->pProxy->setValueFromStr( argv[j+1] );
83 j++; 83 j++;
diff --git a/src/stable/thread.cpp b/src/stable/thread.cpp
index 98f08ec..7d362e9 100644
--- a/src/stable/thread.cpp
+++ b/src/stable/thread.cpp
@@ -51,7 +51,7 @@ bool Bu::Thread::start()
51 return true; 51 return true;
52} 52}
53 53
54bool Bu::Thread::stop() 54bool Bu::Thread::forceStop()
55{ 55{
56 pthread_cancel( ptHandle ); 56 pthread_cancel( ptHandle );
57 57
diff --git a/src/stable/thread.h b/src/stable/thread.h
index 294c250..5174d1c 100644
--- a/src/stable/thread.h
+++ b/src/stable/thread.h
@@ -79,11 +79,16 @@ namespace Bu
79 * probably create a memory leak type of situation. Instead of stop, 79 * probably create a memory leak type of situation. Instead of stop,
80 * consider using cancel, which can be handled by the running thread in 80 * consider using cancel, which can be handled by the running thread in
81 * a graceful manner. 81 * a graceful manner.
82 *
83 * NOTE: This was called stop(), but that was too ambiguous and common
84 * of a name to implement in subclasses. The functionality was not
85 * obvious, and this was sometimes used in place of a clean stopping
86 * routine.
82 *@returns True if the thread was stopped, false otherwise. When this 87 *@returns True if the thread was stopped, false otherwise. When this
83 * function returns the thread may not have stopped, to ensure that the 88 * function returns the thread may not have stopped, to ensure that the
84 * thread has really stopped, call join. 89 * thread has really stopped, call join.
85 */ 90 */
86 bool stop(); 91 bool forceStop();
87 92
88 /** 93 /**
89 * Join the thread in action. This function performs what is commonly 94 * Join the thread in action. This function performs what is commonly