GNU CommonC++
thread.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction. Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License. This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++. If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way. To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
44 #ifndef CCXX_THREAD_H_
45 #define CCXX_THREAD_H_
46 
47 #include <cc++/config.h>
48 
49 #ifndef CCXX_STRING_H_
50 #include <cc++/string.h>
51 #endif
52 
53 #ifndef WIN32
54 #define CCXX_POSIX
55 #endif // !WIN32
56 
57 #include <ctime>
58 
59 #ifndef WIN32
60 #include <pthread.h>
61 #endif // !WIN32
62 
63 #undef CCXX_USE_WIN32_ATOMIC
64 #ifndef WIN32
65 #include <time.h>
66 #include <signal.h>
67 #include <unistd.h>
68 
69 #ifdef _THR_UNIXWARE
70 #undef PTHREAD_MUTEXTYPE_RECURSIVE
71 #endif
72 
73 typedef pthread_t cctid_t;
74 typedef unsigned long timeout_t;
75 
76 /*
77 #if defined(__CYGWIN32__)
78 __declspec(dllimport) long __stdcall InterlockedIncrement(long *);
79 __declspec(dllimport) long __stdcall InterlockedDecrement(long *);
80 __declspec(dllimport) long __stdcall InterlockedExchange(long *, long);
81 #define CCXX_USE_WIN32_ATOMIC 1
82 #endif
83 */
84 
85 #else // WIN32
86 typedef DWORD cctid_t;
87 typedef DWORD timeout_t;
88 
89 #define MAX_SEM_VALUE 1000000
90 #define CCXX_USE_WIN32_ATOMIC 1
91 
92 #endif // !WIN32
93 
94 #ifdef HAVE_GCC_CXX_BITS_ATOMIC
95 #include <ios>
96 #endif
97 
98 #ifdef CCXX_NAMESPACES
99 namespace ost {
100 #ifdef __BORLANDC__
101 # if __BORLANDC__ >= 0x0560
102 using std::time_t;
103 using std::tm;
104 # endif
105 #endif
106 #endif
107 
108 #ifdef HAVE_GCC_CXX_BITS_ATOMIC
109 using namespace __gnu_cxx;
110 #endif
111 
112 class __EXPORT Thread;
113 class __EXPORT ThreadKey;
114 
115 #define TIMEOUT_INF ~((timeout_t) 0)
116 
117 #define ENTER_CRITICAL enterMutex();
118 #define LEAVE_CRITICAL leaveMutex();
119 #define ENTER_DEFERRED setCancel(cancelDeferred);
120 #define LEAVE_DEFERRED setCancel(cancelImmediate);
121 
122 #ifndef WIN32
123 // These macros override common functions with thread-safe versions. In
124 // particular the common "libc" sleep() has problems since it normally
125 // uses SIGARLM (as actually defined by "posix"). The pthread_delay and
126 // usleep found in libpthread are gaurenteed not to use SIGALRM and offer
127 // higher resolution. psleep() is defined to call the old process sleep.
128 
129 #undef sleep
130 #define psleep(x) (sleep)(x)
131 
132 #ifdef signal
133 #undef signal
134 #endif
135 
136 #endif // !WIN32
137 
138 #undef Yield
139 
140 class __EXPORT Conditional;
141 class __EXPORT Event;
142 
187 {
188 private:
189  static bool _debug;
190  String _name;
191 #ifndef WIN32
192 #ifndef PTHREAD_MUTEXTYPE_RECURSIVE
193  int volatile _level;
194  Thread *volatile _tid;
195 #endif
196  /*
197  * Pthread mutex object. This is protected rather than private
198  * because some mixed mode pthread operations require a mutex as
199  * well as their primary pthread object. A good example of this
200  * is the Event class, as waiting on a conditional object must be
201  * associated with an accessable mutex. An alternative would be
202  * to make such classes "friend" classes of the Mutex.
203  */
204  pthread_mutex_t _mutex;
205 #else // WIN32
206 
207 # if defined(MUTEX_UNDERGROUND_WIN32_MUTEX) && defined(MUTEX_UNDERGROUND_WIN32_CRITICALSECTION)
208 # error "Can't determine underground for Mutex"
209 # endif
210 
211 #ifdef MUTEX_UNDERGROUND_WIN32_MUTEX
212  HANDLE _mutex;
213 #endif
214 #ifdef MUTEX_UNDERGROUND_WIN32_CRITICALSECTION
215  CRITICAL_SECTION _criticalSection;
216 #endif
217 
218 #endif // WIN32
219 
220 public:
226  Mutex(const char *name = NULL);
227 
233  virtual ~Mutex();
234 
240  static void setDebug(bool mode)
241  {_debug = mode;};
242 
248  inline void nameMutex(const char *name)
249  {_name = name;};
250 
258  void enterMutex(void);
259 
263  inline void enter(void)
264  {enterMutex();};
265 
269  inline void leave(void)
270  {leaveMutex();};
271 
277  inline bool test(void)
278  {return tryEnterMutex();};
279 
290  bool tryEnterMutex(void);
291 
302  void leaveMutex(void);
303 };
304 
329 {
330 private:
331  Mutex& mutex;
332 public:
338  MutexLock( Mutex& _mutex ) : mutex( _mutex )
339  { mutex.enterMutex(); }
340 
344  // this should be not-virtual
346  { mutex.leaveMutex(); }
347 };
348 
358 {
359 private:
360 #ifdef HAVE_PTHREAD_RWLOCK
361  pthread_rwlock_t _lock;
362 #else
363  Mutex mutex;
364 #endif
365 
366 public:
371 
375  virtual ~ThreadLock();
376 
380  void readLock(void);
381 
385  void writeLock(void);
386 
392  bool tryReadLock(void);
393 
399  bool tryWriteLock(void);
400 
404  void unlock(void);
405 };
406 
428 {
429 private:
430  ThreadLock& tl;
431 
432 public:
438  ReadLock( ThreadLock& _tl ) : tl( _tl )
439  { tl.readLock(); }
443  // this should be not-virtual
445  { tl.unlock(); }
446 };
447 
469 {
470 private:
471  ThreadLock& tl;
472 
473 public:
479  WriteLock( ThreadLock& _tl ) : tl( _tl )
480  { tl.writeLock(); }
484  // this should be not-virtual
486  { tl.unlock(); }
487 };
488 
489 
500 {
501 private:
502  volatile int counter;
503 
504 public:
510  MutexCounter(const char *id = NULL);
511 
519  MutexCounter(int initial, const char *id = NULL);
520 
523 };
524 
536 {
537 #ifndef CCXX_USE_WIN32_ATOMIC
538 private:
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;
545 // __gnu_cxx::_Atomic_word counter;
546 #elif defined(HAVE_ATOMIC)
547  atomic_t atomic;
548 #else
549  volatile int counter;
550  pthread_mutex_t _mutex;
551 #endif
552 
553 public:
558 
564  AtomicCounter(int value);
565 
567 
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);
576  operator int();
577 #else
578 private:
579  long atomic;
580 
581 public:
582  inline AtomicCounter()
583  {atomic = 0;};
584 
585  inline AtomicCounter(int value)
586  {atomic = value;};
587 
588  inline int operator++(void)
589  {return InterlockedIncrement(&atomic);};
590 
591  inline int operator--(void)
592  {return InterlockedDecrement(&atomic);};
593 
594  int operator+=(int change);
595 
596  int operator-=(int change);
597 
598  inline int operator+(int change)
599  {return atomic + change;};
600 
601  inline int operator-(int change)
602  {return atomic - change;};
603 
604  inline int operator=(int value)
605  {return InterlockedExchange(&atomic, value);};
606 
607  inline bool operator!(void)
608  {return (atomic == 0) ? true : false;};
609 
610  inline operator int()
611  {return atomic;};
612 #endif
613 };
614 
615 #ifndef WIN32
637 {
638 private:
639  pthread_cond_t _cond;
640  pthread_mutex_t _mutex;
641 
642 public:
648  Conditional(const char *id = NULL);
649 
653  virtual ~Conditional();
654 
660  void signal(bool broadcast);
661 
668  bool wait(timeout_t timer = 0, bool locked = false);
669 
676  void enterMutex(void);
677 
686  inline void lock(void)
687  {enterMutex();};
688 
699  bool tryEnterMutex(void);
700 
701  inline bool test(void)
702  {return tryEnterMutex();};
703 
709  void leaveMutex(void);
710 
711  inline void unlock(void)
712  {return leaveMutex();};
713 };
714 #endif
715 
734 {
735 private:
736 #ifndef WIN32
737  unsigned _count, _waiters;
738  pthread_mutex_t _mutex;
739  pthread_cond_t _cond;
740 #else
741  HANDLE semObject;
742 #endif // !WIN32
743 
744 public:
753  Semaphore(unsigned resource = 0);
754 
761  virtual ~Semaphore();
762 
778  bool wait(timeout_t timeout = 0);
779 
791  void post(void);
792 
793 #ifndef WIN32
803 
804 #endif // WIN32
805 
806  // FIXME: how implement getValue for posix compatibility ?
807  // not portable...
808 #if 0
814  int getValue(void);
815 #endif
816 };
817 
838 {
839 private:
840  Semaphore& sem;
841 
842 public:
846  SemaphoreLock( Semaphore& _sem ) : sem( _sem )
847  { sem.wait(); }
851  // this should be not-virtual
853  { sem.post(); }
854 };
855 
870 {
871 private:
872 #ifndef WIN32
873  pthread_mutex_t _mutex;
874  pthread_cond_t _cond;
875  bool _signaled;
876  int _count;
877 #else
878  HANDLE cond;
879 #endif
880 
881 public:
882  Event();
883 
884  virtual ~Event();
885 
892  void reset(void);
893 
897  void signal(void);
898 
907  bool wait(timeout_t timer);
908  bool wait(void);
909 };
910 
911 
1094 {
1095 public:
1099  typedef enum Throw {
1102  throwException
1103  } Throw;
1104 
1108  typedef enum Cancel {
1109  cancelInitial=0,
1110  cancelDeferred=1,
1113  cancelManual,
1115  cancelDefault=cancelDeferred
1117  } Cancel;
1118 
1122  typedef enum Suspend {
1124  suspendDisable
1125  } Suspend;
1126 
1127 #ifndef WIN32
1129 friend class PosixThread;
1130 #endif
1132 friend class DummyThread;
1133 private:
1134  friend class Cancellation;
1135  friend class postream_type;
1136  friend class Slog;
1137 
1138  Semaphore joinSem;
1139  static Thread* _main;
1140 
1141  Thread *_parent;
1142  Cancel _cancel;
1143  Semaphore *_start;
1144 
1145  // private data
1146  friend class ThreadImpl;
1147  class ThreadImpl* priv;
1148 
1149 public:
1150  static Thread *get(void);
1151 
1152 private:
1153 #ifdef WIN32
1154  static unsigned __stdcall Execute(Thread *th);
1155 #endif
1156 
1157  // close current thread, free all and call Notify
1158  void close();
1159 
1160 private:
1161  char _name[32];
1162  static size_t _autostack;
1163 
1164 #ifdef WIN32
1165  DWORD waitHandle(HANDLE obj, timeout_t timeout);
1166 #endif
1167 
1168 protected:
1176  void setName(const char *text);
1177 
1187  virtual void run(void) = 0;
1188 
1210  virtual void final(void);
1211 
1223  virtual void initial(void);
1224 
1234  virtual void* getExtended(void);
1235 
1243  virtual void notify(Thread*);
1244 
1250  void exit(void);
1251 
1255  void sync(void);
1256 
1260  bool testCancel(void);
1261 
1271  void setCancel(Cancel mode);
1272 
1280  void setSuspend(Suspend mode);
1281 
1290  void terminate(void);
1291 
1295  inline void clrParent(void)
1296  {_parent = NULL;};
1297 
1298 public:
1307  Thread(bool isMain);
1308 
1320  Thread(int pri = 0, size_t stack = 0);
1321 
1322 #ifndef WIN32
1331  Thread(const Thread &th);
1332 #endif
1333 
1340  virtual ~Thread();
1341 
1347  static void setStack(size_t size = 0)
1348  {_autostack = size;};
1349 
1359  static void sleep(timeout_t msec);
1360 
1365  static void yield(void);
1366 
1379  int start(Semaphore *start = 0);
1380 
1389  int detach(Semaphore *start = 0);
1390 
1397  inline Thread *getParent(void)
1398  {return _parent;};
1399 
1406  void suspend(void);
1407 
1411  void resume(void);
1412 
1419  inline Cancel getCancel(void)
1420  {return _cancel;};
1421 
1428  bool isRunning(void) const;
1429 
1435  bool isDetached(void) const;
1436 
1440  void join(void);
1441 
1448  bool isThread(void) const;
1449 
1455  cctid_t getId(void) const;
1456 
1463  const char *getName(void) const
1464  {return _name;};
1465 
1471  static Throw getException(void);
1472 
1478  static void setException(Throw mode);
1479 
1486  friend inline void operator++(Thread &th)
1487  {if (th._start) th._start->post();};
1488 
1489  friend inline void operator--(Thread &th)
1490  {if (th._start) th._start->wait();};
1491 
1492 #ifdef WIN32
1493  bool isCancelled() const;
1494 
1495  static DWORD waitThread(HANDLE hRef, timeout_t timeout);
1496 #endif
1497 
1505  static Cancel enterCancel(void);
1506 
1512  static void exitCancel(Cancel cancel);
1513 };
1514 
1525 {
1526 private:
1527  Thread::Cancel prior;
1528 
1529 public:
1532 };
1533 
1534 #if !defined(WIN32) && !defined(__MINGW32__)
1535 typedef int signo_t;
1536 
1537 class PosixThread: public Thread
1538 {
1539 private:
1540 #ifndef WIN32
1542  friend class ThreadImpl;
1543  friend class Thread;
1544 #endif
1545 #ifndef CCXX_SIG_THREAD_ALARM
1546  static PosixThread *_timer;
1547  static Mutex _arm;
1548 #endif
1549 
1550  time_t _alarm;
1551  static void signalThread(Thread* th,signo_t signo);
1552 protected:
1553 
1560  inline void signalParent(signo_t signo)
1561  { signalThread(_parent,signo); };
1562 
1569  inline void signalMain(signo_t signo)
1570  { signalThread(_main,signo);};
1571 
1576  virtual void onTimer(void);
1577 
1582  virtual void onHangup(void);
1583 
1588  virtual void onException(void);
1589 
1594  virtual void onDisconnect(void);
1595 
1600  virtual void onPolling(void);
1601 
1608  virtual void onSignal(int);
1609 
1622  void setTimer(timeout_t timer, bool periodic = false);
1623 
1630  timeout_t getTimer(void) const;
1631 
1637  void endTimer(void);
1638 
1639 #if defined(HAVE_SIGWAIT) || defined(HAVE_SIGWAIT2)
1646  void waitSignal(signo_t signo);
1647 #endif
1648 
1655  void setSignal(int signo, bool active);
1656 
1663  pthread_attr_t *getPthreadAttrPtr(void);
1664 
1669  pthread_t getPthreadId(void);
1670 
1671 public:
1672 
1673  PosixThread(int pri = 0, size_t stack = 0);
1674 
1680  inline void signalThread(int signo)
1681  {signalThread(this, signo);};
1682 
1689  static void sigInstall(int signo);
1690 };
1691 #endif
1692 
1708 {
1709 private:
1710 #ifndef WIN32
1711  pthread_key_t key;
1712  typedef void (*TDestruct)(void*);
1713  friend class ThreadImpl;
1714  ThreadKey(TDestruct destruct);
1715 #else
1716  DWORD key;
1717 #endif
1718 
1719 public:
1724 
1728  virtual ~ThreadKey();
1729 
1737  void *getKey(void);
1738 
1746  void setKey(void *);
1747 };
1748 
1760 {
1761 #ifndef WIN32
1762  struct timeval timer;
1763 #else
1764  DWORD timer;
1765 #endif
1766  bool active;
1767 
1768 public:
1776 
1785  void setTimer(timeout_t timeout = 0);
1786 
1796  void incTimer(timeout_t timeout);
1797 
1807  void decTimer(timeout_t timeout);
1808 
1813  void sleepTimer(void);
1814 
1820  void endTimer(void);
1821 
1833  timeout_t getTimer(void) const;
1834 
1844  timeout_t getElapsed(void) const;
1845 };
1846 
1847 
1848 
1849 // FIXME: not in win32 implementation
1850 #if !defined(WIN32)
1851 
1852 // FIXME: private declaration ???
1853 struct timespec *getTimeout(struct timespec *spec, timeout_t timeout);
1854 
1855 #if !defined(__CYGWIN32__) && !defined(__MINGW32__)
1856 void wait(signo_t signo);
1857 #endif
1858 
1859 #endif // !WIN32
1860 
1861 #ifdef USE_POLL
1862 
1870 class Poller
1871 {
1872 private:
1873  int nufds;
1874  pollfd *ufds;
1875 
1876 public:
1877  Poller();
1878 
1879  virtual ~Poller();
1880 
1888  pollfd *getList(int cnt);
1889 
1895  inline pollfd *getList(void)
1896  {return ufds;};
1897 };
1898 #endif
1899 
1900 inline Thread *getThread(void)
1901  {return Thread::get();}
1902 
1933 {
1934 private:
1935  static Mutex timeLock;
1936 
1937 protected:
1938  inline static void lock(void)
1939  {timeLock.enterMutex();}
1940 
1941  inline static void unlock(void)
1942  {timeLock.leaveMutex();}
1943 
1944 public:
1945  static time_t getTime(time_t *tloc = NULL);
1946  static time_t time(time_t *tloc)
1947  { return getTime(tloc); };
1948 
1949  static int getTimeOfDay(struct timeval *tp);
1950  static int gettimeofday(struct timeval *tp, struct timezone *)
1951  { return getTimeOfDay(tp); };
1952 
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); };
1956 
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);};
1960 };
1961 
1962 #ifndef HAVE_LOCALTIME_R
1963 
1964 inline struct tm *localtime_r(const time_t *t, struct tm *b)
1965  {return SysTime::getLocalTime(t, b);};
1966 inline char *ctime_r(const time_t *t, char *buf)
1967  {return ctime(t);};
1968 inline struct tm *gmtime_r(const time_t *t, struct tm *b) \
1969 {return SysTime::getGMTTime(t, b);};
1970 inline char *asctime_r(const struct tm *tm, char *b) \
1971  {return asctime(tm);};
1972 
1973 #endif
1974 
1975 #ifdef CCXX_NAMESPACES
1976 }
1977 #endif
1978 
1979 #endif
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 value)
int operator++(void)
int operator-(int change)
bool operator!(void)
int operator-=(int change)
int operator+(int change)
int operator--(void)
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.
bool wait(void)
virtual ~Event()
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
Definition: thread.h:1538
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.
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.
pthread_attr_t * getPthreadAttrPtr(void)
Access to pthread_attr structure this allows setting/modifying pthread attributes not covered in the ...
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 * getGMTTime(const time_t *clock, struct tm *result)
static struct tm * gmtime(const time_t *clock, struct tm *result)
Definition: thread.h:1958
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 * getLocalTime(const time_t *clock, struct tm *result)
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.
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.
friend void operator--(Thread &th)
Definition: thread.h:1489
static Thread * get(void)
Thread * getParent(void)
Gets the pointer to the Thread class which created the current thread object.
Definition: thread.h:1397
void setName(const char *text)
Set the name of the current thread.
virtual void * getExtended(void)
Since getParent() and getThread() only refer to an object of the Thread "base" type,...
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
const char * getName(void) const
Get the name string for this thread, to use in debug messages.
Definition: thread.h:1463
static void exitCancel(Cancel cancel)
This is used to restore a cancel block.
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 * getKey(void)
Get the value of the pointer for the thread specific data 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.
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
Definition: address.h:64
char * ctime_r(const time_t *t, char *buf)
Definition: thread.h:1966
class __EXPORT ThreadKey
Definition: thread.h:113
void wait(signo_t signo)
struct tm * localtime_r(const time_t *t, struct tm *b)
Definition: thread.h:1964
class __EXPORT Conditional
Definition: thread.h:140
int signo_t
Definition: thread.h:1535
struct tm * gmtime_r(const time_t *t, struct tm *b)
Definition: thread.h:1968
char * asctime_r(const struct tm *tm, char *b)
Definition: thread.h:1970
struct timespec * getTimeout(struct timespec *spec, timeout_t timeout)
Thread * getThread(void)
Definition: thread.h:1900
class __EXPORT Event
Definition: thread.h:141
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