ucommon
thread.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
53 #ifndef _UCOMMON_THREAD_H_
54 #define _UCOMMON_THREAD_H_
55 
56 #ifndef _UCOMMON_CPR_H_
57 #include <ucommon/cpr.h>
58 #endif
59 
60 #ifndef _UCOMMON_ACCESS_H_
61 #include <ucommon/access.h>
62 #endif
63 
64 #ifndef _UCOMMON_TIMERS_H_
65 #include <ucommon/timers.h>
66 #endif
67 
68 #ifndef _UCOMMON_MEMORY_H_
69 #include <ucommon/memory.h>
70 #endif
71 
72 NAMESPACE_UCOMMON
73 
74 class SharedPointer;
75 
86 class __EXPORT Conditional
87 {
88 private:
89  friend class ConditionalAccess;
90 
91 #if defined(_MSCONDITIONAL_)
92  CRITICAL_SECTION mutex;
93  CONDITION_VARIABLE cond;
94 #elif defined(_MSWINDOWS_)
95  enum {SIGNAL = 0, BROADCAST = 1};
96  HANDLE events[2];
97  unsigned waiting;
98  CRITICAL_SECTION mlock;
99  CRITICAL_SECTION mutex;
100 #else
101 #ifndef __PTH__
102  class __LOCAL attribute
103  {
104  public:
105  pthread_condattr_t attr;
106  attribute();
107  };
108 
109  __LOCAL static attribute attr;
110 #endif
111 
112  pthread_cond_t cond;
113  pthread_mutex_t mutex;
114 #endif
115 
116 protected:
117  friend class TimedEvent;
118 
124  bool wait(timeout_t timeout);
125 
131  bool wait(struct timespec *timeout);
132 
133 #ifdef _MSWINDOWS_
134  inline void lock(void)
135  {EnterCriticalSection(&mutex);};
136 
137  inline void unlock(void)
138  {LeaveCriticalSection(&mutex);};
139 
140  void wait(void);
141  void signal(void);
142  void broadcast(void);
143 
144 #else
145 
148  inline void lock(void)
149  {pthread_mutex_lock(&mutex);};
150 
154  inline void unlock(void)
155  {pthread_mutex_unlock(&mutex);};
156 
160  inline void wait(void)
161  {pthread_cond_wait(&cond, &mutex);};
162 
166  inline void signal(void)
167  {pthread_cond_signal(&cond);};
168 
172  inline void broadcast(void)
173  {pthread_cond_broadcast(&cond);};
174 #endif
175 
179  Conditional();
180 
184  ~Conditional();
185 
186 public:
187 #if !defined(_MSWINDOWS_) && !defined(__PTH__)
188 
193  static inline pthread_condattr_t *initializer(void)
194  {return &attr.attr;};
195 #endif
196 
203  static void set(struct timespec *hires, timeout_t timeout);
204 };
205 
213 class __EXPORT ConditionalAccess : private Conditional
214 {
215 protected:
216 #if defined _MSCONDITIONAL_
217  CONDITION_VARIABLE bcast;
218 #elif !defined(_MSWINDOWS_)
219  pthread_cond_t bcast;
220 #endif
221 
222  unsigned pending, waiting, sharing;
223 
229  bool waitSignal(timeout_t timeout);
230 
236  bool waitBroadcast(timeout_t timeout);
237 
238 
244  bool waitSignal(struct timespec *timeout);
245 
251  bool waitBroadcast(struct timespec *timeout);
252 
259  inline static void set(struct timespec *hires, timeout_t timeout)
260  {Conditional::set(hires, timeout);};
261 
262 
263 #ifdef _MSWINDOWS_
264  inline void lock(void)
265  {EnterCriticalSection(&mutex);};
266 
267  inline void unlock(void)
268  {LeaveCriticalSection(&mutex);};
269 
270  void waitSignal(void);
271  void waitBroadcast(void);
272 
273  inline void signal(void)
274  {Conditional::signal();};
275 
276  inline void broadcast(void)
278 
279 #else
280 
283  inline void lock(void)
284  {pthread_mutex_lock(&mutex);};
285 
289  inline void unlock(void)
290  {pthread_mutex_unlock(&mutex);};
291 
295  inline void waitSignal(void)
296  {pthread_cond_wait(&cond, &mutex);};
297 
301  inline void waitBroadcast(void)
302  {pthread_cond_wait(&bcast, &mutex);};
303 
304 
308  inline void signal(void)
309  {pthread_cond_signal(&cond);};
310 
314  inline void broadcast(void)
315  {pthread_cond_broadcast(&bcast);};
316 #endif
317 public:
322 
327 
331  void access(void);
332 
336  void modify(void);
337 
341  void release(void);
342 
346  void commit(void);
347 
354  void limit_sharing(unsigned max);
355 };
356 
365 class __EXPORT TimedEvent : public Timer
366 {
367 private:
368 #ifdef _MSWINDOWS_
369  HANDLE event;
370 #else
371  pthread_cond_t cond;
372  bool signalled;
373 #endif
374  pthread_mutex_t mutex;
375 
376 protected:
381  void lock(void);
382 
387  void release(void);
388 
396  bool sync(void);
397 
398 public:
402  TimedEvent(void);
403 
408  TimedEvent(timeout_t timeout);
409 
414  TimedEvent(time_t timeout);
415 
419  ~TimedEvent();
420 
426  void signal(void);
427 
434  bool wait(timeout_t timeout);
435 
439  void wait(void);
440 
444  void reset(void);
445 };
446 
454 class __EXPORT RecursiveMutex : private Conditional, public ExclusiveAccess
455 {
456 protected:
457  unsigned waiting;
458  unsigned lockers;
459  pthread_t locker;
460 
461  virtual void _lock(void);
462  virtual void _unlock(void);
463 
464 public:
468  RecursiveMutex();
469 
473  void lock(void);
474 
478  bool lock(timeout_t timeout);
479 
483  void release(void);
484 };
485 
498 class __EXPORT ThreadLock : private ConditionalAccess, public ExclusiveAccess, public SharedAccess
499 {
500 protected:
501  unsigned writers;
502  pthread_t writeid;
503 
504  virtual void _lock(void);
505  virtual void _share(void);
506  virtual void _unlock(void);
507 
508 public:
516  class __EXPORT guard_reader
517  {
518  private:
519  const void *object;
520 
521  public:
526  guard_reader();
527 
532  guard_reader(const void *object);
533 
537  ~guard_reader();
538 
544  void set(const void *object);
545 
549  void release(void);
550 
556  inline void operator=(const void *pointer)
557  {set(pointer);};
558  };
559 
567  class __EXPORT guard_writer
568  {
569  private:
570  const void *object;
571 
572  public:
577  guard_writer();
578 
583  guard_writer(const void *object);
584 
588  ~guard_writer();
589 
595  void set(const void *object);
596 
600  void release(void);
601 
607  inline void operator=(const void *pointer)
608  {set(pointer);};
609  };
610 
614  ThreadLock();
615 
621  bool modify(timeout_t timeout = Timer::inf);
622 
628  bool access(timeout_t timeout = Timer::inf);
629 
636  static void indexing(unsigned size);
637 
645  static bool writer(const void *object, timeout_t timeout = Timer::inf);
646 
654  static bool reader(const void *object, timeout_t timeout = Timer::inf);
655 
660  static void release(const void *object);
661 
665  void release(void);
666 };
667 
678 class __EXPORT ReusableAllocator : protected Conditional
679 {
680 protected:
681  ReusableObject *freelist;
682  unsigned waiting;
683 
688 
695  {return object->getNext();};
696 
701  void release(ReusableObject *object);
702 };
703 
714 class __EXPORT ConditionalLock : protected ConditionalAccess, public SharedAccess
715 {
716 protected:
717  class Context : public LinkedObject
718  {
719  public:
720  inline Context(LinkedObject **root) : LinkedObject(root) {};
721 
722  pthread_t thread;
723  unsigned count;
724  };
725 
726  LinkedObject *contexts;
727 
728  virtual void _share(void);
729  virtual void _unlock(void);
730 
731  Context *getContext(void);
732 
733 public:
737  ConditionalLock();
738 
742  ~ConditionalLock();
743 
747  void modify(void);
748 
752  void commit(void);
753 
757  void access(void);
758 
762  void release(void);
763 
768  virtual void exclusive(void);
769 
773  virtual void share(void);
774 };
775 
788 class __EXPORT barrier : private Conditional
789 {
790 private:
791  unsigned count;
792  unsigned waits;
793 
794 public:
799  barrier(unsigned count);
800 
804  ~barrier();
805 
811  void set(unsigned count);
812 
816  void inc(void);
817 
821  void dec(void);
822 
827  unsigned operator++(void);
828 
829  unsigned operator--(void);
830 
834  void wait(void);
835 
842  bool wait(timeout_t timeout);
843 };
844 
853 class __EXPORT Semaphore : public SharedAccess, protected Conditional
854 {
855 protected:
856  unsigned count, waits, used;
857 
858  virtual void _share(void);
859  virtual void _unlock(void);
860 
861 public:
866  Semaphore(unsigned count = 0);
867 
873  Semaphore(unsigned count, unsigned avail);
874 
879  void wait(void);
880 
888  bool wait(timeout_t timeout);
889 
894  void set(unsigned count);
895 
899  void release(void);
900 
904  inline void operator++(void)
905  {wait();};
906 
910  inline void operator--(void)
911  {release();};
912 };
913 
927 class __EXPORT Mutex : public ExclusiveAccess
928 {
929 protected:
930  pthread_mutex_t mlock;
931 
932  virtual void _lock(void);
933  virtual void _unlock(void);
934 
935 public:
943  class __EXPORT guard
944  {
945  private:
946  const void *object;
947 
948  public:
953  guard();
954 
959  guard(const void *object);
960 
964  ~guard();
965 
971  void set(const void *object);
972 
976  void release(void);
977 
983  inline void operator=(void *pointer)
984  {set(pointer);};
985  };
986 
987 
991  Mutex();
992 
996  ~Mutex();
997 
1001  inline void acquire(void)
1002  {pthread_mutex_lock(&mlock);};
1003 
1007  inline void lock(void)
1008  {pthread_mutex_lock(&mlock);};
1009 
1013  inline void unlock(void)
1014  {pthread_mutex_unlock(&mlock);};
1015 
1019  inline void release(void)
1020  {pthread_mutex_unlock(&mlock);};
1021 
1026  inline static void acquire(pthread_mutex_t *lock)
1027  {pthread_mutex_lock(lock);};
1028 
1033  inline static void release(pthread_mutex_t *lock)
1034  {pthread_mutex_unlock(lock);};
1035 
1042  static void indexing(unsigned size);
1043 
1049  static void protect(const void *pointer);
1050 
1055  static void release(const void *pointer);
1056 };
1057 
1066 class __EXPORT auto_protect
1067 {
1068 private:
1069  // cannot copy...
1070  inline auto_protect(const auto_object &pointer) {};
1071 
1072 protected:
1073  const void *object;
1074 
1075  auto_protect();
1076 
1077 public:
1082  auto_protect(const void *object);
1083 
1088  ~auto_protect();
1089 
1093  void release(void);
1094 
1099  inline bool operator!() const
1100  {return object == NULL;};
1101 
1106  inline operator bool() const
1107  {return object != NULL;};
1108 
1115  void operator=(const void *object);
1116 };
1117 
1129 class __EXPORT LockedPointer
1130 {
1131 private:
1132  friend class locked_release;
1133  pthread_mutex_t mutex;
1135 
1136 protected:
1140  LockedPointer();
1141 
1146  void replace(ObjectProtocol *object);
1147 
1152  ObjectProtocol *dup(void);
1153 
1158  inline void operator=(ObjectProtocol *object)
1159  {replace(object);};
1160 };
1161 
1170 class __EXPORT SharedObject
1171 {
1172 protected:
1173  friend class SharedPointer;
1174 
1183  virtual void commit(SharedPointer *pointer);
1184 
1185 public:
1189  virtual ~SharedObject();
1190 };
1191 
1202 class __EXPORT SharedPointer : protected ConditionalAccess
1203 {
1204 private:
1205  friend class shared_release;
1207 
1208 protected:
1212  SharedPointer();
1213 
1217  ~SharedPointer();
1218 
1225  void replace(SharedObject *object);
1226 
1233  SharedObject *share(void);
1234 };
1235 
1246 class __EXPORT Thread
1247 {
1248 protected:
1249 // may be used in future if we need cancelable threads...
1250 #ifdef _MSWINDOWS_
1251  HANDLE cancellor;
1252 #else
1253  void *cancellor;
1254 #endif
1255 
1256  enum {} reserved; // cancel mode?
1257  pthread_t tid;
1258  size_t stack;
1259  int priority;
1260 
1266  Thread(size_t stack = 0);
1267 
1272  void map(void);
1273 
1277  virtual bool is_active(void);
1278 
1279 public:
1286  void setPriority(void);
1287 
1292  static void yield(void);
1293 
1298  static void sleep(timeout_t timeout);
1299 
1306  static Thread *get(void);
1307 
1311  virtual void run(void) = 0;
1312 
1316  virtual ~Thread();
1317 
1326  virtual void exit(void);
1327 
1331  static void init(void);
1332 
1338  static void policy(int polid);
1339 
1344  static void concurrency(int level);
1345 
1352  static bool equal(pthread_t thread1, pthread_t thread2);
1353 
1358  static pthread_t self(void);
1359 
1360  inline operator bool()
1361  {return is_active();}
1362 
1363  inline bool operator!()
1364  {return !is_active();}
1365 
1366  inline bool isRunning(void)
1367  {return is_active();}
1368 };
1369 
1380 class __EXPORT JoinableThread : public Thread
1381 {
1382 protected:
1383 #ifdef _MSWINDOWS_
1384  HANDLE running;
1385 #else
1386  volatile bool running;
1387 #endif
1388  volatile bool joining;
1389 
1394  JoinableThread(size_t size = 0);
1395 
1400  virtual ~JoinableThread();
1401 
1407  void join(void);
1408 
1409  bool is_active(void);
1410 
1411  virtual void run(void) = 0;
1412 
1413 public:
1414 
1423  void start(int priority = 0);
1424 
1429  inline void background(void)
1430  {start(-1);};
1431 };
1432 
1440 class __EXPORT DetachedThread : public Thread
1441 {
1442 protected:
1443  bool active;
1444 
1449  DetachedThread(size_t size = 0);
1450 
1456  ~DetachedThread();
1457 
1466  void exit(void);
1467 
1468  bool is_active(void);
1469 
1470  virtual void run(void) = 0;
1471 
1472 public:
1479  void start(int priority = 0);
1480 };
1481 
1490 class __EXPORT locked_release
1491 {
1492 protected:
1498  locked_release();
1499 
1505  locked_release(const locked_release &object);
1506 
1507 public:
1514 
1519  ~locked_release();
1520 
1524  void release(void);
1525 
1531  locked_release &operator=(LockedPointer &pointer);
1532 };
1533 
1543 class __EXPORT shared_release
1544 {
1545 protected:
1551  shared_release();
1552 
1558  shared_release(const shared_release &object);
1559 
1560 public:
1566 
1572  ~shared_release();
1573 
1577  void release(void);
1578 
1583  SharedObject *get(void);
1584 
1590  shared_release &operator=(SharedPointer &pointer);
1591 };
1592 
1600 template<class T>
1602 {
1603 public:
1608 
1616  inline const T *dup(void)
1617  {return static_cast<const T*>(SharedPointer::share());};
1618 
1625  inline void replace(T *object)
1626  {SharedPointer::replace(object);};
1627 
1632  inline void operator=(T *object)
1633  {replace(object);};
1634 
1639  inline T *operator*()
1640  {return dup();};
1641 };
1642 
1650 template<class T>
1652 {
1653 public:
1658 
1664  inline T* dup(void)
1665  {return static_cast<T *>(LockedPointer::dup());};
1666 
1671  inline void replace(T *object)
1672  {LockedPointer::replace(object);};
1673 
1678  inline void operator=(T *object)
1679  {replace(object);};
1680 
1686  inline T *operator*()
1687  {return dup();};
1688 };
1689 
1695 template<class T>
1697 {
1698 public:
1703 
1709 
1714  inline T& operator*() const
1715  {return *(static_cast<T&>(object));};
1716 
1721  inline T* operator->() const
1722  {return static_cast<T*>(object);};
1723 
1728  inline T* get(void) const
1729  {return static_cast<T*>(object);};
1730 };
1731 
1737 template<class T>
1739 {
1740 public:
1745 
1752 
1756  inline const T& operator*() const
1757  {return *(static_cast<const T&>(ptr->pointer));};
1758 
1763  inline const T* operator->() const
1764  {return static_cast<const T*>(ptr->pointer);};
1765 
1770  inline const T* get(void) const
1771  {return static_cast<const T*>(ptr->pointer);};
1772 };
1773 
1780 template <class T>
1782 {
1783 public:
1787  inline mutex_pointer() : auto_protect() {};
1788 
1793  inline mutex_pointer(T* object) : auto_protect(object) {};
1794 
1799  inline T& operator*() const
1800  {return *(static_cast<T&>(auto_protect::object));};
1801 
1806  inline T* operator->() const
1807  {return static_cast<T*>(auto_protect::object);};
1808 
1813  inline T* get(void) const
1814  {return static_cast<T*>(auto_protect::object);};
1815 };
1816 
1822 inline void start(JoinableThread *thread, int priority = 0)
1823  {thread->start(priority);}
1824 
1830 inline void start(DetachedThread *thread, int priority = 0)
1831  {thread->start(priority);}
1832 
1837 
1842 
1847 
1851 typedef Mutex mutex_t;
1852 
1857 
1862 
1867 
1872 
1877 inline void wait(barrier_t &barrier)
1878  {barrier.wait();}
1879 
1885 inline void wait(semaphore_t &semaphore, timeout_t timeout = Timer::inf)
1886  {semaphore.wait(timeout);}
1887 
1892 inline void release(semaphore_t &semaphore)
1893  {semaphore.release();}
1894 
1899 inline void acquire(mutex_t &mutex)
1900  {mutex.lock();}
1901 
1906 inline void release(mutex_t &mutex)
1907  {mutex.release();}
1908 
1913 inline void modify(accesslock_t &lock)
1914  {lock.modify();}
1915 
1920 inline void access(accesslock_t &lock)
1921  {lock.access();}
1922 
1928  {lock.release();}
1929 
1935 inline void commit(accesslock_t &lock)
1936  {lock.commit();}
1937 
1943  {lock.exclusive();}
1944 
1949 inline void share(condlock_t &lock)
1950  {lock.share();}
1951 
1956 inline void modify(condlock_t &lock)
1957  {lock.modify();}
1958 
1964 inline void commit(condlock_t &lock)
1965  {lock.commit();}
1966 
1971 inline void access(condlock_t &lock)
1972  {lock.access();}
1973 
1978 inline void release(condlock_t &lock)
1979  {lock.release();}
1980 
1986 inline bool exclusive(rwlock_t &lock, timeout_t timeout = Timer::inf)
1987  {return lock.modify(timeout);}
1988 
1994 inline bool share(rwlock_t &lock, timeout_t timeout = Timer::inf)
1995  {return lock.access(timeout);}
1996 
2001 inline void release(rwlock_t &lock)
2002  {lock.release();}
2003 
2008 inline void lock(rexlock_t &lock)
2009  {lock.lock();}
2010 
2015 inline void release(rexlock_t &lock)
2016  {lock.release();}
2017 
2018 inline bool _sync_protect_(const void *obj)
2019 {
2020  Mutex::protect(obj);
2021  return true;
2022 }
2023 
2024 inline bool _sync_release_(const void *obj)
2025 {
2026  Mutex::release(obj);
2027  return false;
2028 }
2029 
2030 inline bool _rw_reader_(const void *obj)
2031 {
2032  ThreadLock::reader(obj);
2033  return true;
2034 }
2035 
2036 inline bool _rw_writer_(const void *obj)
2037 {
2038  ThreadLock::writer(obj);
2039  return true;
2040 }
2041 
2042 inline bool _rw_release_(const void *obj)
2043 {
2044  ThreadLock::release(obj);
2045  return false;
2046 }
2047 
2048 #define ENTER_EXCLUSIVE \
2049  do { static pthread_mutex_t __sync__ = PTHREAD_MUTEX_INITIALIZER; \
2050  pthread_mutex_lock(&__sync__);
2051 
2052 #define LEAVE_EXCLUSIVE \
2053  pthread_mutex_unlock(&__sync__);} while(0);
2054 
2055 #define SYNC(obj) for(bool _sync_flag_ = _sync_protect_(obj); _sync_flag_; _sync_flag_ = _sync_release_(obj))
2056 
2057 #define SHARED(obj) for(bool _sync_flag_ = _rw_reader_(obj); _sync_flag_; _sync_flag_ = _rw_release_(obj))
2058 
2059 #define EXCLUSIVE(obj) for(bool _sync_flag_ = _rw_writer_(obj); _sync_flag_; _sync_flag_ = _rw_release_(obj))
2060 
2061 END_NAMESPACE
2062 
2063 #endif
locked_pointer()
Create an instance of a typed locked pointer.
Definition: thread.h:1657
void wait(void)
Wait at the barrier until the count of threads waiting is reached.
void operator--(void)
Convenience operator to release a counting semaphore.
Definition: thread.h:910
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
Definition: thread.h:1899
An exclusive locking access interface base.
Definition: access.h:95
Semaphore semaphore_t
Convenience type for using counting semaphores.
Definition: thread.h:1866
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it&#39;s protocol.
Definition: access.h:267
void operator=(T *object)
Replace existing object through assignment.
Definition: thread.h:1678
void broadcast(void)
Signal the conditional to release all waiting threads.
Definition: thread.h:172
void wait(void)
Wait until the semphore usage count is less than the thread limit.
void operator=(void *pointer)
Set guard to mutex lock a new object.
Definition: thread.h:983
Templated shared pointer for singleton shared objects of specific type.
Definition: thread.h:1601
An object pointer that uses mutex to assure thread-safe singleton use.
Definition: thread.h:1129
void wait(void)
Wait (block) until signalled.
Definition: thread.h:160
T * dup(void)
Create a duplicate reference counted instance of the current typed object.
Definition: thread.h:1664
A common base class for all managed objects.
Definition: protocols.h:544
An optimized and convertable shared lock.
Definition: thread.h:714
An abstract class for defining classes that operate as a thread.
Definition: thread.h:1246
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:1607
void modify(void)
Acquire write (exclusive modify) lock.
static void sync(Timer &timer)
Sleep current thread until the specified timer expires.
void waitBroadcast(void)
Wait (block) until broadcast.
Definition: thread.h:301
void operator=(const void *pointer)
Set guard to read lock a new object.
Definition: thread.h:607
void access(void)
Access mode shared thread scheduling.
void waitSignal(void)
Wait (block) until signalled.
Definition: thread.h:295
void release(SharedAccess &object)
Convenience function to unlock shared object through it&#39;s protocol.
Definition: access.h:260
Guard class to apply scope based exclusive locking to objects.
Definition: thread.h:567
A child thread object that may be joined by parent.
Definition: thread.h:1380
void unlock(void)
Release acquired lock.
Definition: thread.h:1013
void start(int priority=0)
Start execution of detached context.
ReusableObject * next(ReusableObject *object)
Get next reusable object in the pool.
Definition: thread.h:694
virtual void run(void)=0
Abstract interface for thread context run method.
void unlock(void)
Unlock the conditional&#39;s supporting mutex.
Definition: thread.h:154
void broadcast(void)
Signal the conditional to release all broadcast threads.
Definition: thread.h:314
locked_instance()
Construct empty locked instance of typed object.
Definition: thread.h:1702
void release(void)
Release or decrease locking.
Auto-pointer support class for shared singleton objects.
Definition: thread.h:1543
Timer class to use when scheduling realtime events.
Definition: timers.h:49
A templated smart pointer instance for shared singleton typed objects.
Definition: thread.h:1738
void signal(void)
Signal the conditional to release one signalled thread.
Definition: thread.h:308
Auto-pointer support class for locked objects.
Definition: thread.h:1490
locked_instance(locked_pointer< T > &pointer)
Construct locked instance of typed object from matching locked_pointer.
Definition: thread.h:1708
Typed smart locked pointer class.
Definition: thread.h:1781
static pthread_condattr_t * initializer(void)
Support function for getting conditional attributes for realtime scheduling.
Definition: thread.h:193
Generic non-recursive exclusive lock class.
Definition: thread.h:927
void lock(void)
Lock the conditional&#39;s supporting mutex.
Definition: thread.h:148
void start(int priority=0)
Start execution of child context.
An exclusive locking protocol interface base.
Definition: access.h:68
Generic smart pointer class.
Definition: generics.h:56
void operator=(const void *pointer)
Set guard to read lock a new object.
Definition: thread.h:556
void background(void)
Start execution of child context as background thread.
Definition: thread.h:1429
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1877
void access(void)
Acquire access (shared read) lock.
Runtime functions.
ConditionalLock condlock_t
Convenience type for using conditional locks.
Definition: thread.h:1836
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Definition: thread.h:1822
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:54
mutex_pointer()
Create a pointer with no reference.
Definition: thread.h:1787
shared_instance(shared_pointer< T > &pointer)
Construct shared access instance of shared typed singleton from matching shared_pointer.
Definition: thread.h:1751
The shared pointer is used to manage a singleton instance of shared object.
Definition: thread.h:1202
The conditional rw seperates scheduling for optizming behavior or rw locks.
Definition: thread.h:213
mutex_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition: thread.h:1793
void lock(void)
Acquire mutex lock.
Definition: thread.h:1007
T & operator*() const
Reference object we are pointing to through pointer indirection.
Definition: thread.h:1799
A portable implimentation of &quot;barrier&quot; thread sychronization.
Definition: thread.h:788
void replace(T *object)
Replace existing typed singleton instance with new one.
Definition: thread.h:1625
virtual void _share(void)=0
Access interface to share lock the object.
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it&#39;s protocol.
Definition: access.h:246
void release(void)
Release the semaphore after waiting for it.
Class for resource bound memory pools between threads.
Definition: thread.h:678
A mutex locked object smart pointer helper class.
Definition: thread.h:1066
void signal(void)
Signal the conditional to release one waiting thread.
Definition: thread.h:166
virtual void exclusive(void)
Convert read lock into exclusive (write/modify) access.
void commit(accesslock_t &lock)
Convenience function to commit an exclusive access lock.
Definition: thread.h:1935
void operator++(void)
Convenience operator to wait on a counting semaphore.
Definition: thread.h:904
Guard class to apply scope based mutex locking to objects.
Definition: thread.h:943
Locking protocol classes for member function automatic operations.
Guard class to apply scope based access locking to objects.
Definition: thread.h:516
static void release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
void replace(T *object)
Replace existing typed object with a new one for next request.
Definition: thread.h:1671
A portable counting semaphore class.
Definition: thread.h:853
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:543
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
A detached thread object that is stand-alone.
Definition: thread.h:1440
Mutex mutex_t
Convenience type for using exclusive mutex locks.
Definition: thread.h:1851
static void release(pthread_mutex_t *lock)
Convenience function to release os native mutex lock directly.
Definition: thread.h:1033
void lock(void)
Acquire or increase locking.
void release(void)
Release access mode read scheduling.
void modify(void)
Exclusive mode write thread scheduling.
Event notification to manage scheduled realtime threads.
Definition: thread.h:365
const T & operator*() const
Access shared typed singleton object this instance locks and references.
Definition: thread.h:1756
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
Definition: linked.h:164
SharedPointer * ptr
Shared lock for protected singleton.
Definition: thread.h:1546
Templated locked pointer for referencing locked objects of specific type.
Definition: thread.h:1651
static const time_t reset
A value to use when resetting.
Definition: timers.h:83
ConditionalAccess accesslock_t
Convenience type for scheduling access.
Definition: thread.h:1841
virtual bool is_active(void)
Check if running.
Shared singleton object.
Definition: thread.h:1170
The conditional is a common base for other thread synchronizing classes.
Definition: thread.h:86
virtual void exit(void)
Exit the thread context.
shared_instance()
Construct empty instance to reference shared typed singleton.
Definition: thread.h:1744
T & operator*() const
Extract instance of locked typed object by pointer reference.
Definition: thread.h:1714
void commit(void)
Complete exclusive mode write scheduling.
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
Definition: thread.h:1861
Reusable objects for forming private heaps.
Definition: linked.h:152
T * operator->() const
Access member of instance of locked typed object by member reference.
Definition: thread.h:1721
void operator=(ObjectProtocol *object)
Replace existing object through assignment.
Definition: thread.h:1158
ThreadLock rwlock_t
Convenience type for using read/write locks.
Definition: thread.h:1856
void acquire(void)
Acquire mutex lock.
Definition: thread.h:1001
Portable recursive exclusive lock.
Definition: thread.h:454
A general purpose smart pointer helper class.
Definition: object.h:132
T * operator*()
Access shared lock typed singleton object by pointer reference.
Definition: thread.h:1639
Private heaps, pools, and associations.
const T * operator->() const
Access member of shared typed singleton object this instance locks and references.
Definition: thread.h:1763
void unlock(void)
Unlock the conditional&#39;s supporting mutex.
Definition: thread.h:289
A generic and portable implimentation of Read/Write locking.
Definition: thread.h:498
void commit(void)
Commit changes / release a modify lock.
T * operator*()
Create a duplicate reference counted instance of the current typed object by pointer reference...
Definition: thread.h:1686
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it&#39;s protocol.
Definition: access.h:274
bool operator!() const
Test if the pointer is not set.
Definition: thread.h:1099
T * operator->() const
Reference member of object we are pointing to.
Definition: thread.h:1806
barrier barrier_t
Convenience type for using thread barriers.
Definition: thread.h:1871
void operator=(T *object)
Replace existing typed singleton object through assignment.
Definition: thread.h:1632
A templated smart pointer instance for lock protected objects.
Definition: thread.h:1696
void release(void)
Release a shared lock.
TimedEvent timedevent_t
Convenience type for using timed events.
Definition: thread.h:1846
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
Definition: thread.h:1913
void lock(void)
Lock the conditional&#39;s supporting mutex.
Definition: thread.h:283
Realtime timers and timer queues.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:478
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it&#39;s protocol.
Definition: access.h:253
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:420
bool modify(timeout_t timeout=Timer::inf)
Request modify (write) access through the lock.
static void acquire(pthread_mutex_t *lock)
Convenience function to acquire os native mutex lock directly.
Definition: thread.h:1026
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it&#39;s protocol.
Definition: access.h:239
ObjectProtocol * object
locked object protected by locked_release
Definition: thread.h:1493
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
Definition: thread.h:259
const T * dup(void)
Acquire a shared (duplocate) reference to the typed singleton object.
Definition: thread.h:1616
void release(void)
Release acquired lock.
Definition: thread.h:1019