Bayonne2 / Common C++ 2 Framework
|
Somewhat generic queue processing class to establish a producer consumer queue. More...
#include <buffer.h>
Data Structures | |
struct | _data |
Public Types | |
enum | Throw { throwNothing, throwObject, throwException } |
How to raise error. More... | |
typedef enum Thread::Throw | Throw |
How to raise error. More... | |
enum | Cancel { cancelInitial =0, cancelDeferred =1, cancelImmediate, cancelDisabled, cancelManual, cancelDefault =cancelDeferred } |
How work cancellation. More... | |
typedef enum Thread::Cancel | Cancel |
How work cancellation. More... | |
enum | Suspend { suspendEnable, suspendDisable } |
How work suspend. More... | |
typedef enum Thread::Suspend | Suspend |
How work suspend. More... | |
Public Member Functions | |
ThreadQueue (const char *id, int pri, size_t stack=0) | |
Create instance of our queue and give it a process priority. More... | |
virtual | ~ThreadQueue () |
Destroy the queue. More... | |
void | setTimer (timeout_t timeout) |
Set the queue timeout. More... | |
void | post (const void *data, unsigned len) |
Put some unspecified data into this queue. More... | |
void | nameMutex (const char *name) |
Enable setting of mutex name for deadlock debug. More... | |
void | enterMutex (void) |
Entering a Mutex locks the mutex for the current thread. More... | |
void | enter (void) |
Future abi will use enter/leave/test members. More... | |
void | leave (void) |
Future abi will use enter/leave/test members. More... | |
bool | test (void) |
Future abi will use enter/leave/test members. More... | |
bool | tryEnterMutex (void) |
Tries to lock the mutex for the current thread. More... | |
void | leaveMutex (void) |
Leaving a mutex frees that mutex for use by another thread. More... | |
int | start (Semaphore *start=0) |
When a new thread is created, it does not begin immediate execution. More... | |
int | detach (Semaphore *start=0) |
Start a new thread as "detached". More... | |
Thread * | getParent (void) |
Gets the pointer to the Thread class which created the current thread object. More... | |
void | suspend (void) |
Suspends execution of the selected thread. More... | |
void | resume (void) |
Resumes execution of the selected thread. More... | |
Cancel | getCancel (void) |
Used to retrieve the cancellation mode in effect for the selected thread. More... | |
bool | isRunning (void) const |
Verifies if the thread is still running or has already been terminated but not yet deleted. More... | |
bool | isDetached (void) const |
Check if this thread is detached. More... | |
void | join (void) |
Blocking call which unlocks when thread terminates. More... | |
bool | isThread (void) const |
Tests to see if the current execution context is the same as the specified thread object. More... | |
cctid_t | getId (void) const |
Get system thread numeric identifier. More... | |
const char * | getName (void) const |
Get the name string for this thread, to use in debug messages. More... | |
bool | wait (timeout_t timeout=0) |
Wait is used to keep a thread held until the semaphore counter is greater than 0. More... | |
void | post (void) |
Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0. More... | |
void | force_unlock_after_cancellation () |
Call it after a deferred cancellation to avoid deadlocks. More... | |
Static Public Member Functions | |
static void | setDebug (bool mode) |
Enable or disable deadlock debugging. More... | |
static Thread * | get (void) |
static void | setStack (size_t size=0) |
Set base stack limit before manual stack sizes have effect. More... | |
static void | sleep (timeout_t msec) |
A thread-safe sleep call. More... | |
static void | yield (void) |
Yields the current thread's CPU time slice to allow another thread to begin immediate execution. More... | |
static Throw | getException (void) |
Get exception mode of the current thread. More... | |
static void | setException (Throw mode) |
Set exception mode of the current thread. More... | |
static Cancel | enterCancel (void) |
This is used to help build wrapper functions in libraries around system calls that should behave as cancellation points but don't. More... | |
static void | exitCancel (Cancel cancel) |
This is used to restore a cancel block. More... | |
Protected Types | |
typedef struct ThreadQueue::_data | data_t |
Protected Member Functions | |
virtual void | final () |
A thread that is self terminating, either by invoking exit() or leaving it's run(), will have this method called. More... | |
virtual void | startQueue (void) |
Start of dequeing. More... | |
virtual void | stopQueue (void) |
End of dequeing, we expect the queue is empty for now. More... | |
virtual void | onTimer (void) |
A derivable method to call when the timout is expired. More... | |
virtual void | runQueue (void *data)=0 |
Virtual callback method to handle processing of a queued data items. More... | |
void | setName (const char *text) |
Set the name of the current thread. More... | |
virtual void | initial (void) |
The initial method is called by a newly created thread when it starts execution. More... | |
virtual void * | getExtended (void) |
Since getParent() and getThread() only refer to an object of the Thread "base" type, this virtual method can be replaced in a derived class with something that returns data specific to the derived class that can still be accessed through the pointer returned by getParent() and getThread(). More... | |
virtual void | notify (Thread *) |
When a thread terminates, it now sends a notification message to the parent thread which created it. More... | |
void | exit (void) |
Used to properly exit from a Thread derived run() or initial() method. More... | |
void | sync (void) |
Used to wait for a join or cancel, in place of explicit exit. More... | |
bool | testCancel (void) |
test a cancellation point for deferred thread cancellation. More... | |
void | setCancel (Cancel mode) |
Sets thread cancellation mode. More... | |
void | setSuspend (Suspend mode) |
Sets the thread's ability to be suspended from execution. More... | |
void | terminate (void) |
Used by another thread to terminate the current thread. More... | |
void | clrParent (void) |
clear parent thread relationship. More... | |
Protected Attributes | |
timeout_t | timeout |
bool | started |
data_t * | first |
data_t * | last |
String | name |
Private Member Functions | |
void | run (void) |
All threads execute by deriving the Run method of Thread. More... | |
Somewhat generic queue processing class to establish a producer consumer queue.
This may be used to buffer cdr records, or for other purposes where an in-memory queue is needed for rapid posting. This class is derived from Mutex and maintains a linked list. A thread is used to dequeue data and pass it to a callback method that is used in place of "run" for each item present on the queue. The conditional is used to signal the run thread when new data is posted.
This class was changed by Angelo Naselli to have a timeout on the queue
in memory data queue interface.
|
inherited |
How work cancellation.
|
protected |
|
inherited |
How work suspend.
|
inherited |
How to raise error.
|
inherited |
How work cancellation.
Enumerator | |
---|---|
cancelInitial |
used internally, do not use |
cancelDeferred |
exit thread on cancellation pointsuch as yield |
cancelImmediate |
exit befor cancellation |
cancelDisabled |
ignore cancellation |
cancelManual |
unimplemented (working in progress)
|
cancelDefault |
default you should use this for compatibility instead of deferred |
|
inherited |
|
inherited |
ThreadQueue::ThreadQueue | ( | const char * | id, |
int | pri, | ||
size_t | stack = 0 |
||
) |
Create instance of our queue and give it a process priority.
id | queue ID. |
pri | process priority. |
stack | stack size. |
|
virtual |
Destroy the queue.
|
inlineprotectedinherited |
|
inherited |
Start a new thread as "detached".
This is an alternative start() method that resolves some issues with later glibc implimentations which incorrectly impliment self-detach.
start | optional starting semaphore to alternately use. |
|
inlineinherited |
|
staticinherited |
This is used to help build wrapper functions in libraries around system calls that should behave as cancellation points but don't.
|
inherited |
Entering a Mutex locks the mutex for the current thread.
This also can be done using the ENTER_CRITICAL macro or by using the ++ operator on a mutex.
|
protectedinherited |
|
staticinherited |
This is used to restore a cancel block.
cancel | type that was saved. |
|
protectedvirtual |
A thread that is self terminating, either by invoking exit() or leaving it's run(), will have this method called.
It can be used to self delete the current object assuming the object was created with new on the heap rather than stack local, hence one may often see final defined as "delete this" in a derived thread class. A final method, while running, cannot be terminated or cancelled by another thread. Final is called for all cancellation type (even immediate).
You can safe delete thread ("delete this") class on final, but you should exit ASAP (or do not try to call CommonC++ methods...)
Reimplemented from Thread.
|
inherited |
Call it after a deferred cancellation to avoid deadlocks.
From PTHREAD_COND_TIMEDWAIT(3P): A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler.
|
staticinherited |
|
inlineinherited |
|
staticinherited |
Get exception mode of the current thread.
|
protectedvirtualinherited |
Since getParent() and getThread() only refer to an object of the Thread "base" type, this virtual method can be replaced in a derived class with something that returns data specific to the derived class that can still be accessed through the pointer returned by getParent() and getThread().
|
inherited |
Get system thread numeric identifier.
|
inlineinherited |
|
inlineinherited |
|
protectedvirtualinherited |
The initial method is called by a newly created thread when it starts execution.
This method is ran with deferred cancellation disabled by default. The Initial method is given a separate handler so that it can create temporary objects on it's own stack frame, rather than having objects created on run() that are only needed by startup and yet continue to consume stack space.
Reimplemented in TCPSession, and UnixSession.
|
inherited |
Check if this thread is detached.
|
inherited |
Verifies if the thread is still running or has already been terminated but not yet deleted.
|
inherited |
Tests to see if the current execution context is the same as the specified thread object.
|
inherited |
Blocking call which unlocks when thread terminates.
|
inlineinherited |
|
inherited |
Leaving a mutex frees that mutex for use by another thread.
If the mutex has been entered (invoked) multiple times (recursivily) by the same thread, then it will need to be exited the same number of instances before it is free for re-use. This operation can also be done using the LEAVE_CRITICAL macro or by the – operator on a mutex.
|
inlineinherited |
|
protectedvirtualinherited |
When a thread terminates, it now sends a notification message to the parent thread which created it.
The actual use of this notification is left to be defined in a derived class.
- | the thread that has terminated. |
|
protectedvirtual |
A derivable method to call when the timout is expired.
void ThreadQueue::post | ( | const void * | data, |
unsigned | len | ||
) |
Put some unspecified data into this queue.
A new qd structure is created and sized to contain a copy of the actual content.
data | pointer to data. |
len | size of data. |
|
inherited |
Posting to a semaphore increments its current value and releases the first thread waiting for the semaphore if it is currently at 0.
Interestingly, there is no support to increment a semaphore by any value greater than 1 to release multiple waiting threads in either pthread or the win32 API. Hence, if one wants to release a semaphore to enable multiple threads to execute, one must perform multiple post operations.
|
inherited |
Resumes execution of the selected thread.
|
privatevirtual |
|
protectedpure virtual |
Virtual callback method to handle processing of a queued data items.
After the item is processed, it is deleted from memory. We can call multiple instances of runQueue in order if multiple items are waiting.
data | item being dequed. |
|
protectedinherited |
Sets thread cancellation mode.
Threads can either be set immune to termination (cancelDisabled), can be set to terminate when reaching specific "thread cancellation points" (cancelDeferred) or immediately when Terminate is requested (cancelImmediate).
mode | for cancellation of the current thread. |
|
inlinestaticinherited |
|
staticinherited |
Set exception mode of the current thread.
|
protectedinherited |
Set the name of the current thread.
If the name is passed as NULL, then the default name is set (usually object pointer).
text | name to use. |
|
inlinestaticinherited |
|
protectedinherited |
Sets the thread's ability to be suspended from execution.
The thread may either have suspend enabled (suspendEnable) or disabled (suspendDisable).
mode | for suspend. |
void ThreadQueue::setTimer | ( | timeout_t | timeout | ) |
Set the queue timeout.
When the timer expires, the onTimer() method is called for the thread
timeout | timeout in milliseconds. |
|
staticinherited |
A thread-safe sleep call.
On most Posix systems, "sleep()" is implimented with SIGALRM making it unusable from multipe threads. Pthread libraries often define an alternate "sleep" handler such as usleep(), nanosleep(), or nap(), that is thread safe, and also offers a higher timer resolution.
msec | timeout in milliseconds. |
|
inherited |
When a new thread is created, it does not begin immediate execution.
This is because the derived class virtual tables are not properly loaded at the time the C++ object is created within the constructor itself, at least in some compiler/system combinations. The thread can either be told to wait for an external semaphore, or it can be started directly after the constructor completes by calling the start() method.
start | optional starting semaphore to alternately use. |
|
protectedvirtual |
Start of dequeing.
Maybe we need to connect a database or something, so we have a virtual...
|
protectedvirtual |
End of dequeing, we expect the queue is empty for now.
Maybe we need to disconnect a database or something, so we have another virtual.
|
inherited |
Suspends execution of the selected thread.
Pthreads do not normally support suspendable threads, so the behavior is simulated with signals. On systems such as Linux that define threads as processes, SIGSTOP and SIGCONT may be used.
|
protectedinherited |
Used to wait for a join or cancel, in place of explicit exit.
|
protectedinherited |
Used by another thread to terminate the current thread.
Termination actually occurs based on the current setCancel() mode. When the current thread does terminate, control is returned to the requesting thread. terminate() should always be called at the start of any destructor of a class derived from Thread to assure the remaining part of the destructor is called without the thread still executing.
|
inlineinherited |
|
protectedinherited |
test a cancellation point for deferred thread cancellation.
|
inherited |
Tries to lock the mutex for the current thread.
Behaves like enterMutex , except that it doesn't block the calling thread if the mutex is already locked by another thread.
|
inherited |
Wait is used to keep a thread held until the semaphore counter is greater than 0.
If the current thread is held, then another thread must increment the semaphore. Once the thread is accepted, the semaphore is automatically decremented, and the thread continues execution.
The pthread semaphore object does not support a timed "wait", and hence to maintain consistancy, neither the posix nor win32 source trees support "timed" semaphore objects.
timeout | period in milliseconds to wait |
|
staticinherited |
Yields the current thread's CPU time slice to allow another thread to begin immediate execution.