UCommon
thread.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
54 #ifndef _UCOMMON_THREAD_H_
55 #define _UCOMMON_THREAD_H_
56 
57 #ifndef _UCOMMON_CPR_H_
58 #include <ucommon/cpr.h>
59 #endif
60 
61 #ifndef _UCOMMON_ACCESS_H_
62 #include <ucommon/access.h>
63 #endif
64 
65 #ifndef _UCOMMON_TIMERS_H_
66 #include <ucommon/timers.h>
67 #endif
68 
69 #ifndef _UCOMMON_MEMORY_H_
70 #include <ucommon/memory.h>
71 #endif
72 
73 namespace ucommon {
74 
75 class SharedPointer;
76 
87 class __EXPORT Conditional
88 {
89 private:
90  friend class ConditionalAccess;
91 
92 #if defined(_MSTHREADS_)
93  mutable CRITICAL_SECTION mutex;
94  mutable CONDITION_VARIABLE cond;
95 #else
96 #ifndef __PTH__
97  class __LOCAL attribute
98  {
99  public:
100  pthread_condattr_t attr;
101  attribute();
102  };
103 
104  __LOCAL static attribute attr;
105 #endif
106 
107  mutable pthread_cond_t cond;
108  mutable pthread_mutex_t mutex;
109 #endif
110 
111 protected:
112  friend class TimedEvent;
113 
119  bool wait(timeout_t timeout);
120 
126  bool wait(struct timespec *timeout);
127 
128 #ifdef _MSTHREADS_
129  inline void lock(void)
130  {EnterCriticalSection(&mutex);};
131 
132  inline void unlock(void)
133  {LeaveCriticalSection(&mutex);};
134 
135  void wait(void);
136  void signal(void);
137  void broadcast(void);
138 
139 #else
140 
143  inline void lock(void)
144  {pthread_mutex_lock(&mutex);}
145 
149  inline void unlock(void)
150  {pthread_mutex_unlock(&mutex);}
151 
155  inline void wait(void)
156  {pthread_cond_wait(&cond, &mutex);}
157 
161  inline void signal(void)
162  {pthread_cond_signal(&cond);}
163 
167  inline void broadcast(void)
168  {pthread_cond_broadcast(&cond);}
169 #endif
170 
174  Conditional();
175 
179  ~Conditional();
180 
181  friend class autolock;
182 
183 public:
184  class __EXPORT autolock
185  {
186  private:
187 #ifdef _MSTHREADS_
188  CRITICAL_SECTION *mutex;
189 #else
190  pthread_mutex_t *mutex;
191 #endif
192 
193  public:
194  inline autolock(const Conditional* object) {
195  mutex = &object->mutex;
196 #ifdef _MSTHREADS_
197  EnterCriticalSection(mutex);
198 #else
199  pthread_mutex_lock(mutex);
200 #endif
201  }
202 
203  inline ~autolock() {
204 #ifdef _MSTHREADS_
205  LeaveCriticalSection(mutex);
206 #else
207  pthread_mutex_unlock(mutex);
208 #endif
209  }
210  };
211 
212 #if !defined(_MSTHREADS_) && !defined(__PTH__)
213 
218  static inline pthread_condattr_t *initializer(void)
219  {return &attr.attr;}
220 #endif
221 
228  static void set(struct timespec *hires, timeout_t timeout);
229 };
230 
238 class __EXPORT ConditionalAccess : private Conditional
239 {
240 protected:
241 #if defined _MSTHREADS_
242  CONDITION_VARIABLE bcast;
243 #else
244  mutable pthread_cond_t bcast;
245 #endif
246 
247  unsigned pending, waiting, sharing;
248 
254  bool waitSignal(timeout_t timeout);
255 
261  bool waitBroadcast(timeout_t timeout);
262 
263 
269  bool waitSignal(struct timespec *timeout);
270 
276  bool waitBroadcast(struct timespec *timeout);
277 
284  inline static void set(struct timespec *hires, timeout_t timeout)
285  {Conditional::set(hires, timeout);}
286 
287 
288 #ifdef _MSTHREADS_
289  inline void lock(void)
290  {EnterCriticalSection(&mutex);};
291 
292  inline void unlock(void)
293  {LeaveCriticalSection(&mutex);};
294 
295  void waitSignal(void);
296 
297  void waitBroadcast(void);
298 
299  inline void signal(void)
300  {Conditional::signal();};
301 
302  inline void broadcast(void)
304 
305 #else
306 
309  inline void lock(void)
310  {pthread_mutex_lock(&mutex);}
311 
315  inline void unlock(void)
316  {pthread_mutex_unlock(&mutex);}
317 
321  inline void waitSignal(void)
322  {pthread_cond_wait(&cond, &mutex);}
323 
327  inline void waitBroadcast(void)
328  {pthread_cond_wait(&bcast, &mutex);}
329 
330 
334  inline void signal(void)
335  {pthread_cond_signal(&cond);}
336 
340  inline void broadcast(void)
341  {pthread_cond_broadcast(&bcast);}
342 #endif
343 public:
348 
353 
357  void access(void);
358 
362  void modify(void);
363 
367  void release(void);
368 
372  void commit(void);
373 
380  void limit_sharing(unsigned max);
381 };
382 
391 class __EXPORT TimedEvent : public Timer
392 {
393 private:
394 #ifdef _MSTHREADS_
395  HANDLE event;
396 #else
397  mutable pthread_cond_t cond;
398  bool signalled;
399 #endif
400  mutable pthread_mutex_t mutex;
401 
402 protected:
407  void lock(void);
408 
413  void release(void);
414 
422  bool sync(void);
423 
424 public:
428  TimedEvent(void);
429 
434  TimedEvent(timeout_t timeout);
435 
440  TimedEvent(time_t timeout);
441 
445  ~TimedEvent();
446 
452  void signal(void);
453 
460  bool wait(timeout_t timeout);
461 
465  void wait(void);
466 
470  void reset(void);
471 };
472 
480 class __EXPORT RecursiveMutex : private Conditional, public ExclusiveAccess
481 {
482 protected:
483  unsigned waiting;
484  unsigned lockers;
485  pthread_t locker;
486 
487  virtual void _lock(void);
488  virtual void _unlock(void);
489 
490 public:
494  RecursiveMutex();
495 
499  void lock(void);
500 
504  bool lock(timeout_t timeout);
505 
509  void release(void);
510 };
511 
524 class __EXPORT ThreadLock : private ConditionalAccess, public ExclusiveAccess, public SharedAccess
525 {
526 protected:
527  unsigned writers;
528  pthread_t writeid;
529 
530  virtual void _lock(void);
531  virtual void _share(void);
532  virtual void _unlock(void);
533 
534 public:
542  class __EXPORT guard_reader
543  {
544  private:
545  const void *object;
546 
547  public:
552  guard_reader();
553 
558  guard_reader(const void *object);
559 
563  ~guard_reader();
564 
570  void set(const void *object);
571 
575  void release(void);
576 
582  inline void operator=(const void *pointer)
583  {set(pointer);}
584  };
585 
593  class __EXPORT guard_writer
594  {
595  private:
596  const void *object;
597 
598  public:
603  guard_writer();
604 
609  guard_writer(const void *object);
610 
614  ~guard_writer();
615 
621  void set(const void *object);
622 
626  void release(void);
627 
633  inline void operator=(const void *pointer)
634  {set(pointer);}
635  };
636 
640  ThreadLock();
641 
647  bool modify(timeout_t timeout = Timer::inf);
648 
654  bool access(timeout_t timeout = Timer::inf);
655 
662  static void indexing(unsigned size);
663 
671  static bool writer(const void *object, timeout_t timeout = Timer::inf);
672 
680  static bool reader(const void *object, timeout_t timeout = Timer::inf);
681 
686  static bool release(const void *object);
687 
691  void release(void);
692 };
693 
704 class __EXPORT ReusableAllocator : protected Conditional
705 {
706 protected:
707  ReusableObject *freelist;
708  unsigned waiting;
709 
714 
721  {return object->getNext();}
722 
727  void release(ReusableObject *object);
728 };
729 
740 class __EXPORT ConditionalLock : protected ConditionalAccess, public SharedAccess
741 {
742 protected:
743  class Context : public LinkedObject
744  {
745  public:
746  inline Context(LinkedObject **root) : LinkedObject(root) {}
747 
748  pthread_t thread;
749  unsigned count;
750  };
751 
752  LinkedObject *contexts;
753 
754  virtual void _share(void);
755  virtual void _unlock(void);
756 
757  Context *getContext(void);
758 
759 public:
763  ConditionalLock();
764 
768  ~ConditionalLock();
769 
773  void modify(void);
774 
778  void commit(void);
779 
783  void access(void);
784 
788  void release(void);
789 
794  virtual void exclusive(void);
795 
799  virtual void share(void);
800 };
801 
814 class __EXPORT barrier : private Conditional
815 {
816 private:
817  unsigned count;
818  unsigned waits;
819 
820 public:
825  barrier(unsigned count);
826 
830  ~barrier();
831 
837  void set(unsigned count);
838 
842  void inc(void);
843 
847  void dec(void);
848 
853  unsigned operator++(void);
854 
855  unsigned operator--(void);
856 
860  void wait(void);
861 
868  bool wait(timeout_t timeout);
869 };
870 
879 class __EXPORT Semaphore : public SharedAccess, protected Conditional
880 {
881 protected:
882  unsigned count, waits, used;
883 
884  virtual void _share(void);
885  virtual void _unlock(void);
886 
887 public:
892  Semaphore(unsigned count = 0);
893 
899  Semaphore(unsigned count, unsigned avail);
900 
905  void wait(void);
906 
914  bool wait(timeout_t timeout);
915 
920  void set(unsigned count);
921 
925  void release(void);
926 
930  inline void operator++(void)
931  {wait();}
932 
936  inline void operator--(void)
937  {release();}
938 };
939 
953 class __EXPORT Mutex : public ExclusiveAccess
954 {
955 protected:
956  mutable pthread_mutex_t mlock;
957 
958  virtual void _lock(void);
959  virtual void _unlock(void);
960 
961 public:
962  friend class autolock;
963 
964  class __EXPORT autolock
965  {
966  private:
967  pthread_mutex_t *mutex;
968 
969  public:
970  inline autolock(const Mutex *object) {
971  mutex = &object->mlock;
972  pthread_mutex_lock(this->mutex);
973  }
974 
975  inline ~autolock() {
976  pthread_mutex_unlock(this->mutex);
977  }
978  };
979 
987  class __EXPORT guard
988  {
989  private:
990  const void *object;
991 
992  public:
997  guard();
998 
1003  guard(const void *object);
1004 
1008  ~guard();
1009 
1015  void set(const void *object);
1016 
1020  void release(void);
1021 
1027  inline void operator=(void *pointer)
1028  {set(pointer);}
1029  };
1030 
1031 
1035  Mutex();
1036 
1040  ~Mutex();
1041 
1045  inline void acquire(void)
1046  {pthread_mutex_lock(&mlock);}
1047 
1051  inline void lock(void)
1052  {pthread_mutex_lock(&mlock);}
1053 
1057  inline void unlock(void)
1058  {pthread_mutex_unlock(&mlock);}
1059 
1063  inline void release(void)
1064  {pthread_mutex_unlock(&mlock);}
1065 
1070  inline static void acquire(pthread_mutex_t *lock)
1071  {pthread_mutex_lock(lock);}
1072 
1077  inline static void release(pthread_mutex_t *lock)
1078  {pthread_mutex_unlock(lock);}
1079 
1086  static void indexing(unsigned size);
1087 
1093  static bool protect(const void *pointer);
1094 
1099  static bool release(const void *pointer);
1100 };
1101 
1110 class __EXPORT auto_protect
1111 {
1112 private:
1113  // cannot copy...
1114  inline auto_protect(const auto_object &pointer) {}
1115 
1116 protected:
1117  const void *object;
1118 
1119  auto_protect();
1120 
1121 public:
1126  auto_protect(const void *object);
1127 
1132  ~auto_protect();
1133 
1137  void release(void);
1138 
1143  inline bool operator!() const
1144  {return object == NULL;}
1145 
1150  inline operator bool() const
1151  {return object != NULL;}
1152 
1159  void operator=(const void *object);
1160 };
1161 
1173 class __EXPORT LockedPointer
1174 {
1175 private:
1176  friend class locked_release;
1177  mutable pthread_mutex_t mutex;
1179 
1180 protected:
1184  LockedPointer();
1185 
1190  void replace(ObjectProtocol *object);
1191 
1196  ObjectProtocol *dup(void);
1197 
1202  inline void operator=(ObjectProtocol *object)
1203  {replace(object);}
1204 };
1205 
1214 class __EXPORT SharedObject
1215 {
1216 protected:
1217  friend class SharedPointer;
1218 
1227  virtual void commit(SharedPointer *pointer);
1228 
1229 public:
1233  virtual ~SharedObject();
1234 };
1235 
1246 class __EXPORT SharedPointer : protected ConditionalAccess
1247 {
1248 private:
1249  friend class shared_release;
1251 
1252 protected:
1256  SharedPointer();
1257 
1261  ~SharedPointer();
1262 
1269  void replace(SharedObject *object);
1270 
1277  SharedObject *share(void);
1278 };
1279 
1290 class __EXPORT Thread
1291 {
1292 protected:
1293 // may be used in future if we need cancelable threads...
1294 #ifdef _MSTHREADS_
1295  HANDLE cancellor;
1296 #else
1297  void *cancellor;
1298 #endif
1299 
1300  enum {R_UNUSED} reserved; // cancel mode?
1301  pthread_t tid;
1302  stacksize_t stack;
1303  int priority;
1304 
1310  Thread(size_t stack = 0);
1311 
1316  void map(void);
1317 
1321  virtual bool is_active(void) const;
1322 
1323 public:
1330  void setPriority(void);
1331 
1336  static void yield(void);
1337 
1342  static void sleep(timeout_t timeout);
1343 
1350  static Thread *get(void);
1351 
1355  virtual void run(void) = 0;
1356 
1360  virtual ~Thread();
1361 
1370  virtual void exit(void);
1371 
1375  static void init(void);
1376 
1382  static void policy(int polid);
1383 
1388  static void concurrency(int level);
1389 
1396  static bool equal(pthread_t thread1, pthread_t thread2);
1397 
1402  static pthread_t self(void);
1403 
1404  inline operator bool() const
1405  {return is_active();}
1406 
1407  inline bool operator!() const
1408  {return !is_active();}
1409 
1410  inline bool isRunning(void) const
1411  {return is_active();}
1412 };
1413 
1424 class __EXPORT JoinableThread : public Thread
1425 {
1426 protected:
1427 #ifdef _MSTHREADS_
1428  HANDLE running;
1429 #else
1430  volatile bool running;
1431 #endif
1432  volatile bool joining;
1433 
1438  JoinableThread(size_t size = 0);
1439 
1444  virtual ~JoinableThread();
1445 
1451  void join(void);
1452 
1453  bool is_active(void) const;
1454 
1455  virtual void run(void) = 0;
1456 
1457 public:
1458 
1467  void start(int priority = 0);
1468 
1473  inline void background(void)
1474  {start(-1);}
1475 };
1476 
1484 class __EXPORT DetachedThread : public Thread
1485 {
1486 protected:
1487  bool active;
1488 
1493  DetachedThread(size_t size = 0);
1494 
1500  ~DetachedThread();
1501 
1510  void exit(void);
1511 
1512  bool is_active(void) const;
1513 
1514  virtual void run(void) = 0;
1515 
1516 public:
1523  void start(int priority = 0);
1524 };
1525 
1534 class __EXPORT locked_release
1535 {
1536 protected:
1542  locked_release();
1543 
1549  locked_release(const locked_release &object);
1550 
1551 public:
1558 
1563  ~locked_release();
1564 
1568  void release(void);
1569 
1575  locked_release &operator=(LockedPointer &pointer);
1576 };
1577 
1587 class __EXPORT shared_release
1588 {
1589 protected:
1595  shared_release();
1596 
1602  shared_release(const shared_release &object);
1603 
1604 public:
1610 
1616  ~shared_release();
1617 
1621  void release(void);
1622 
1627  SharedObject *get(void);
1628 
1634  shared_release &operator=(SharedPointer &pointer);
1635 };
1636 
1644 template<class T>
1646 {
1647 public:
1652 
1660  inline const T *dup(void)
1661  {return static_cast<const T*>(SharedPointer::share());}
1662 
1669  inline void replace(T *object)
1670  {SharedPointer::replace(object);}
1671 
1676  inline void operator=(T *object)
1677  {replace(object);}
1678 
1683  inline T *operator*()
1684  {return dup();}
1685 };
1686 
1694 template<class T>
1696 {
1697 public:
1702 
1708  inline T* dup(void)
1709  {return static_cast<T *>(LockedPointer::dup());}
1710 
1715  inline void replace(T *object)
1716  {LockedPointer::replace(object);}
1717 
1722  inline void operator=(T *object)
1723  {replace(object);}
1724 
1730  inline T *operator*()
1731  {return dup();}
1732 };
1733 
1739 template<class T>
1741 {
1742 public:
1747 
1753 
1758  inline T& operator*() const
1759  {return *(static_cast<T&>(object));}
1760 
1765  inline T* operator->() const
1766  {return static_cast<T*>(object);}
1767 
1772  inline T* get(void) const
1773  {return static_cast<T*>(object);}
1774 };
1775 
1781 template<class T>
1783 {
1784 public:
1789 
1796 
1800  inline const T& operator*() const
1801  {return *(static_cast<const T&>(ptr->pointer));}
1802 
1807  inline const T* operator->() const
1808  {return static_cast<const T*>(ptr->pointer);}
1809 
1814  inline const T* get(void) const
1815  {return static_cast<const T*>(ptr->pointer);}
1816 };
1817 
1824 template <class T>
1826 {
1827 public:
1831  inline mutex_pointer() : auto_protect() {}
1832 
1837  inline mutex_pointer(T* object) : auto_protect(object) {}
1838 
1843  inline T& operator*() const
1844  {return *(static_cast<T&>(auto_protect::object));}
1845 
1850  inline T* operator->() const
1851  {return static_cast<T*>(auto_protect::object);}
1852 
1857  inline T* get(void) const
1858  {return static_cast<T*>(auto_protect::object);}
1859 };
1860 
1866 inline void start(JoinableThread *thread, int priority = 0)
1867  {thread->start(priority);}
1868 
1874 inline void start(DetachedThread *thread, int priority = 0)
1875  {thread->start(priority);}
1876 
1881 
1886 
1891 
1895 typedef Mutex mutex_t;
1896 
1901 
1906 
1911 
1916 
1921 inline void wait(barrier_t &barrier)
1922  {barrier.wait();}
1923 
1929 inline void wait(semaphore_t &semaphore, timeout_t timeout = Timer::inf)
1930  {semaphore.wait(timeout);}
1931 
1936 inline void release(semaphore_t &semaphore)
1937  {semaphore.release();}
1938 
1943 inline void acquire(mutex_t &mutex)
1944  {mutex.lock();}
1945 
1950 inline void release(mutex_t &mutex)
1951  {mutex.release();}
1952 
1957 inline void modify(accesslock_t &lock)
1958  {lock.modify();}
1959 
1964 inline void access(accesslock_t &lock)
1965  {lock.access();}
1966 
1971 inline void release(accesslock_t &lock)
1972  {lock.release();}
1973 
1979 inline void commit(accesslock_t &lock)
1980  {lock.commit();}
1981 
1986 inline void exclusive(condlock_t &lock)
1987  {lock.exclusive();}
1988 
1993 inline void share(condlock_t &lock)
1994  {lock.share();}
1995 
2000 inline void modify(condlock_t &lock)
2001  {lock.modify();}
2002 
2008 inline void commit(condlock_t &lock)
2009  {lock.commit();}
2010 
2015 inline void access(condlock_t &lock)
2016  {lock.access();}
2017 
2022 inline void release(condlock_t &lock)
2023  {lock.release();}
2024 
2030 inline bool exclusive(rwlock_t &lock, timeout_t timeout = Timer::inf)
2031  {return lock.modify(timeout);}
2032 
2038 inline bool share(rwlock_t &lock, timeout_t timeout = Timer::inf)
2039  {return lock.access(timeout);}
2040 
2045 inline void release(rwlock_t &lock)
2046  {lock.release();}
2047 
2052 inline void lock(rexlock_t &lock)
2053  {lock.lock();}
2054 
2059 inline void release(rexlock_t &lock)
2060  {lock.release();}
2061 
2062 #define __AUTOPROTECT(x) Mutex::guard __autolock__(x)
2063 
2064 #define __AUTOLOCK__ autolock __autolock__(this);
2065 
2066 #define __SYNC__ for(bool _sync_flag_ = Mutex::protect(this); _sync_flag_; _sync_flag_ = !Mutex::release(this))
2067 
2068 #define __SHARED__ for(bool _sync_flag_ = ThreadLock::reader(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
2069 
2070 #define ___EXCLUSIVE__ for(bool _sync_flag_ = ThreadLock::writer(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
2071 
2072 } // namespace ucommon
2073 
2074 #endif
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
Definition: thread.h:1905
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:271
Semaphore semaphore_t
Convenience type for using counting semaphores.
Definition: thread.h:1910
void start(int priority=0)
Start execution of detached context.
Shared singleton object.
Definition: thread.h:1214
The conditional is a common base for other thread synchronizing classes.
Definition: thread.h:87
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it's protocol.
Definition: access.h:289
Auto-pointer support class for shared singleton objects.
Definition: thread.h:1587
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:280
void operator--(void)
Convenience operator to release a counting semaphore.
Definition: thread.h:936
Portable recursive exclusive lock.
Definition: thread.h:480
A general purpose smart pointer helper class.
Definition: object.h:137
Event notification to manage scheduled realtime threads.
Definition: thread.h:391
void commit(accesslock_t &lock)
Convenience function to commit an exclusive access lock.
Definition: thread.h:1979
SharedPointer * ptr
Shared lock for protected singleton.
Definition: thread.h:1590
void background(void)
Start execution of child context as background thread.
Definition: thread.h:1473
TimedEvent timedevent_t
Convenience type for using timed events.
Definition: thread.h:1890
void access(void)
Acquire access (shared read) lock.
void modify(void)
Acquire write (exclusive modify) lock.
void signal(void)
Signal the conditional to release one waiting thread.
Definition: thread.h:161
ThreadLock rwlock_t
Convenience type for using read/write locks.
Definition: thread.h:1900
A templated smart pointer instance for lock protected objects.
Definition: thread.h:1740
shared_instance(shared_pointer< T > &pointer)
Construct shared access instance of shared typed singleton from matching shared_pointer.
Definition: thread.h:1795
void replace(SharedObject *object)
Replace existing singleton instance with new one.
void release(void)
Release acquired lock.
Definition: thread.h:1063
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:59
An exclusive locking access interface base.
Definition: access.h:98
Templated shared pointer for singleton shared objects of specific type.
Definition: thread.h:1645
void wait(void)
Wait until the semphore usage count is less than the thread limit.
void operator=(T *object)
Replace existing typed singleton object through assignment.
Definition: thread.h:1676
void commit(void)
Complete exclusive mode write scheduling.
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
ConditionalLock condlock_t
Convenience type for using conditional locks.
Definition: thread.h:1880
An object pointer that uses mutex to assure thread-safe singleton use.
Definition: thread.h:1173
void operator++(void)
Convenience operator to wait on a counting semaphore.
Definition: thread.h:930
static void acquire(pthread_mutex_t *lock)
Convenience function to acquire os native mutex lock directly.
Definition: thread.h:1070
T * dup(void)
Create a duplicate reference counted instance of the current typed object.
Definition: thread.h:1708
ConditionalAccess accesslock_t
Convenience type for scheduling access.
Definition: thread.h:1885
Guard class to apply scope based mutex locking to objects.
Definition: thread.h:987
void wait(void)
Wait at the barrier until the count of threads waiting is reached.
void unlock(void)
Unlock the conditional's supporting mutex.
Definition: thread.h:315
virtual void share(void)
Return an exclusive access lock back to share mode.
shared_pointer()
Created shared locking for typed singleton pointer.
Definition: thread.h:1651
void lock(void)
Acquire or increase locking.
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
Definition: thread.h:1957
bool operator!() const
Test if the pointer is not set.
Definition: thread.h:1143
void broadcast(void)
Signal the conditional to release all waiting threads.
Definition: thread.h:167
const T * dup(void)
Acquire a shared (duplocate) reference to the typed singleton object.
Definition: thread.h:1660
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:324
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:510
void lock(void)
Lock the conditional's supporting mutex.
Definition: thread.h:143
Private heaps, pools, and associations.
void operator=(T *object)
Replace existing object through assignment.
Definition: thread.h:1722
void release(void)
Release the semaphore after waiting for it.
Locking protocol classes for member function automatic operations.
A common base class for all managed objects.
Definition: protocols.h:545
void broadcast(void)
Signal the conditional to release all broadcast threads.
Definition: thread.h:340
An optimized and convertable shared lock.
Definition: thread.h:740
barrier barrier_t
Convenience type for using thread barriers.
Definition: thread.h:1915
virtual void exclusive(void)
Convert read lock into exclusive (write/modify) access.
void release(void)
Release or decrease locking.
void acquire(void)
Acquire mutex lock.
Definition: thread.h:1045
Reusable objects for forming private heaps.
Definition: linked.h:158
locked_instance(locked_pointer< T > &pointer)
Construct locked instance of typed object from matching locked_pointer.
Definition: thread.h:1752
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
Definition: thread.h:1943
Typed smart locked pointer class.
Definition: thread.h:1825
Realtime timers and timer queues.
void operator=(void *pointer)
Set guard to mutex lock a new object.
Definition: thread.h:1027
void waitBroadcast(void)
Wait (block) until broadcast.
Definition: thread.h:327
void commit(void)
Commit changes / release a modify lock.
Generic non-recursive exclusive lock class.
Definition: thread.h:953
A child thread object that may be joined by parent.
Definition: thread.h:1424
T * operator*()
Create a duplicate reference counted instance of the current typed object by pointer reference...
Definition: thread.h:1730
mutex_pointer()
Create a pointer with no reference.
Definition: thread.h:1831
void operator=(ObjectProtocol *object)
Replace existing object through assignment.
Definition: thread.h:1202
void waitSignal(void)
Wait (block) until signalled.
Definition: thread.h:321
Generic smart pointer class.
Definition: generics.h:54
void start(int priority=0)
Start execution of child context.
const T & operator*() const
Access shared typed singleton object this instance locks and references.
Definition: thread.h:1800
locked_pointer()
Create an instance of a typed locked pointer.
Definition: thread.h:1701
A templated smart pointer instance for shared singleton typed objects.
Definition: thread.h:1782
void operator=(const void *pointer)
Set guard to read lock a new object.
Definition: thread.h:633
T & operator*() const
Extract instance of locked typed object by pointer reference.
Definition: thread.h:1758
void unlock(void)
Release acquired lock.
Definition: thread.h:1057
shared_instance()
Construct empty instance to reference shared typed singleton.
Definition: thread.h:1788
ObjectProtocol * dup(void)
Create a duplicate reference counted instance of the current object.
Auto-pointer support class for locked objects.
Definition: thread.h:1534
Common namespace for all ucommon objects.
Definition: access.h:47
Guard class to apply scope based exclusive locking to objects.
Definition: thread.h:593
The shared pointer is used to manage a singleton instance of shared object.
Definition: thread.h:1246
The conditional rw seperates scheduling for optizming behavior or rw locks.
Definition: thread.h:238
void lock(void)
Lock the conditional's supporting mutex.
Definition: thread.h:309
A generic and portable implimentation of Read/Write locking.
Definition: thread.h:524
void lock(void)
Acquire mutex lock.
Definition: thread.h:1051
T * operator->() const
Reference member of object we are pointing to.
Definition: thread.h:1850
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it's protocol.
Definition: access.h:253
A portable implimentation of "barrier" thread sychronization.
Definition: thread.h:814
void replace(ObjectProtocol *object)
Replace existing object with a new one for next request.
An exclusive locking protocol interface base.
Definition: access.h:69
Class for resource bound memory pools between threads.
Definition: thread.h:704
A mutex locked object smart pointer helper class.
Definition: thread.h:1110
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
Definition: access.h:262
ObjectProtocol * object
locked object protected by locked_release
Definition: thread.h:1537
void wait(void)
Wait (block) until signalled.
Definition: thread.h:155
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
Definition: thread.h:284
Timer class to use when scheduling realtime events.
Definition: timers.h:50
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1921
const T * operator->() const
Access member of shared typed singleton object this instance locks and references.
Definition: thread.h:1807
void replace(T *object)
Replace existing typed object with a new one for next request.
Definition: thread.h:1715
A detached thread object that is stand-alone.
Definition: thread.h:1484
Mutex mutex_t
Convenience type for using exclusive mutex locks.
Definition: thread.h:1895
T & operator*() const
Reference object we are pointing to through pointer indirection.
Definition: thread.h:1843
static pthread_condattr_t * initializer(void)
Support function for getting conditional attributes for realtime scheduling.
Definition: thread.h:218
void unlock(void)
Unlock the conditional's supporting mutex.
Definition: thread.h:149
locked_instance()
Construct empty locked instance of typed object.
Definition: thread.h:1746
static void release(pthread_mutex_t *lock)
Convenience function to release os native mutex lock directly.
Definition: thread.h:1077
void operator=(const void *pointer)
Set guard to read lock a new object.
Definition: thread.h:582
void release(void)
Release a shared lock.
void release(void)
Release access mode read scheduling.
void modify(void)
Exclusive mode write thread scheduling.
static const timeout_t inf
A value to use for infinite time.
Definition: timers.h:83
Templated locked pointer for referencing locked objects of specific type.
Definition: thread.h:1695
void signal(void)
Signal the conditional to release one signalled thread.
Definition: thread.h:334
mutex_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition: thread.h:1837
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:414
Guard class to apply scope based access locking to objects.
Definition: thread.h:542
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
void replace(T *object)
Replace existing typed singleton instance with new one.
Definition: thread.h:1669
An abstract class for defining classes that operate as a thread.
Definition: thread.h:1290
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it's protocol.
Definition: access.h:298
A portable counting semaphore class.
Definition: thread.h:879
static bool release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
void access(void)
Access mode shared thread scheduling.
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Definition: thread.h:1866
Runtime functions.
T * operator*()
Access shared lock typed singleton object by pointer reference.
Definition: thread.h:1683
bool modify(timeout_t timeout=Timer::inf)
Request modify (write) access through the lock.
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
Definition: linked.h:170
SharedObject * share(void)
Acquire a shared reference to the singleton object.
ReusableObject * next(ReusableObject *object)
Get next reusable object in the pool.
Definition: thread.h:720
T * operator->() const
Access member of instance of locked typed object by member reference.
Definition: thread.h:1765