From df3af60bb3f65e51b5b9e8340d0c2ad754e29a33 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 18 Feb 2020 07:09:19 -0800 Subject: Event updates! It's...like an actual Event now. --- src/stable/event.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) (limited to 'src') 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 @@ namespace Bu { /** + * Represents a true/false state that controls thread synchronization. This + * is primarilly intended to control the synchronization state of + * multithreaded services. For example, telling all threads when to exit. + * + * An Event is either set or unset. If the Event is unset then it can be + * waited on for something to happen. As soon as the Event is set all + * waiting threads are released and new requests to wait are ignored until + * the Event is cleared again. + * + * Threads can also be woken up without setting the Event, which may be + * handy in certain circumstances. *@ingroup Threading */ class Event { public: - Event() + Event() : + bIsSet( false ) { } @@ -29,20 +41,50 @@ namespace Bu { } - void wait() + /** + * Wait indefinitely for the Event to trigger. If the event is already + * set, then return immediately. It's important to note that this may + * return at any time, not only when the Event is set, so examining the + * return value is important. + *@returns the set status of the Event. + */ + bool wait() { cBlock.lock(); + if( bIsSet ) + { + cBlock.unlock(); + return true; + } cBlock.wait(); + bool bRet = bIsSet; cBlock.unlock(); + return bIsSet; } - void wait( int nSec, int nUSec ) + /** + * Wait for up to nSec seconds and nUSec nanoseconds for the event to + * trigger. If the Event is already set then return immediately. + *@returns the set status of the Event. + */ + bool wait( int nSec, int nUSec ) { cBlock.lock(); + if( bIsSet ) + { + cBlock.unlock(); + return true; + } cBlock.wait( nSec, nUSec ); + bool bRet = bIsSet; cBlock.unlock(); + return bIsSet; } + /** + * Allow one of the waiting threads to unlock without updating the set + * state of the Event. + */ void unblockOne() { cBlock.lock(); @@ -50,6 +92,10 @@ namespace Bu cBlock.unlock(); } + /** + * Allow all waiting threads to unlock and proceed without updating the + * set state of the Event. + */ void unblockAll() { cBlock.lock(); @@ -57,8 +103,43 @@ namespace Bu cBlock.unlock(); } + /** + * Find out if the Event is in the set state or not. + *@returns True if set, false otherwise. + */ + bool isSet() + { + cBlock.lock(); + bool bRet = bIsSet; + cBlock.unlock(); + return bRet; + } + + /** + * Sets the Event's state to true and triggers all waiting threads. + */ + void set() + { + cBlock.lock(); + bIsSet = true; + cBlock.broadcast(); + cBlock.unlock(); + } + + /** + * Sets the Event's state to false. This does NOT trigger any waiting + * threads. + */ + void clear() + { + cBlock.lock(); + bIsSet = false; + cBlock.unlock(); + } + private: Condition cBlock; /**< The condition for blocking dequeues. */ + bool bIsSet; }; } -- cgit v1.2.3 From 0690dbdf8e57cd148d8b7f7ba56f23d673f7eb22 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 18 Feb 2020 08:41:03 -0800 Subject: Minor Bu::Event bugfix. It was returning the object state version of the set flag, not the threadsafe local copy. --- src/stable/event.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/stable/event.h b/src/stable/event.h index f12dc7d..5f3c819 100644 --- a/src/stable/event.h +++ b/src/stable/event.h @@ -59,7 +59,7 @@ namespace Bu cBlock.wait(); bool bRet = bIsSet; cBlock.unlock(); - return bIsSet; + return bRet; } /** @@ -78,7 +78,7 @@ namespace Bu cBlock.wait( nSec, nUSec ); bool bRet = bIsSet; cBlock.unlock(); - return bIsSet; + return bRet; } /** -- cgit v1.2.3 From ead9b8be325bdadb6b2a4e2a3b22dd34d7df8e4d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 18 Feb 2020 08:41:41 -0800 Subject: Renamed Bu::Thread::stop to Bu::Thread::forceStop. This wound up biting me, stop wasn't implemented in one child class and it was force-stopping it. Stop isn't usually implemented like this in threadding classes, and this should be no exception. I'm still exposing the posix threads stop functionality, but it really shouldn't even be used. This may break some things, but if it does, they probably weren't working correctly anyway. Sorry. --- src/stable/thread.cpp | 2 +- src/stable/thread.h | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src') 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() return true; } -bool Bu::Thread::stop() +bool Bu::Thread::forceStop() { pthread_cancel( ptHandle ); 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 * probably create a memory leak type of situation. Instead of stop, * consider using cancel, which can be handled by the running thread in * a graceful manner. + * + * NOTE: This was called stop(), but that was too ambiguous and common + * of a name to implement in subclasses. The functionality was not + * obvious, and this was sometimes used in place of a clean stopping + * routine. *@returns True if the thread was stopped, false otherwise. When this * function returns the thread may not have stopped, to ensure that the * thread has really stopped, call join. */ - bool stop(); + bool forceStop(); /** * Join the thread in action. This function performs what is commonly -- cgit v1.2.3 From 9db480db19a6929d958edd143d22fbf249afcb6f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 23 Mar 2020 13:48:33 -0700 Subject: Updated to work with newer GCC. Super minor type mismatch, which it was quite correct about. --- src/stable/optparser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') 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 ) { pOpt->pProxy->setValueFromStr( sExtraParam ); } - else if( argv[j+1] != '\0' ) + else if( argv[j+1] != 0 ) { pOpt->pProxy->setValueFromStr( argv[j+1] ); j++; -- cgit v1.2.3