44 #ifndef CCXX_THREAD_H_
45 #define CCXX_THREAD_H_
49 #ifndef CCXX_STRING_H_
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
98 #ifdef CCXX_NAMESPACES
101 # if __BORLANDC__ >= 0x0560
108 #ifdef HAVE_GCC_CXX_BITS_ATOMIC
109 using 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;
226 Mutex(
const char *name = NULL);
258 void enterMutex(
void);
278 {
return tryEnterMutex();};
290 bool tryEnterMutex(
void);
302 void leaveMutex(
void);
339 { mutex.enterMutex(); }
346 { mutex.leaveMutex(); }
360 #ifdef HAVE_PTHREAD_RWLOCK
361 pthread_rwlock_t _lock;
385 void writeLock(
void);
392 bool tryReadLock(
void);
399 bool tryWriteLock(
void);
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;
568 int operator++(
void);
569 int operator--(
void);
570 int operator+=(
int change);
571 int operator-=(
int change);
572 int operator+(
int change);
573 int operator-(
int change);
574 int operator=(
int value);
575 bool operator!(
void);
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;
660 void signal(
bool broadcast);
676 void enterMutex(
void);
699 bool tryEnterMutex(
void);
702 {
return tryEnterMutex();};
709 void leaveMutex(
void);
712 {
return leaveMutex();};
737 unsigned _count, _waiters;
738 pthread_mutex_t _mutex;
739 pthread_cond_t _cond;
802 void force_unlock_after_cancellation();
873 pthread_mutex_t _mutex;
874 pthread_cond_t _cond;
1115 cancelDefault=cancelDeferred
1132 friend class DummyThread;
1135 friend class postream_type;
1146 friend class ThreadImpl;
1147 class ThreadImpl* priv;
1150 static Thread *
get(void);
1154 static unsigned __stdcall Execute(
Thread *th);
1162 static size_t _autostack;
1176 void setName(
const char *text);
1187 virtual void run(
void) = 0;
1210 virtual void final(void);
1223 virtual void initial(
void);
1234 virtual void* getExtended(
void);
1243 virtual void notify(
Thread*);
1260 bool testCancel(
void);
1271 void setCancel(
Cancel mode);
1280 void setSuspend(
Suspend mode);
1290 void terminate(
void);
1320 Thread(
int pri = 0,
size_t stack = 0);
1348 {_autostack = size;};
1365 static void yield(
void);
1428 bool isRunning(
void)
const;
1435 bool isDetached(
void)
const;
1448 bool isThread(
void)
const;
1471 static Throw getException(
void);
1478 static void setException(Throw mode);
1487 {
if (th._start) th._start->
post();};
1490 {
if (th._start) th._start->
wait();};
1493 bool isCancelled()
const;
1505 static Cancel enterCancel(
void);
1512 static void exitCancel(Cancel cancel);
1534 #if !defined(WIN32) && !defined(__MINGW32__)
1542 friend class ThreadImpl;
1545 #ifndef CCXX_SIG_THREAD_ALARM
1561 { signalThread(_parent,signo); };
1570 { signalThread(_main,signo);};
1576 virtual void onTimer(
void);
1582 virtual void onHangup(
void);
1588 virtual void onException(
void);
1594 virtual void onDisconnect(
void);
1600 virtual void onPolling(
void);
1608 virtual void onSignal(
int);
1622 void setTimer(
timeout_t timer,
bool periodic =
false);
1637 void endTimer(
void);
1639 #if defined(HAVE_SIGWAIT) || defined(HAVE_SIGWAIT2)
1646 void waitSignal(
signo_t signo);
1655 void setSignal(
int signo,
bool active);
1663 pthread_attr_t *getPthreadAttrPtr(
void);
1669 pthread_t getPthreadId(
void);
1681 {signalThread(
this, signo);};
1689 static void sigInstall(
int signo);
1712 typedef void (*TDestruct)(
void*);
1713 friend class ThreadImpl;
1746 void setKey(
void *);
1762 struct timeval timer;
1813 void sleepTimer(
void);
1820 void endTimer(
void);
1855 #if !defined(__CYGWIN32__) && !defined(__MINGW32__)
1888 pollfd *getList(
int cnt);
1895 inline pollfd *getList(
void)
1935 static Mutex timeLock;
1939 {timeLock.enterMutex();}
1942 {timeLock.leaveMutex();}
1945 static time_t getTime(time_t *tloc = NULL);
1947 {
return getTime(tloc); };
1949 static int getTimeOfDay(
struct timeval *tp);
1951 {
return getTimeOfDay(tp); };
1953 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
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
1968 inline struct tm *
gmtime_r(
const time_t *t,
struct tm *b) \
1971 {
return asctime(tm);};
1975 #ifdef CCXX_NAMESPACES
void leave(void)
Future abi will use enter/leave/test members.
Definition: thread.h:269
Suspend
How work suspend.
Definition: thread.h:1122
bool test(void)
Definition: thread.h:701
static struct tm * gmtime(const time_t *clock, struct tm *result)
Definition: thread.h:1958
const char * getName(void) const
Get the name string for this thread, to use in debug messages.
Definition: thread.h:1463
This is a generic and portable string class.
Definition: string.h:77
char * ctime_r(const time_t *t, char *buf)
Definition: thread.h:1966
Common C++ generic string class.
int signo_t
Definition: thread.h:1535
int HANDLE
Definition: serial.h:60
Thread * getThread(void)
Definition: thread.h:1900
class __EXPORT Event
Definition: thread.h:141
~WriteLock()
Post the semaphore automatically.
Definition: thread.h:485
static void setStack(size_t size=0)
Set base stack limit before manual stack sizes have effect.
Definition: thread.h:1347
WriteLock(ThreadLock &_tl)
Wait for write access.
Definition: thread.h:479
#define __EXPORT
Definition: config.h:980
exit befor cancellation
Definition: thread.h:1111
void enter(void)
Future abi will use enter/leave/test members.
Definition: thread.h:263
Cancel
How work cancellation.
Definition: thread.h:1108
static struct tm * locatime(const time_t *clock, struct tm *result)
Definition: thread.h:1954
~ReadLock()
Post the semaphore automatically.
Definition: thread.h:444
class __EXPORT Conditional
Definition: thread.h:140
This class is used to access non-reentrant date and time functions in the standard C library...
Definition: thread.h:1932
class __EXPORT Thread
Definition: thread.h:112
static int gettimeofday(struct timeval *tp, struct timezone *)
Definition: thread.h:1950
void clrParent(void)
clear parent thread relationship.
Definition: thread.h:1295
static void unlock(void)
Definition: thread.h:1941
bool wait(timeout_t timeout=0)
Wait is used to keep a thread held until the semaphore counter is greater than 0. ...
static Thread * get(void)
pthread_t cctid_t
Definition: thread.h:73
The AtomicCounter class offers thread-safe manipulation of an integer counter.
Definition: thread.h:535
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
continue without throwing error
Definition: thread.h:1100
unsigned long timeout_t
Definition: thread.h:74
The Event class implements a feature originally found in the WIN32 API; event notification.
Definition: thread.h:869
ReadLock(ThreadLock &_tl)
Wait for read access.
Definition: thread.h:438
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
Definition: thread.h:328
~SemaphoreLock()
Post the semaphore automatically.
Definition: thread.h:852
struct tm * localtime_r(const time_t *t, struct tm *b)
Definition: thread.h:1964
friend void operator--(Thread &th)
Definition: thread.h:1489
A class to automatically set the thread cancellation mode of a member function.
Definition: thread.h:1524
void post(void)
Posting to a semaphore increments its current value and releases the first thread waiting for the sem...
void unlock(void)
Definition: thread.h:711
throw object that cause error (throw this)
Definition: thread.h:1101
The slog class is used to stream messages to the system's logging facility (syslogd).
Definition: slog.h:104
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
Definition: thread.h:1759
class __EXPORT ThreadKey
Definition: thread.h:113
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
~MutexLock()
Release the mutex automatically.
Definition: thread.h:345
MutexLock(Mutex &_mutex)
Acquire the mutex.
Definition: thread.h:338
A conditional variable synchcronization object for one to one and one to many signal and control even...
Definition: thread.h:636
bool test(void)
Future abi will use enter/leave/test members.
Definition: thread.h:277
static time_t time(time_t *tloc)
Definition: thread.h:1946
Thread * getParent(void)
Gets the pointer to the Thread class which created the current thread object.
Definition: thread.h:1397
suspend enabled
Definition: thread.h:1123
static void lock(void)
Definition: thread.h:1938
static void setDebug(bool mode)
Enable or disable deadlock debugging.
Definition: thread.h:240
Throw
How to raise error.
Definition: thread.h:1099
This class allows the creation of a thread context unique "pointer" that can be set and retrieved and...
Definition: thread.h:1707
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:186
Every thread of execution in an application is created by instantiating an object of a class derived ...
Definition: thread.h:1093
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
Definition: thread.h:427
Definition: thread.h:1537
The SemaphoreLock class is used to protect a section of code through a semaphore so that only x insta...
Definition: thread.h:837
void nameMutex(const char *name)
Enable setting of mutex name for deadlock debug.
Definition: thread.h:248
struct tm * gmtime_r(const time_t *t, struct tm *b)
Definition: thread.h:1968
struct timespec * getTimeout(struct timespec *spec, timeout_t timeout)
static struct tm * getGMTTime(const time_t *clock, struct tm *result)
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
Definition: thread.h:499
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
ignore cancellation
Definition: thread.h:1112
Cancel getCancel(void)
Used to retrieve the cancellation mode in effect for the selected thread.
Definition: thread.h:1419
static struct tm * getLocalTime(const time_t *clock, struct tm *result)
The ThreadLock class impliments a thread rwlock for optimal reader performance on systems which have ...
Definition: thread.h:357
char * asctime_r(const struct tm *tm, char *b)
Definition: thread.h:1970
void signalThread(int signo)
Delivers a Posix signal to the current thread.
Definition: thread.h:1680
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
Definition: thread.h:468
A semaphore is generally used as a synchronization object between multiple threads or to protect a li...
Definition: thread.h:733
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
Definition: thread.h:846
friend void operator++(Thread &th)
Signal the semaphore that the specified thread is waiting for before beginning execution.
Definition: thread.h:1486