44 #ifndef CCXX_THREAD_H_
45 #define CCXX_THREAD_H_
47 #include <cc++/config.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
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);
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)
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()
660 void signal(
bool broadcast);
676 void enterMutex(
void);
699 bool tryEnterMutex(
void);
702 {
return tryEnterMutex();};
709 void leaveMutex(
void);
712 {
return leaveMutex();};
802 void force_unlock_after_cancellation();
1115 cancelDefault=cancelDeferred
1132 friend class DummyThread;
1135 friend class postream_type;
1146 friend class ThreadImpl;
1150 static Thread *
get(void);
1154 static unsigned __stdcall Execute(
Thread *th);
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);
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)
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
static struct tm * getLocalTime(const time_t *clock, struct tm *result)
The SemaphoreLock class is used to protect a section of code through a semaphore so that only x insta...
void lock(void)
In the future we will use lock in place of enterMutex since the conditional composite is not a recurs...
Common C++ generic string class.
void post(void)
Posting to a semaphore increments its current value and releases the first thread waiting for the sem...
MutexLock(Mutex &_mutex)
Acquire the mutex.
struct tm * localtime_r(const time_t *t, struct tm *b)
A semaphore is generally used as a synchronization object between multiple threads or to protect a li...
void signalThread(int signo)
Delivers a Posix signal to the current thread.
The Mutex class is used to protect a section of code so that at any given time only a single thread c...
friend void operator++(Thread &th)
Signal the semaphore that the specified thread is waiting for before beginning execution.
Thread * getParent(void)
Gets the pointer to the Thread class which created the current thread object.
static struct tm * gmtime(const time_t *clock, struct tm *result)
Cancel
How work cancellation.
bool test(void)
Future abi will use enter/leave/test members.
This class allows the creation of a thread context unique "pointer" that can be set and retrieved and...
This is a generic and portable string class.
void clrParent(void)
clear parent thread relationship.
static Thread * get(void)
A conditional variable synchcronization object for one to one and one to many signal and control even...
A class to automatically set the thread cancellation mode of a member function.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
ReadLock(ThreadLock &_tl)
Wait for read access.
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...
void nameMutex(const char *name)
Enable setting of mutex name for deadlock debug.
static int gettimeofday(struct timeval *tp, struct timezone *)
struct tm * gmtime_r(const time_t *t, struct tm *b)
static time_t time(time_t *tloc)
void enter(void)
Future abi will use enter/leave/test members.
~ReadLock()
Post the semaphore automatically.
char * ctime_r(const time_t *t, char *buf)
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...
~SemaphoreLock()
Post the semaphore automatically.
The ThreadLock class impliments a thread rwlock for optimal reader performance on systems which have ...
static void setDebug(bool mode)
Enable or disable deadlock debugging.
Cancel getCancel(void)
Used to retrieve the cancellation mode in effect for the selected thread.
friend void operator--(Thread &th)
throw object that cause error (throw this)
~MutexLock()
Release the mutex automatically.
The slog class is used to stream messages to the system's logging facility (syslogd).
Every thread of execution in an application is created by instantiating an object of a class derived ...
The Event class implements a feature originally found in the WIN32 API; event notification.
struct timespec * getTimeout(struct timespec *spec, timeout_t timeout)
class __EXPORT Conditional
static void setStack(size_t size=0)
Set base stack limit before manual stack sizes have effect.
static struct tm * getGMTTime(const time_t *clock, struct tm *result)
continue without throwing error
void leave(void)
Future abi will use enter/leave/test members.
bool wait(timeout_t timeout=0)
Wait is used to keep a thread held until the semaphore counter is greater than 0. ...
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
This class is used to access non-reentrant date and time functions in the standard C library...
char * asctime_r(const struct tm *tm, char *b)
WriteLock(ThreadLock &_tl)
Wait for write access.
~WriteLock()
Post the semaphore automatically.
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
const char * getName(void) const
Get the name string for this thread, to use in debug messages.
static struct tm * locatime(const time_t *clock, struct tm *result)
The AtomicCounter class offers thread-safe manipulation of an integer counter.
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
static PosixThread * _timer