43 #ifndef CCXX_THREAD_H_
44 #define CCXX_THREAD_H_
58 #undef CCXX_USE_WIN32_ATOMIC
65 #undef PTHREAD_MUTEXTYPE_RECURSIVE
84 #define MAX_SEM_VALUE 1000000
85 #define CCXX_USE_WIN32_ATOMIC 1
89 #ifdef HAVE_GCC_CXX_BITS_ATOMIC
93 #ifdef CCXX_NAMESPACES
96 # if __BORLANDC__ >= 0x0560
103 #ifdef HAVE_GCC_CXX_BITS_ATOMIC
104 using namespace __gnu_cxx;
110 #define TIMEOUT_INF ~((timeout_t) 0)
112 #define ENTER_CRITICAL enterMutex();
113 #define LEAVE_CRITICAL leaveMutex();
114 #define ENTER_DEFERRED setCancel(cancelDeferred);
115 #define LEAVE_DEFERRED setCancel(cancelImmediate);
125 #define psleep(x) (sleep)(x)
187 #ifndef PTHREAD_MUTEXTYPE_RECURSIVE
199 pthread_mutex_t _mutex;
202 # if defined(MUTEX_UNDERGROUND_WIN32_MUTEX) && defined(MUTEX_UNDERGROUND_WIN32_CRITICALSECTION)
203 # error "Can't determine underground for Mutex"
206 #ifdef MUTEX_UNDERGROUND_WIN32_MUTEX
209 #ifdef MUTEX_UNDERGROUND_WIN32_CRITICALSECTION
210 CRITICAL_SECTION _criticalSection;
221 Mutex(
const char *name = NULL);
235 static void setDebug(
bool mode)
243 inline void nameMutex(
const char *name)
253 void enterMutex(
void);
258 inline void enter(
void)
264 inline void leave(
void)
272 inline bool test(
void)
273 {
return tryEnterMutex();};
285 bool tryEnterMutex(
void);
297 void leaveMutex(
void);
334 { mutex.enterMutex(); }
341 { mutex.leaveMutex(); }
355 #ifdef HAVE_PTHREAD_RWLOCK
356 pthread_rwlock_t _lock;
380 void writeLock(
void);
387 bool tryReadLock(
void);
394 bool tryWriteLock(
void);
497 volatile int counter;
532 #ifndef CCXX_USE_WIN32_ATOMIC
534 #if defined(HAVE_ATOMIC_AIX)
535 volatile int counter;
536 #elif defined(HAVE_GCC_BITS_ATOMIC)
537 volatile _Atomic_word counter;
538 #elif defined(HAVE_GCC_CXX_BITS_ATOMIC)
539 volatile _Atomic_word counter;
541 #elif defined(HAVE_ATOMIC)
544 volatile int counter;
545 pthread_mutex_t _mutex;
563 int operator++(
void);
564 int operator--(
void);
565 int operator+=(
int change);
566 int operator-=(
int change);
567 int operator+(
int change);
568 int operator-(
int change);
569 int operator=(
int value);
570 bool operator!(
void);
583 inline int operator++(
void)
584 {
return InterlockedIncrement(&atomic);};
586 inline int operator--(
void)
587 {
return InterlockedDecrement(&atomic);};
589 int operator+=(
int change);
591 int operator-=(
int change);
593 inline int operator+(
int change)
594 {
return atomic + change;};
596 inline int operator-(
int change)
597 {
return atomic - change;};
599 inline int operator=(
int value)
600 {
return InterlockedExchange(&atomic, value);};
602 inline bool operator!(
void)
603 {
return (atomic == 0) ?
true :
false;};
605 inline operator int()
634 pthread_cond_t _cond;
635 pthread_mutex_t _mutex;
655 void signal(
bool broadcast);
671 void enterMutex(
void);
681 inline void lock(
void)
694 bool tryEnterMutex(
void);
696 inline bool test(
void)
697 {
return tryEnterMutex();};
704 void leaveMutex(
void);
706 inline void unlock(
void)
707 {
return leaveMutex();};
732 unsigned _count, _waiters;
733 pthread_mutex_t _mutex;
734 pthread_cond_t _cond;
856 pthread_mutex_t _mutex;
857 pthread_cond_t _cond;
1098 cancelDefault=cancelDeferred
1115 friend class DummyThread;
1118 friend class postream_type;
1129 friend class ThreadImpl;
1130 class ThreadImpl* priv;
1133 static Thread *
get(void);
1137 static unsigned __stdcall Execute(
Thread *th);
1145 static size_t _autostack;
1159 void setName(
const char *text);
1170 virtual void run(
void) = 0;
1193 virtual void final(void);
1206 virtual void initial(
void);
1217 virtual void* getExtended(
void);
1226 virtual void notify(
Thread*);
1243 bool testCancel(
void);
1254 void setCancel(
Cancel mode);
1263 void setSuspend(
Suspend mode);
1273 void terminate(
void);
1278 inline void clrParent(
void)
1303 Thread(
int pri = 0,
size_t stack = 0);
1330 static void setStack(
size_t size = 0)
1331 {_autostack = size;};
1348 static void yield(
void);
1411 bool isRunning(
void)
const;
1418 bool isDetached(
void)
const;
1431 bool isThread(
void)
const;
1446 const char *getName(
void)
const
1454 static Throw getException(
void);
1461 static void setException(Throw mode);
1470 {
if (th._start) th._start->
post();};
1473 {
if (th._start) th._start->
wait();};
1476 bool isCancelled()
const;
1488 static Cancel enterCancel(
void);
1495 static void exitCancel(Cancel cancel);
1517 #if !defined(WIN32) && !defined(__MINGW32__)
1525 friend class ThreadImpl;
1528 #ifndef CCXX_SIG_THREAD_ALARM
1544 { signalThread(_parent,signo); };
1553 { signalThread(_main,signo);};
1559 virtual void onTimer(
void);
1565 virtual void onHangup(
void);
1571 virtual void onException(
void);
1577 virtual void onDisconnect(
void);
1583 virtual void onPolling(
void);
1591 virtual void onSignal(
int);
1605 void setTimer(
timeout_t timer,
bool periodic =
false);
1620 void endTimer(
void);
1622 #if defined(HAVE_SIGWAIT) || defined(HAVE_SIGWAIT2)
1629 void waitSignal(
signo_t signo);
1638 void setSignal(
int signo,
bool active);
1646 pthread_attr_t *getPthreadAttrPtr(
void);
1652 pthread_t getPthreadId(
void);
1663 inline void signalThread(
int signo)
1672 static void sigInstall(
int signo);
1695 typedef void (*TDestruct)(
void*);
1696 friend class ThreadImpl;
1729 void setKey(
void *);
1745 struct timeval timer;
1796 void sleepTimer(
void);
1803 void endTimer(
void);
1838 #if !defined(__CYGWIN32__) && !defined(__MINGW32__)
1871 pollfd *getList(
int cnt);
1878 inline pollfd *getList(
void)
1918 static Mutex timeLock;
1921 inline static void lock(
void)
1924 inline static void unlock(
void)
1925 {timeLock.leaveMutex();}
1928 static time_t getTime(time_t *tloc = NULL);
1929 static time_t time(time_t *tloc)
1930 {
return getTime(tloc); };
1932 static int getTimeOfDay(
struct timeval *tp);
1933 static int gettimeofday(
struct timeval *tp,
struct timezone *)
1934 {
return getTimeOfDay(tp); };
1936 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
1937 static struct tm *locatime(
const time_t *clock,
struct tm *result)
1938 {
return getLocalTime(clock, result); };
1940 static struct tm *getGMTTime(
const time_t *clock,
struct tm *result);
1941 static struct tm *gmtime(
const time_t *clock,
struct tm *result)
1942 {
return getGMTTime(clock, result);};
1945 #ifndef HAVE_LOCALTIME_R
1951 inline struct tm *
gmtime_r(
const time_t *t,
struct tm *b) \
1954 {
return asctime(tm);};
1958 #ifdef CCXX_NAMESPACES