63#undef CCXX_USE_WIN32_ATOMIC
70#undef PTHREAD_MUTEXTYPE_RECURSIVE
89#define MAX_SEM_VALUE 1000000
90#define CCXX_USE_WIN32_ATOMIC 1
94#ifdef HAVE_GCC_CXX_BITS_ATOMIC
101# if __BORLANDC__ >= 0x0560
108#ifdef HAVE_GCC_CXX_BITS_ATOMIC
109using namespace __gnu_cxx;
115#define TIMEOUT_INF ~((timeout_t) 0)
117#define ENTER_CRITICAL enterMutex();
118#define LEAVE_CRITICAL leaveMutex();
119#define ENTER_DEFERRED setCancel(cancelDeferred);
120#define LEAVE_DEFERRED setCancel(cancelImmediate);
130#define psleep(x) (sleep)(x)
192#ifndef PTHREAD_MUTEXTYPE_RECURSIVE
204 pthread_mutex_t _mutex;
207# if defined(MUTEX_UNDERGROUND_WIN32_MUTEX) && defined(MUTEX_UNDERGROUND_WIN32_CRITICALSECTION)
208# error "Can't determine underground for Mutex"
211#ifdef MUTEX_UNDERGROUND_WIN32_MUTEX
214#ifdef MUTEX_UNDERGROUND_WIN32_CRITICALSECTION
215 CRITICAL_SECTION _criticalSection;
278 {
return tryEnterMutex();};
360#ifdef HAVE_PTHREAD_RWLOCK
361 pthread_rwlock_t _lock;
502 volatile int counter;
537#ifndef CCXX_USE_WIN32_ATOMIC
539#if defined(HAVE_ATOMIC_AIX)
540 volatile int counter;
541#elif defined(HAVE_GCC_BITS_ATOMIC)
542 volatile _Atomic_word counter;
543#elif defined(HAVE_GCC_CXX_BITS_ATOMIC)
544 volatile _Atomic_word counter;
546#elif defined(HAVE_ATOMIC)
549 volatile int counter;
550 pthread_mutex_t _mutex;
588 inline int operator++(
void)
589 {
return InterlockedIncrement(&atomic);};
591 inline int operator--(
void)
592 {
return InterlockedDecrement(&atomic);};
594 int operator+=(
int change);
596 int operator-=(
int change);
598 inline int operator+(
int change)
599 {
return atomic + change;};
601 inline int operator-(
int change)
602 {
return atomic - change;};
604 inline int operator=(
int value)
605 {
return InterlockedExchange(&atomic, value);};
607 inline bool operator!(
void)
608 {
return (atomic == 0) ? true :
false;};
610 inline operator int()
639 pthread_cond_t _cond;
640 pthread_mutex_t _mutex;
702 {
return tryEnterMutex();};
712 {
return leaveMutex();};
737 unsigned _count, _waiters;
738 pthread_mutex_t _mutex;
739 pthread_cond_t _cond;
873 pthread_mutex_t _mutex;
874 pthread_cond_t _cond;
1115 cancelDefault=cancelDeferred
1132friend class DummyThread;
1135 friend class postream_type;
1146 friend class ThreadImpl;
1147 class ThreadImpl* priv;
1154 static unsigned __stdcall Execute(
Thread *th);
1162 static size_t _autostack;
1210 virtual void final(void);
1348 {_autostack = size;};
1487 {
if (th._start) th._start->
post();};
1490 {
if (th._start) th._start->
wait();};
1493 bool isCancelled()
const;
1534#if !defined(WIN32) && !defined(__MINGW32__)
1545#ifndef CCXX_SIG_THREAD_ALARM
1561 { signalThread(_parent,signo); };
1570 { signalThread(_main,signo);};
1639#if defined(HAVE_SIGWAIT) || defined(HAVE_SIGWAIT2)
1646 void waitSignal(
signo_t signo);
1681 {signalThread(
this, signo);};
1712 typedef void (*TDestruct)(
void*);
1713 friend class ThreadImpl;
1762 struct timeval timer;
1855#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
1888 pollfd *getList(
int cnt);
1895 inline pollfd *getList(
void)
1935 static Mutex timeLock;
1947 {
return getTime(tloc); };
1951 {
return getTimeOfDay(tp); };
1954 static struct tm *
locatime(
const time_t *clock,
struct tm *result)
1955 {
return getLocalTime(clock, result); };
1957 static struct tm *
getGMTTime(
const time_t *clock,
struct tm *result);
1958 static struct tm *
gmtime(
const time_t *clock,
struct tm *result)
1959 {
return getGMTTime(clock, result);};
1962#ifndef HAVE_LOCALTIME_R
1968inline struct tm *
gmtime_r(
const time_t *t,
struct tm *b) \
1971 {
return asctime(tm);};
1975#ifdef CCXX_NAMESPACES
The AtomicCounter class offers thread-safe manipulation of an integer counter.
Definition thread.h:536
AtomicCounter(int value)
Initialize an atomic counter to a known value.
int operator-(int change)
int operator-=(int change)
int operator+(int change)
int operator+=(int change)
AtomicCounter()
Initialize an atomic counter to 0.
A class to automatically set the thread cancellation mode of a member function.
Definition thread.h:1525
Cancellation(Thread::Cancel cancel)
A conditional variable synchcronization object for one to one and one to many signal and control even...
Definition thread.h:637
void lock(void)
In the future we will use lock in place of enterMutex since the conditional composite is not a recurs...
Definition thread.h:686
bool wait(timeout_t timer=0, bool locked=false)
Wait to be signaled from another thread.
bool test(void)
Definition thread.h:701
Conditional(const char *id=NULL)
Create an instance of a conditional.
void signal(bool broadcast)
Signal a conditional object and a waiting threads.
void unlock(void)
Definition thread.h:711
virtual ~Conditional()
Destroy the conditional.
void leaveMutex(void)
Leaving a mutex frees that mutex for use by another thread.
bool tryEnterMutex(void)
Tries to lock the conditional for the current thread.
void enterMutex(void)
Locks the conditional's mutex for this thread.
The Event class implements a feature originally found in the WIN32 API; event notification.
Definition thread.h:870
void signal(void)
Signal the event for the waiting thread.
void reset(void)
Once signaled, the Event class must be "reset" before responding to a new signal.
bool wait(timeout_t timer)
Wait either for the event to be signaled by another thread or for the specified timeout duration.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
Definition thread.h:500
friend __EXPORT int operator--(MutexCounter &mc)
friend __EXPORT int operator++(MutexCounter &mc)
MutexCounter(int initial, const char *id=NULL)
Create and optionally name a mutex protected counter with an initial value.
MutexCounter(const char *id=NULL)
Create and optionally name a mutex protected counter.
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
Definition thread.h:187
Mutex(const char *name=NULL)
The mutex is always initialized as a recursive entity.
void leaveMutex(void)
Leaving a mutex frees that mutex for use by another thread.
virtual ~Mutex()
Destroying the mutex removes any system resources associated with it.
static void setDebug(bool mode)
Enable or disable deadlock debugging.
Definition thread.h:240
bool tryEnterMutex(void)
Tries to lock the mutex for the current thread.
void leave(void)
Future abi will use enter/leave/test members.
Definition thread.h:269
void nameMutex(const char *name)
Enable setting of mutex name for deadlock debug.
Definition thread.h:248
bool test(void)
Future abi will use enter/leave/test members.
Definition thread.h:277
void enterMutex(void)
Entering a Mutex locks the mutex for the current thread.
void enter(void)
Future abi will use enter/leave/test members.
Definition thread.h:263
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
Definition thread.h:329
~MutexLock()
Release the mutex automatically.
Definition thread.h:345
MutexLock(Mutex &_mutex)
Acquire the mutex.
Definition thread.h:338
virtual void onDisconnect(void)
A derived method to call when a SIGPIPE is being delivered to a specific thread.
void signalParent(signo_t signo)
In the Posix version of Common C++, this can be used to send a signal into the parent thread of the c...
Definition thread.h:1560
virtual void onSignal(int)
A derivable method to call for delivering a signal event to a specified thread.
static void sigInstall(int signo)
Install a signal handler for use by threads and the OnSignal() event notification handler.
virtual void onPolling(void)
A derived method to handle asynchronous I/O requests delivered to the specified thread.
pthread_attr_t * getPthreadAttrPtr(void)
Access to pthread_attr structure this allows setting/modifying pthread attributes not covered in the ...
virtual void onTimer(void)
A derivable method to call when a SIGALRM is being delivered to a specific thread.
timeout_t getTimer(void) const
Gets the time remaining for the current threads timer before it expires.
void setSignal(int signo, bool active)
Used to enable or disable a signal within the current thread.
virtual void onHangup(void)
A derived method to handle hangup events being delivered to a specific thread.
void signalMain(signo_t signo)
In the Posix version of Common C++, this can be used to send a signal into the main application threa...
Definition thread.h:1569
void setTimer(timeout_t timer, bool periodic=false)
Used to specify a timeout event that can be delivered to the current thread via SIGALRM.
virtual void onException(void)
A derived method to call when a SIGABRT is being delivered to a specific thread.
friend class ThreadImpl
Definition thread.h:1542
void endTimer(void)
Terminates the timer before the timeout period has expired.
PosixThread(int pri=0, size_t stack=0)
pthread_t getPthreadId(void)
Get pthread_t of underlying posix thread (useful for debugging/logging)
void signalThread(int signo)
Delivers a Posix signal to the current thread.
Definition thread.h:1680
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
Definition thread.h:428
ReadLock(ThreadLock &_tl)
Wait for read access.
Definition thread.h:438
~ReadLock()
Post the semaphore automatically.
Definition thread.h:444
A semaphore is generally used as a synchronization object between multiple threads or to protect a li...
Definition thread.h:734
Semaphore(unsigned resource=0)
The initial value of the semaphore can be specified.
virtual ~Semaphore()
Destroying a semaphore also removes any system resources associated with it.
bool wait(timeout_t timeout=0)
Wait is used to keep a thread held until the semaphore counter is greater than 0.
void force_unlock_after_cancellation()
Call it after a deferred cancellation to avoid deadlocks.
void post(void)
Posting to a semaphore increments its current value and releases the first thread waiting for the sem...
The SemaphoreLock class is used to protect a section of code through a semaphore so that only x insta...
Definition thread.h:838
~SemaphoreLock()
Post the semaphore automatically.
Definition thread.h:852
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
Definition thread.h:846
The slog class is used to stream messages to the system's logging facility (syslogd).
Definition slog.h:105
This is a generic and portable string class.
Definition string.h:81
This class is used to access non-reentrant date and time functions in the standard C library.
Definition thread.h:1933
static void unlock(void)
Definition thread.h:1941
static struct tm * getLocalTime(const time_t *clock, struct tm *result)
static struct tm * getGMTTime(const time_t *clock, struct tm *result)
static int getTimeOfDay(struct timeval *tp)
static void lock(void)
Definition thread.h:1938
static time_t getTime(time_t *tloc=NULL)
static struct tm * gmtime(const time_t *clock, struct tm *result)
Definition thread.h:1958
static time_t time(time_t *tloc)
Definition thread.h:1946
static struct tm * locatime(const time_t *clock, struct tm *result)
Definition thread.h:1954
static int gettimeofday(struct timeval *tp, struct timezone *)
Definition thread.h:1950
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition thread.h:1094
static Throw getException(void)
Get exception mode of the current thread.
Thread(int pri=0, size_t stack=0)
When a thread object is contructed, a new thread of execution context is created.
void resume(void)
Resumes execution of the selected thread.
Thread(const Thread &th)
A thread of execution can also be specified by cloning an existing thread.
cctid_t getId(void) const
Get system thread numeric identifier.
void sync(void)
Used to wait for a join or cancel, in place of explicit exit.
void join(void)
Blocking call which unlocks when thread terminates.
Cancel
How work cancellation.
Definition thread.h:1108
@ cancelImmediate
exit befor cancellation
Definition thread.h:1111
@ cancelDisabled
ignore cancellation
Definition thread.h:1112
friend void operator++(Thread &th)
Signal the semaphore that the specified thread is waiting for before beginning execution.
Definition thread.h:1486
int start(Semaphore *start=0)
When a new thread is created, it does not begin immediate execution.
virtual void initial(void)
The initial method is called by a newly created thread when it starts execution.
Cancel getCancel(void)
Used to retrieve the cancellation mode in effect for the selected thread.
Definition thread.h:1419
virtual void notify(Thread *)
When a thread terminates, it now sends a notification message to the parent thread which created it.
static void yield(void)
Yields the current thread's CPU time slice to allow another thread to begin immediate execution.
void setCancel(Cancel mode)
Sets thread cancellation mode.
static Cancel enterCancel(void)
This is used to help build wrapper functions in libraries around system calls that should behave as c...
bool isRunning(void) const
Verifies if the thread is still running or has already been terminated but not yet deleted.
void clrParent(void)
clear parent thread relationship.
Definition thread.h:1295
void suspend(void)
Suspends execution of the selected thread.
Thread * getParent(void)
Gets the pointer to the Thread class which created the current thread object.
Definition thread.h:1397
static void sleep(timeout_t msec)
A thread-safe sleep call.
Thread(bool isMain)
This is actually a special constructor that is used to create a thread "object" for the current execu...
bool isThread(void) const
Tests to see if the current execution context is the same as the specified thread object.
const char * getName(void) const
Get the name string for this thread, to use in debug messages.
Definition thread.h:1463
virtual void * getExtended(void)
Since getParent() and getThread() only refer to an object of the Thread "base" type,...
friend void operator--(Thread &th)
Definition thread.h:1489
void setName(const char *text)
Set the name of the current thread.
bool isDetached(void) const
Check if this thread is detached.
Throw
How to raise error.
Definition thread.h:1099
@ throwNothing
continue without throwing error
Definition thread.h:1100
@ throwObject
throw object that cause error (throw this)
Definition thread.h:1101
static void exitCancel(Cancel cancel)
This is used to restore a cancel block.
static Thread * get(void)
int detach(Semaphore *start=0)
Start a new thread as "detached".
static void setStack(size_t size=0)
Set base stack limit before manual stack sizes have effect.
Definition thread.h:1347
void exit(void)
Used to properly exit from a Thread derived run() or initial() method.
void setSuspend(Suspend mode)
Sets the thread's ability to be suspended from execution.
virtual ~Thread()
The thread destructor should clear up any resources that have been allocated by the thread.
virtual void run(void)=0
All threads execute by deriving the Run method of Thread.
void terminate(void)
Used by another thread to terminate the current thread.
static void setException(Throw mode)
Set exception mode of the current thread.
bool testCancel(void)
test a cancellation point for deferred thread cancellation.
Suspend
How work suspend.
Definition thread.h:1122
@ suspendEnable
suspend enabled
Definition thread.h:1123
This class allows the creation of a thread context unique "pointer" that can be set and retrieved and...
Definition thread.h:1708
ThreadKey()
Create a unique thread specific container.
void setKey(void *)
Set the value of the pointer for the current thread specific execution context.
virtual ~ThreadKey()
Destroy a thread specific container and any contents reserved.
void * getKey(void)
Get the value of the pointer for the thread specific data container.
The ThreadLock class impliments a thread rwlock for optimal reader performance on systems which have ...
Definition thread.h:358
bool tryReadLock(void)
Attempt read lock for current object.
void readLock(void)
Aquire a read lock for the current object.
ThreadLock()
Create a process shared thread lock object.
void writeLock(void)
Aquire a write lock for the current object.
void unlock(void)
Release any held locks.
virtual ~ThreadLock()
Destroy a process shared thread lock object.
bool tryWriteLock(void)
Attempt write lock for current object.
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
Definition thread.h:1760
void sleepTimer(void)
Sleep until the current timer expires.
void setTimer(timeout_t timeout=0)
Set a new start time for the object based on when this call is made and optionally activate the timer...
timeout_t getElapsed(void) const
This is used to determine how much time has elapsed since a timer port setTimer benchmark time was in...
void decTimer(timeout_t timeout)
Adjust a timeout based on the current time reference value either from object creation or the last se...
void endTimer(void)
This is used to "disable" the service thread from expiring the timer object.
timeout_t getTimer(void) const
This is used by service threads to determine how much time remains before the timer expires based on ...
void incTimer(timeout_t timeout)
Set a timeout based on the current time reference value either from object creation or the last setTi...
TimerPort()
Create a timer, mark it as inactive, and set the initial "start" time to the creation time of the tim...
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
Definition thread.h:469
~WriteLock()
Post the semaphore automatically.
Definition thread.h:485
WriteLock(ThreadLock &_tl)
Wait for write access.
Definition thread.h:479
#define __EXPORT
Definition config.h:1045
char * ctime_r(const time_t *t, char *buf)
Definition thread.h:1966
struct tm * gmtime_r(const time_t *t, struct tm *b)
Definition thread.h:1968
int signo_t
Definition thread.h:1535
Thread * getThread(void)
Definition thread.h:1900
char * asctime_r(const struct tm *tm, char *b)
Definition thread.h:1970
struct tm * localtime_r(const time_t *t, struct tm *b)
Definition thread.h:1964
struct timespec * getTimeout(struct timespec *spec, timeout_t timeout)
int HANDLE
Definition serial.h:60
Common C++ generic string class.
pthread_t cctid_t
Definition thread.h:73
unsigned long timeout_t
Definition thread.h:74