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);
240 static void setDebug(
bool mode)
248 inline void nameMutex(
const char *name)
258 void enterMutex(
void);
263 inline void enter(
void)
269 inline void leave(
void)
277 inline bool test(
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);
686 inline void lock(
void)
699 bool tryEnterMutex(
void);
701 inline bool test(
void)
702 {
return tryEnterMutex();};
709 void leaveMutex(
void);
711 inline void unlock(
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);
1295 inline void clrParent(
void)
1320 Thread(
int pri = 0,
size_t stack = 0);
1347 static void setStack(
size_t size = 0)
1348 {_autostack = size;};
1365 static void yield(
void);
1428 bool isRunning(
void)
const;
1435 bool isDetached(
void)
const;
1448 bool isThread(
void)
const;
1463 const char *getName(
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);
1680 inline void signalThread(
int 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;
1938 inline static void lock(
void)
1941 inline static void unlock(
void)
1942 {timeLock.leaveMutex();}
1945 static time_t getTime(time_t *tloc = NULL);
1946 static time_t time(time_t *tloc)
1947 {
return getTime(tloc); };
1949 static int getTimeOfDay(
struct timeval *tp);
1950 static int gettimeofday(
struct timeval *tp,
struct timezone *)
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