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  mutable CRITICAL_SECTION mutex;
93  mutable CONDITION_VARIABLE cond;
94 #elif defined(_MSTHREADS_)
95  enum {SIGNAL = 0, BROADCAST = 1};
96  HANDLE events[2];
97  unsigned waiting;
98  mutable CRITICAL_SECTION mlock;
99  mutable 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  mutable pthread_cond_t cond;
113  mutable 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 _MSTHREADS_
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  friend class autolock;
188 
189  class __EXPORT autolock
190  {
191  private:
192 #ifdef _MSTHREADS_
193  CRITICAL_SECTION *mutex;
194 #else
195  pthread_mutex_t *mutex;
196 #endif
197 
198  public:
199  inline autolock(const Conditional* object) {
200  mutex = &object->mutex;
201 #ifdef _MSTHREADS_
202  EnterCriticalSection(mutex);
203 #else
204  pthread_mutex_lock(mutex);
205 #endif
206  }
207 
208  inline ~autolock() {
209 #ifdef _MSTHREADS_
210  LeaveCriticalSection(mutex);
211 #else
212  pthread_mutex_unlock(mutex);
213 #endif
214  }
215  };
216 
217 #if !defined(_MSTHREADS_) && !defined(__PTH__)
218 
223  static inline pthread_condattr_t *initializer(void)
224  {return &attr.attr;}
225 #endif
226 
233  static void set(struct timespec *hires, timeout_t timeout);
234 };
235 
243 class __EXPORT ConditionalAccess : private Conditional
244 {
245 protected:
246 #if defined _MSCONDITIONAL_
247  CONDITION_VARIABLE bcast;
248 #elif !defined(_MSTHREADS_)
249  mutable pthread_cond_t bcast;
250 #endif
251 
252  unsigned pending, waiting, sharing;
253 
259  bool waitSignal(timeout_t timeout);
260 
266  bool waitBroadcast(timeout_t timeout);
267 
268 
274  bool waitSignal(struct timespec *timeout);
275 
281  bool waitBroadcast(struct timespec *timeout);
282 
289  inline static void set(struct timespec *hires, timeout_t timeout)
290  {Conditional::set(hires, timeout);}
291 
292 
293 #ifdef _MSTHREADS_
294  inline void lock(void)
295  {EnterCriticalSection(&mutex);};
296 
297  inline void unlock(void)
298  {LeaveCriticalSection(&mutex);};
299 
300  void waitSignal(void);
301  void waitBroadcast(void);
302 
303  inline void signal(void)
304  {Conditional::signal();};
305 
306  inline void broadcast(void)
308 
309 #else
310 
313  inline void lock(void)
314  {pthread_mutex_lock(&mutex);}
315 
319  inline void unlock(void)
320  {pthread_mutex_unlock(&mutex);}
321 
325  inline void waitSignal(void)
326  {pthread_cond_wait(&cond, &mutex);}
327 
331  inline void waitBroadcast(void)
332  {pthread_cond_wait(&bcast, &mutex);}
333 
334 
338  inline void signal(void)
339  {pthread_cond_signal(&cond);}
340 
344  inline void broadcast(void)
345  {pthread_cond_broadcast(&bcast);}
346 #endif
347 public:
352 
357 
361  void access(void);
362 
366  void modify(void);
367 
371  void release(void);
372 
376  void commit(void);
377 
384  void limit_sharing(unsigned max);
385 };
386 
395 class __EXPORT TimedEvent : public Timer
396 {
397 private:
398 #ifdef _MSTHREADS_
399  HANDLE event;
400 #else
401  mutable pthread_cond_t cond;
402  bool signalled;
403 #endif
404  mutable pthread_mutex_t mutex;
405 
406 protected:
411  void lock(void);
412 
417  void release(void);
418 
426  bool sync(void);
427 
428 public:
432  TimedEvent(void);
433 
438  TimedEvent(timeout_t timeout);
439 
444  TimedEvent(time_t timeout);
445 
449  ~TimedEvent();
450 
456  void signal(void);
457 
464  bool wait(timeout_t timeout);
465 
469  void wait(void);
470 
474  void reset(void);
475 };
476 
484 class __EXPORT RecursiveMutex : private Conditional, public ExclusiveAccess
485 {
486 protected:
487  unsigned waiting;
488  unsigned lockers;
489  pthread_t locker;
490 
491  virtual void _lock(void);
492  virtual void _unlock(void);
493 
494 public:
498  RecursiveMutex();
499 
503  void lock(void);
504 
508  bool lock(timeout_t timeout);
509 
513  void release(void);
514 };
515 
528 class __EXPORT ThreadLock : private ConditionalAccess, public ExclusiveAccess, public SharedAccess
529 {
530 protected:
531  unsigned writers;
532  pthread_t writeid;
533 
534  virtual void _lock(void);
535  virtual void _share(void);
536  virtual void _unlock(void);
537 
538 public:
546  class __EXPORT guard_reader
547  {
548  private:
549  const void *object;
550 
551  public:
556  guard_reader();
557 
562  guard_reader(const void *object);
563 
567  ~guard_reader();
568 
574  void set(const void *object);
575 
579  void release(void);
580 
586  inline void operator=(const void *pointer)
587  {set(pointer);}
588  };
589 
597  class __EXPORT guard_writer
598  {
599  private:
600  const void *object;
601 
602  public:
607  guard_writer();
608 
613  guard_writer(const void *object);
614 
618  ~guard_writer();
619 
625  void set(const void *object);
626 
630  void release(void);
631 
637  inline void operator=(const void *pointer)
638  {set(pointer);}
639  };
640 
644  ThreadLock();
645 
651  bool modify(timeout_t timeout = Timer::inf);
652 
658  bool access(timeout_t timeout = Timer::inf);
659 
666  static void indexing(unsigned size);
667 
675  static bool writer(const void *object, timeout_t timeout = Timer::inf);
676 
684  static bool reader(const void *object, timeout_t timeout = Timer::inf);
685 
690  static bool release(const void *object);
691 
695  void release(void);
696 };
697 
708 class __EXPORT ReusableAllocator : protected Conditional
709 {
710 protected:
711  ReusableObject *freelist;
712  unsigned waiting;
713 
718 
725  {return object->getNext();}
726 
731  void release(ReusableObject *object);
732 };
733 
744 class __EXPORT ConditionalLock : protected ConditionalAccess, public SharedAccess
745 {
746 protected:
747  class Context : public LinkedObject
748  {
749  public:
750  inline Context(LinkedObject **root) : LinkedObject(root) {}
751 
752  pthread_t thread;
753  unsigned count;
754  };
755 
756  LinkedObject *contexts;
757 
758  virtual void _share(void);
759  virtual void _unlock(void);
760 
761  Context *getContext(void);
762 
763 public:
767  ConditionalLock();
768 
772  ~ConditionalLock();
773 
777  void modify(void);
778 
782  void commit(void);
783 
787  void access(void);
788 
792  void release(void);
793 
798  virtual void exclusive(void);
799 
803  virtual void share(void);
804 };
805 
818 class __EXPORT barrier : private Conditional
819 {
820 private:
821  unsigned count;
822  unsigned waits;
823 
824 public:
829  barrier(unsigned count);
830 
834  ~barrier();
835 
841  void set(unsigned count);
842 
846  void inc(void);
847 
851  void dec(void);
852 
857  unsigned operator++(void);
858 
859  unsigned operator--(void);
860 
864  void wait(void);
865 
872  bool wait(timeout_t timeout);
873 };
874 
883 class __EXPORT Semaphore : public SharedAccess, protected Conditional
884 {
885 protected:
886  unsigned count, waits, used;
887 
888  virtual void _share(void);
889  virtual void _unlock(void);
890 
891 public:
896  Semaphore(unsigned count = 0);
897 
903  Semaphore(unsigned count, unsigned avail);
904 
909  void wait(void);
910 
918  bool wait(timeout_t timeout);
919 
924  void set(unsigned count);
925 
929  void release(void);
930 
934  inline void operator++(void)
935  {wait();}
936 
940  inline void operator--(void)
941  {release();}
942 };
943 
957 class __EXPORT Mutex : public ExclusiveAccess
958 {
959 protected:
960  mutable pthread_mutex_t mlock;
961 
962  virtual void _lock(void);
963  virtual void _unlock(void);
964 
965 public:
966  friend class autolock;
967 
968  class __EXPORT autolock
969  {
970  private:
971  pthread_mutex_t *mutex;
972 
973  public:
974  inline autolock(const Mutex *object) {
975  mutex = &object->mlock;
976  pthread_mutex_lock(this->mutex);
977  }
978 
979  inline ~autolock() {
980  pthread_mutex_unlock(this->mutex);
981  }
982  };
983 
991  class __EXPORT guard
992  {
993  private:
994  const void *object;
995 
996  public:
1001  guard();
1002 
1007  guard(const void *object);
1008 
1012  ~guard();
1013 
1019  void set(const void *object);
1020 
1024  void release(void);
1025 
1031  inline void operator=(void *pointer)
1032  {set(pointer);}
1033  };
1034 
1035 
1039  Mutex();
1040 
1044  ~Mutex();
1045 
1049  inline void acquire(void)
1050  {pthread_mutex_lock(&mlock);}
1051 
1055  inline void lock(void)
1056  {pthread_mutex_lock(&mlock);}
1057 
1061  inline void unlock(void)
1062  {pthread_mutex_unlock(&mlock);}
1063 
1067  inline void release(void)
1068  {pthread_mutex_unlock(&mlock);}
1069 
1074  inline static void acquire(pthread_mutex_t *lock)
1075  {pthread_mutex_lock(lock);}
1076 
1081  inline static void release(pthread_mutex_t *lock)
1082  {pthread_mutex_unlock(lock);}
1083 
1090  static void indexing(unsigned size);
1091 
1097  static bool protect(const void *pointer);
1098 
1103  static bool release(const void *pointer);
1104 };
1105 
1114 class __EXPORT auto_protect
1115 {
1116 private:
1117  // cannot copy...
1118  inline auto_protect(const auto_object &pointer) {}
1119 
1120 protected:
1121  const void *object;
1122 
1123  auto_protect();
1124 
1125 public:
1130  auto_protect(const void *object);
1131 
1136  ~auto_protect();
1137 
1141  void release(void);
1142 
1147  inline bool operator!() const
1148  {return object == NULL;}
1149 
1154  inline operator bool() const
1155  {return object != NULL;}
1156 
1163  void operator=(const void *object);
1164 };
1165 
1177 class __EXPORT LockedPointer
1178 {
1179 private:
1180  friend class locked_release;
1181  mutable pthread_mutex_t mutex;
1183 
1184 protected:
1188  LockedPointer();
1189 
1194  void replace(ObjectProtocol *object);
1195 
1200  ObjectProtocol *dup(void);
1201 
1206  inline void operator=(ObjectProtocol *object)
1207  {replace(object);}
1208 };
1209 
1218 class __EXPORT SharedObject
1219 {
1220 protected:
1221  friend class SharedPointer;
1222 
1231  virtual void commit(SharedPointer *pointer);
1232 
1233 public:
1237  virtual ~SharedObject();
1238 };
1239 
1250 class __EXPORT SharedPointer : protected ConditionalAccess
1251 {
1252 private:
1253  friend class shared_release;
1255 
1256 protected:
1260  SharedPointer();
1261 
1265  ~SharedPointer();
1266 
1273  void replace(SharedObject *object);
1274 
1281  SharedObject *share(void);
1282 };
1283 
1294 class __EXPORT Thread
1295 {
1296 protected:
1297 // may be used in future if we need cancelable threads...
1298 #ifdef _MSTHREADS_
1299  HANDLE cancellor;
1300 #else
1301  void *cancellor;
1302 #endif
1303 
1304  enum {R_UNUSED} reserved; // cancel mode?
1305  pthread_t tid;
1306  size_t stack;
1307  int priority;
1308 
1314  Thread(size_t stack = 0);
1315 
1320  void map(void);
1321 
1325  virtual bool is_active(void) const;
1326 
1327 public:
1334  void setPriority(void);
1335 
1340  static void yield(void);
1341 
1346  static void sleep(timeout_t timeout);
1347 
1354  static Thread *get(void);
1355 
1359  virtual void run(void) = 0;
1360 
1364  virtual ~Thread();
1365 
1374  virtual void exit(void);
1375 
1379  static void init(void);
1380 
1386  static void policy(int polid);
1387 
1392  static void concurrency(int level);
1393 
1400  static bool equal(pthread_t thread1, pthread_t thread2);
1401 
1406  static pthread_t self(void);
1407 
1408  inline operator bool() const
1409  {return is_active();}
1410 
1411  inline bool operator!() const
1412  {return !is_active();}
1413 
1414  inline bool isRunning(void) const
1415  {return is_active();}
1416 };
1417 
1428 class __EXPORT JoinableThread : public Thread
1429 {
1430 protected:
1431 #ifdef _MSTHREADS_
1432  HANDLE running;
1433 #else
1434  volatile bool running;
1435 #endif
1436  volatile bool joining;
1437 
1442  JoinableThread(size_t size = 0);
1443 
1448  virtual ~JoinableThread();
1449 
1455  void join(void);
1456 
1457  bool is_active(void) const;
1458 
1459  virtual void run(void) = 0;
1460 
1461 public:
1462 
1471  void start(int priority = 0);
1472 
1477  inline void background(void)
1478  {start(-1);}
1479 };
1480 
1488 class __EXPORT DetachedThread : public Thread
1489 {
1490 protected:
1491  bool active;
1492 
1497  DetachedThread(size_t size = 0);
1498 
1504  ~DetachedThread();
1505 
1514  void exit(void);
1515 
1516  bool is_active(void) const;
1517 
1518  virtual void run(void) = 0;
1519 
1520 public:
1527  void start(int priority = 0);
1528 };
1529 
1538 class __EXPORT locked_release
1539 {
1540 protected:
1546  locked_release();
1547 
1553  locked_release(const locked_release &object);
1554 
1555 public:
1562 
1567  ~locked_release();
1568 
1572  void release(void);
1573 
1579  locked_release &operator=(LockedPointer &pointer);
1580 };
1581 
1591 class __EXPORT shared_release
1592 {
1593 protected:
1599  shared_release();
1600 
1606  shared_release(const shared_release &object);
1607 
1608 public:
1614 
1620  ~shared_release();
1621 
1625  void release(void);
1626 
1631  SharedObject *get(void);
1632 
1638  shared_release &operator=(SharedPointer &pointer);
1639 };
1640 
1648 template<class T>
1650 {
1651 public:
1656 
1664  inline const T *dup(void)
1665  {return static_cast<const T*>(SharedPointer::share());}
1666 
1673  inline void replace(T *object)
1674  {SharedPointer::replace(object);}
1675 
1680  inline void operator=(T *object)
1681  {replace(object);}
1682 
1687  inline T *operator*()
1688  {return dup();}
1689 };
1690 
1698 template<class T>
1700 {
1701 public:
1706 
1712  inline T* dup(void)
1713  {return static_cast<T *>(LockedPointer::dup());}
1714 
1719  inline void replace(T *object)
1720  {LockedPointer::replace(object);}
1721 
1726  inline void operator=(T *object)
1727  {replace(object);}
1728 
1734  inline T *operator*()
1735  {return dup();}
1736 };
1737 
1743 template<class T>
1745 {
1746 public:
1751 
1757 
1762  inline T& operator*() const
1763  {return *(static_cast<T&>(object));}
1764 
1769  inline T* operator->() const
1770  {return static_cast<T*>(object);}
1771 
1776  inline T* get(void) const
1777  {return static_cast<T*>(object);}
1778 };
1779 
1785 template<class T>
1787 {
1788 public:
1793 
1800 
1804  inline const T& operator*() const
1805  {return *(static_cast<const T&>(ptr->pointer));}
1806 
1811  inline const T* operator->() const
1812  {return static_cast<const T*>(ptr->pointer);}
1813 
1818  inline const T* get(void) const
1819  {return static_cast<const T*>(ptr->pointer);}
1820 };
1821 
1828 template <class T>
1830 {
1831 public:
1835  inline mutex_pointer() : auto_protect() {}
1836 
1841  inline mutex_pointer(T* object) : auto_protect(object) {}
1842 
1847  inline T& operator*() const
1848  {return *(static_cast<T&>(auto_protect::object));}
1849 
1854  inline T* operator->() const
1855  {return static_cast<T*>(auto_protect::object);}
1856 
1861  inline T* get(void) const
1862  {return static_cast<T*>(auto_protect::object);}
1863 };
1864 
1870 inline void start(JoinableThread *thread, int priority = 0)
1871  {thread->start(priority);}
1872 
1878 inline void start(DetachedThread *thread, int priority = 0)
1879  {thread->start(priority);}
1880 
1885 
1890 
1895 
1899 typedef Mutex mutex_t;
1900 
1905 
1910 
1915 
1920 
1925 inline void wait(barrier_t &barrier)
1926  {barrier.wait();}
1927 
1933 inline void wait(semaphore_t &semaphore, timeout_t timeout = Timer::inf)
1934  {semaphore.wait(timeout);}
1935 
1940 inline void release(semaphore_t &semaphore)
1941  {semaphore.release();}
1942 
1947 inline void acquire(mutex_t &mutex)
1948  {mutex.lock();}
1949 
1954 inline void release(mutex_t &mutex)
1955  {mutex.release();}
1956 
1961 inline void modify(accesslock_t &lock)
1962  {lock.modify();}
1963 
1968 inline void access(accesslock_t &lock)
1969  {lock.access();}
1970 
1975 inline void release(accesslock_t &lock)
1976  {lock.release();}
1977 
1983 inline void commit(accesslock_t &lock)
1984  {lock.commit();}
1985 
1990 inline void exclusive(condlock_t &lock)
1991  {lock.exclusive();}
1992 
1997 inline void share(condlock_t &lock)
1998  {lock.share();}
1999 
2004 inline void modify(condlock_t &lock)
2005  {lock.modify();}
2006 
2012 inline void commit(condlock_t &lock)
2013  {lock.commit();}
2014 
2019 inline void access(condlock_t &lock)
2020  {lock.access();}
2021 
2026 inline void release(condlock_t &lock)
2027  {lock.release();}
2028 
2034 inline bool exclusive(rwlock_t &lock, timeout_t timeout = Timer::inf)
2035  {return lock.modify(timeout);}
2036 
2042 inline bool share(rwlock_t &lock, timeout_t timeout = Timer::inf)
2043  {return lock.access(timeout);}
2044 
2049 inline void release(rwlock_t &lock)
2050  {lock.release();}
2051 
2056 inline void lock(rexlock_t &lock)
2057  {lock.lock();}
2058 
2063 inline void release(rexlock_t &lock)
2064  {lock.release();}
2065 
2066 #define __AUTOLOCK__ autolock __autolock__(this);
2067 
2068 #define __SYNC__ for(bool _sync_flag_ = Mutex::protect(this); _sync_flag_; _sync_flag_ = !Mutex::release(this))
2069 
2070 #define __SHARED__ for(bool _sync_flag_ = ThreadLock::reader(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
2071 
2072 #define ___EXCLUSIVE__ for(bool _sync_flag_ = ThreadLock::writer(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
2073 
2074 } // namespace ucommon
2075 
2076 #endif
locked_pointer()
Create an instance of a typed locked pointer.
Definition: thread.h:1705
void operator--(void)
Convenience operator to release a counting semaphore.
Definition: thread.h:940
void start(int priority=0)
Start execution of detached context.
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
Definition: thread.h:1947
An exclusive locking access interface base.
Definition: access.h:95
The conditional is a common base for other thread synchronizing classes.
Definition: thread.h:86
Semaphore semaphore_t
Convenience type for using counting semaphores.
Definition: thread.h:1914
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it's protocol.
Definition: access.h:267
void operator=(T *object)
Replace existing object through assignment.
Definition: thread.h:1726
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:1031
Templated shared pointer for singleton shared objects of specific type.
Definition: thread.h:1649
A general purpose smart pointer helper class.
Definition: object.h:132
An object pointer that uses mutex to assure thread-safe singleton use.
Definition: thread.h:1177
T * dup(void)
Create a duplicate reference counted instance of the current typed object.
Definition: thread.h:1712
A common base class for all managed objects.
Definition: protocols.h:544
An abstract class for defining classes that operate as a thread.
Definition: thread.h:1294
shared_pointer()
Created shared locking for typed singleton pointer.
Definition: thread.h:1655
void background(void)
Start execution of child context as background thread.
Definition: thread.h:1477
void operator=(const void *pointer)
Set guard to read lock a new object.
Definition: thread.h:637
void access(void)
Acquire access (shared read) lock.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Definition: access.h:260
void modify(void)
Acquire write (exclusive modify) lock.
SharedObject * share(void)
Acquire a shared reference to the singleton object.
Guard class to apply scope based exclusive locking to objects.
Definition: thread.h:597
void signal(void)
Signal the conditional to release one waiting thread.
Definition: thread.h:166
void unlock(void)
Release acquired lock.
Definition: thread.h:1061
ReusableObject * next(ReusableObject *object)
Get next reusable object in the pool.
Definition: thread.h:724
locked_instance()
Construct empty locked instance of typed object.
Definition: thread.h:1750
Common namespace for all ucommon objects.
Definition: access.h:46
void release(void)
Release or decrease locking.
Auto-pointer support class for shared singleton objects.
Definition: thread.h:1591
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:1786
void commit(void)
Complete exclusive mode write scheduling.
void replace(ObjectProtocol *object)
Replace existing object with a new one for next request.
Auto-pointer support class for locked objects.
Definition: thread.h:1538
locked_instance(locked_pointer< T > &pointer)
Construct locked instance of typed object from matching locked_pointer.
Definition: thread.h:1756
Typed smart locked pointer class.
Definition: thread.h:1829
Generic non-recursive exclusive lock class.
Definition: thread.h:957
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:319
virtual void share(void)
Return an exclusive access lock back to share mode.
Generic smart pointer class.
Definition: generics.h:53
void operator=(const void *pointer)
Set guard to read lock a new object.
Definition: thread.h:586
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
Definition: thread.h:1925
Runtime functions.
bool operator!() const
Test if the pointer is not set.
Definition: thread.h:1147
ConditionalLock condlock_t
Convenience type for using conditional locks.
Definition: thread.h:1884
void broadcast(void)
Signal the conditional to release all waiting threads.
Definition: thread.h:172
ObjectProtocol * dup(void)
Create a duplicate reference counted instance of the current object.
void lock(void)
Lock the conditional's supporting mutex.
Definition: thread.h:148
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Definition: thread.h:1870
Common base class for all objects that can be formed into a linked list.
Definition: linked.h:58
static bool release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
mutex_pointer()
Create a pointer with no reference.
Definition: thread.h:1835
shared_instance(shared_pointer< T > &pointer)
Construct shared access instance of shared typed singleton from matching shared_pointer.
Definition: thread.h:1799
void broadcast(void)
Signal the conditional to release all broadcast threads.
Definition: thread.h:344
The shared pointer is used to manage a singleton instance of shared object.
Definition: thread.h:1250
An optimized and convertable shared lock.
Definition: thread.h:744
mutex_pointer(T *object)
Create a pointer with a reference to a heap object.
Definition: thread.h:1841
virtual void exclusive(void)
Convert read lock into exclusive (write/modify) access.
void lock(void)
Acquire mutex lock.
Definition: thread.h:1055
T & operator*() const
Reference object we are pointing to through pointer indirection.
Definition: thread.h:1847
void replace(T *object)
Replace existing typed singleton instance with new one.
Definition: thread.h:1673
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it'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:708
void commit(accesslock_t &lock)
Convenience function to commit an exclusive access lock.
Definition: thread.h:1983
void operator++(void)
Convenience operator to wait on a counting semaphore.
Definition: thread.h:934
Guard class to apply scope based mutex locking to objects.
Definition: thread.h:991
void waitBroadcast(void)
Wait (block) until broadcast.
Definition: thread.h:331
void commit(void)
Commit changes / release a modify lock.
Locking protocol classes for member function automatic operations.
Guard class to apply scope based access locking to objects.
Definition: thread.h:546
A child thread object that may be joined by parent.
Definition: thread.h:1428
void replace(T *object)
Replace existing typed object with a new one for next request.
Definition: thread.h:1719
A portable counting semaphore class.
Definition: thread.h:883
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Definition: generics.h:540
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
void waitSignal(void)
Wait (block) until signalled.
Definition: thread.h:325
Mutex mutex_t
Convenience type for using exclusive mutex locks.
Definition: thread.h:1899
void start(int priority=0)
Start execution of child context.
static void release(pthread_mutex_t *lock)
Convenience function to release os native mutex lock directly.
Definition: thread.h:1081
void lock(void)
Acquire or increase locking.
Event notification to manage scheduled realtime threads.
Definition: thread.h:395
const T & operator*() const
Access shared typed singleton object this instance locks and references.
Definition: thread.h:1804
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
Definition: linked.h:168
SharedPointer * ptr
Shared lock for protected singleton.
Definition: thread.h:1594
Templated locked pointer for referencing locked objects of specific type.
Definition: thread.h:1699
ConditionalAccess accesslock_t
Convenience type for scheduling access.
Definition: thread.h:1889
Shared singleton object.
Definition: thread.h:1218
The conditional rw seperates scheduling for optizming behavior or rw locks.
Definition: thread.h:243
void lock(void)
Lock the conditional's supporting mutex.
Definition: thread.h:313
shared_instance()
Construct empty instance to reference shared typed singleton.
Definition: thread.h:1792
T & operator*() const
Extract instance of locked typed object by pointer reference.
Definition: thread.h:1762
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
Definition: thread.h:1909
Reusable objects for forming private heaps.
Definition: linked.h:156
T * operator->() const
Access member of instance of locked typed object by member reference.
Definition: thread.h:1769
A portable implimentation of "barrier" thread sychronization.
Definition: thread.h:818
void operator=(ObjectProtocol *object)
Replace existing object through assignment.
Definition: thread.h:1206
ThreadLock rwlock_t
Convenience type for using read/write locks.
Definition: thread.h:1904
void acquire(void)
Acquire mutex lock.
Definition: thread.h:1049
An exclusive locking protocol interface base.
Definition: access.h:68
Portable recursive exclusive lock.
Definition: thread.h:484
static const timeout_t inf
A value to use for infinite time.
Definition: timers.h:82
A mutex locked object smart pointer helper class.
Definition: thread.h:1114
T * operator*()
Access shared lock typed singleton object by pointer reference.
Definition: thread.h:1687
Private heaps, pools, and associations.
void wait(void)
Wait (block) until signalled.
Definition: thread.h:160
const T * operator->() const
Access member of shared typed singleton object this instance locks and references.
Definition: thread.h:1811
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
Definition: thread.h:289
A generic and portable implimentation of Read/Write locking.
Definition: thread.h:528
T * operator*()
Create a duplicate reference counted instance of the current typed object by pointer reference...
Definition: thread.h:1734
A detached thread object that is stand-alone.
Definition: thread.h:1488
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it's protocol.
Definition: access.h:274
static pthread_condattr_t * initializer(void)
Support function for getting conditional attributes for realtime scheduling.
Definition: thread.h:223
void unlock(void)
Unlock the conditional's supporting mutex.
Definition: thread.h:154
T * operator->() const
Reference member of object we are pointing to.
Definition: thread.h:1854
void release(void)
Release a shared lock.
void release(void)
Release access mode read scheduling.
void modify(void)
Exclusive mode write thread scheduling.
barrier barrier_t
Convenience type for using thread barriers.
Definition: thread.h:1919
void operator=(T *object)
Replace existing typed singleton object through assignment.
Definition: thread.h:1680
A templated smart pointer instance for lock protected objects.
Definition: thread.h:1744
TimedEvent timedevent_t
Convenience type for using timed events.
Definition: thread.h:1894
void signal(void)
Signal the conditional to release one signalled thread.
Definition: thread.h:338
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
Definition: thread.h:1961
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
Realtime timers and timer queues.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
Definition: generics.h:475
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Definition: access.h:253
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:431
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:1074
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:334
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it's protocol.
Definition: access.h:239
void access(void)
Access mode shared thread scheduling.
ObjectProtocol * object
locked object protected by locked_release
Definition: thread.h:1541
const T * dup(void)
Acquire a shared (duplocate) reference to the typed singleton object.
Definition: thread.h:1664
void replace(SharedObject *object)
Replace existing singleton instance with new one.
void release(void)
Release acquired lock.
Definition: thread.h:1067