ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 
43 #ifndef _UCOMMON_THREAD_H_
44 #define _UCOMMON_THREAD_H_
45 
46 #ifndef _UCOMMON_CPR_H_
47 #include <ucommon/cpr.h>
48 #endif
49 
50 #ifndef _UCOMMON_ACCESS_H_
51 #include <ucommon/access.h>
52 #endif
53 
54 #ifndef _UCOMMON_TIMERS_H_
55 #include <ucommon/timers.h>
56 #endif
57 
58 #ifndef _UCOMMON_MEMORY_H_
59 #include <ucommon/memory.h>
60 #endif
61 
62 #ifndef _UCOMMON_CONDITION_H_
63 #include <ucommon/condition.h>
64 #endif
65 
66 namespace ucommon {
67 
84 {
85 private:
87 
88 protected:
89  unsigned writers;
90  pthread_t writeid;
91 
92  virtual void _share(void) __OVERRIDE;
93 
94  virtual void _lock(void) __OVERRIDE;
95 
96  virtual void _unlock(void) __OVERRIDE;
97 
98  virtual void _unshare(void) __OVERRIDE;
99 
100 public:
102 
104 
113  {
114  private:
115  const void *object;
116 
118 
119  public:
124  reader();
125 
130  reader(const void *object);
131 
135  ~reader();
136 
142  void set(const void *object);
143 
147  void release(void);
148 
154  inline void operator=(const void *pointer) {
155  set(pointer);
156  }
157 
165  static bool lock(const void *object, timeout_t timeout = Timer::inf);
166  };
167 
176  {
177  private:
178  const void *object;
179 
181 
182  public:
187  writer();
188 
193  writer(const void *object);
194 
198  ~writer();
199 
205  void set(const void *object);
206 
210  void release(void);
211 
217  inline void operator=(const void *pointer) {
218  set(pointer);
219  }
220 
228  static bool lock(const void *object, timeout_t timeout = Timer::inf);
229  };
230 
234  RWLock();
235 
241  bool modify(timeout_t timeout = Timer::inf);
242 
248  bool access(timeout_t timeout = Timer::inf);
249 
256  static void indexing(unsigned size);
257 
262  static bool release(const void *object);
263 
267  void release(void);
268 };
269 
278 class __EXPORT TimedEvent : public Timer
279 {
280 private:
281 #ifdef _MSTHREADS_
282  HANDLE event;
283 #else
284  mutable pthread_cond_t cond;
285  bool signalled;
286 #endif
287  mutable pthread_mutex_t mutex;
288 
290 
291 protected:
296  void lock(void);
297 
302  void release(void);
303 
311  bool sync(void);
312 
313 public:
317  TimedEvent(void);
318 
324 
329  TimedEvent(time_t timeout);
330 
334  ~TimedEvent();
335 
341  void signal(void);
342 
349  bool wait(timeout_t timeout);
350 
354  void wait(void);
355 
359  void reset(void);
360 };
361 
370 {
371 private:
373 
374 protected:
375  unsigned waiting;
376  unsigned lockers;
377  pthread_t locker;
378 
379  virtual void _lock(void) __OVERRIDE;
380  virtual void _unlock(void) __OVERRIDE;
381 
382 public:
384 
388  RecursiveMutex();
389 
393  void lock(void);
394 
398  bool lock(timeout_t timeout);
399 
403  void release(void);
404 };
405 
417 {
418 private:
420 
421 protected:
423  unsigned waiting;
424 
429 
435  inline ReusableObject *next(ReusableObject *object) {
436  return object->getNext();
437  }
438 
443  void release(ReusableObject *object);
444 };
445 
460 {
461 private:
463 
464 protected:
465  mutable pthread_mutex_t mlock;
466 
467  virtual void _lock(void) __OVERRIDE;
468  virtual void _unlock(void) __OVERRIDE;
469 
470 public:
472 
476  Mutex();
477 
481  ~Mutex();
482 
486  inline void acquire(void) {
487  pthread_mutex_lock(&mlock);
488  }
489 
493  inline void lock(void) {
494  pthread_mutex_lock(&mlock);
495  }
496 
500  inline void unlock(void) {
501  pthread_mutex_unlock(&mlock);
502  }
503 
507  inline void release(void) {
508  pthread_mutex_unlock(&mlock);
509  }
510 
515  inline static void acquire(pthread_mutex_t *lock) {
516  pthread_mutex_lock(lock);
517  }
518 
523  inline static void release(pthread_mutex_t *lock) {
524  pthread_mutex_unlock(lock);
525  }
526 
533  static void indexing(unsigned size);
534 
540  static bool protect(const void *pointer);
541 
546  static bool release(const void *pointer);
547 };
548 
557 {
558 private:
559 
561 
562 protected:
563  const void *object;
564 
569  AutoProtect();
570 
576  void set(const void *object);
577 
581  void release(void);
582 
583 public:
588  AutoProtect(const void *object);
589 
593  ~AutoProtect();
594 
595  inline operator bool() const {
596  return object != NULL;
597  }
598 
599  inline bool operator!() const {
600  return object == NULL;
601  }
602 };
603 
604 template<typename T>
605 class autoprotect : public AutoProtect
606 {
607 public:
608  inline autoprotect() : AutoProtect() {};
609 
610  inline autoprotect(const T *object) : AutoProtect(object) {};
611 
612  inline void set(const T *object) {
613  AutoProtect::set(object);
614  }
615 
616  inline void release() {
618  }
619 
620  inline autoprotect& operator=(const T* object) {
621  AutoProtect::set(object);
622  return *this;
623  }
624 
625  inline T* operator->() const {
626  return static_cast<T*>(object);
627  }
628 
629  inline T& operator*() const {
630  __THROW_DEREF(object);
631  return *(static_cast<T*>(object));
632  }
633 };
634 
646 {
647 private:
649 
650 protected:
651 // may be used in future if we need cancelable threads...
652 #ifdef _MSTHREADS_
653  HANDLE cancellor;
654 #else
655  void *cancellor;
656 #endif
657 
658  enum {R_UNUSED} reserved; // cancel mode?
659  pthread_t tid;
661  int priority;
662 
668  Thread(size_t stack = 0);
669 
674  void map(void);
675 
679  virtual bool is_active(void) const;
680 
681 public:
682  class __EXPORT Local : public LinkedObject
683  {
684  private:
685  friend class Thread;
686 
687  pthread_key_t key;
689 
691 
692  protected:
693  Local();
694 
695  virtual void release(void *instance) = 0;
696 
697  virtual void *allocate();
698 
699  public:
700  ~Local();
701 
702  void *operator*();
703 
704  void set(void *instance);
705 
706  void *get(void);
707 
708  inline void clear() {
709  set(nullptr);
710  }
711  };
712 
719  void setPriority(void);
720 
725  static void yield(void);
726 
731  static void sleep(timeout_t timeout);
732 
739  static Thread *get(void);
740 
744  virtual void run(void) = 0;
745 
749  virtual ~Thread();
750 
759  virtual void exit(void);
760 
764  static void init(void);
765 
769  static size_t cache(void);
770 
776  static void policy(int polid);
777 
782  static void concurrency(int level);
783 
790  static bool equal(pthread_t thread1, pthread_t thread2);
791 
796  static pthread_t self(void);
797 
798  inline operator bool() const {
799  return is_active();
800  }
801 
802  inline bool operator!() const {
803  return !is_active();
804  }
805 
806  inline bool isRunning(void) const {
807  return is_active();
808  }
809 
810  static void release(void);
811 };
812 
824 {
825 private:
827 
828 protected:
829 #ifdef _MSTHREADS_
830  HANDLE running;
831 #else
832  volatile bool running;
833 #endif
834  volatile bool joining;
835 
840  JoinableThread(size_t size = 0);
841 
846  virtual ~JoinableThread();
847 
853  void join(void);
854 
855  bool is_active(void) const __OVERRIDE;
856 
857  virtual void run(void) __OVERRIDE = 0;
858 
859 public:
860 
869  void start(int priority = 0);
870 
875  inline void background(void) {
876  start(-1);
877  }
878 };
879 
888 {
889 private:
891 
892 protected:
893  bool active;
894 
899  DetachedThread(size_t size = 0);
900 
906  ~DetachedThread();
907 
916  void exit(void) __OVERRIDE;
917 
918  bool is_active(void) const __OVERRIDE;
919 
920  virtual void run(void) __OVERRIDE = 0;
921 
922 public:
929  void start(int priority = 0);
930 };
931 
936 
940 typedef Mutex mutex_t;
941 
945 typedef RWLock rwlock_t;
946 
951 
952 #define __AUTOLOCK(x) autolock __autolock__(x)
953 #define __AUTOPROTECT(x) AutoProtect __autolock__(x)
954 #define __SYNC(x) for(bool _sync_flag_ = Mutex::protect(x); _sync_flag_; _sync_flag_ = !Mutex::release(x))
955 
956 } // namespace ucommon
957 
958 #endif
ucommon::autoprotect::release
void release()
Definition: thread.h:616
ucommon::Timer
Definition: timers.h:50
stacksize_t
size_t stacksize_t
Definition: platform.h:413
ucommon::rexlock_t
RecursiveMutex rexlock_t
Definition: thread.h:950
ucommon::ReusableAllocator::freelist
ReusableObject * freelist
Definition: thread.h:422
ucommon
Definition: access.cpp:23
init
T * init(T *memory)
Definition: platform.h:551
ucommon::autoshared
Definition: access.h:293
ucommon::RWLock
Definition: thread.h:83
ucommon::autoprotect::autoprotect
autoprotect()
Definition: thread.h:608
ucommon::ReusableObject
Definition: linked.h:152
ucommon::RWLock::writer
Definition: thread.h:175
ost::mlock
static pthread_mutex_t mlock
Definition: thread.cpp:321
ucommon::Thread::Local::key
pthread_key_t key
Definition: thread.h:687
__OVERRIDE
#define __OVERRIDE
Definition: platform.h:158
ucommon::AutoProtect::object
const void * object
Definition: thread.h:563
timeout_t
unsigned long timeout_t
Definition: platform.h:453
ucommon::rwlock_t
RWLock rwlock_t
Definition: thread.h:945
ucommon::JoinableThread::running
volatile bool running
Definition: thread.h:832
ucommon::RWLock::reader
Definition: thread.h:112
ucommon::timedevent_t
TimedEvent timedevent_t
Definition: thread.h:935
ucommon::ReusableAllocator::waiting
unsigned waiting
Definition: thread.h:423
ucommon::DetachedThread::active
bool active
Definition: thread.h:893
ucommon::Thread::isRunning
bool isRunning(void) const
Definition: thread.h:806
ucommon::mutex_t
Mutex mutex_t
Definition: thread.h:940
ucommon::Mutex::acquire
static void acquire(pthread_mutex_t *lock)
Definition: thread.h:515
ucommon::autoexclusive
Definition: access.h:282
ucommon::DetachedThread
Definition: thread.h:887
condition.h
ucommon::Mutex::lock
void lock(void)
Definition: thread.h:493
ucommon::Mutex::acquire
void acquire(void)
Definition: thread.h:486
ucommon::RWLock::writer::operator=
void operator=(const void *pointer)
Definition: thread.h:217
ucommon::ConditionalAccess
Definition: condition.h:337
ucommon::Mutex::release
static void release(pthread_mutex_t *lock)
Definition: thread.h:523
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::RWLock::writeid
pthread_t writeid
Definition: thread.h:90
ucommon::Mutex::release
void release(void)
Definition: thread.h:507
ucommon::Mutex::mlock
pthread_mutex_t mlock
Definition: thread.h:465
ucommon::autoprotect
Definition: thread.h:605
ucommon::AutoProtect::set
void set(const void *object)
Definition: thread.cpp:437
ucommon::autoprotect::autoprotect
autoprotect(const T *object)
Definition: thread.h:610
ucommon::JoinableThread::joining
volatile bool joining
Definition: thread.h:834
ucommon::Thread::operator!
bool operator!() const
Definition: thread.h:802
ucommon::JoinableThread::background
void background(void)
Definition: thread.h:875
ucommon::AutoProtect::operator!
bool operator!() const
Definition: thread.h:599
ucommon::TimedEvent::signalled
bool signalled
Definition: thread.h:285
ucommon::Thread::cancellor
void * cancellor
Definition: thread.h:655
ucommon::RecursiveMutex::locker
pthread_t locker
Definition: thread.h:377
ucommon::RWLock::autoreader
autoshared< RWLock > autoreader
Definition: thread.h:101
ucommon::pointer
Definition: generics.h:54
cpr.h
ucommon::RWLock::autowriter
autoexclusive< RWLock > autowriter
Definition: thread.h:103
ucommon::autoprotect::operator->
T * operator->() const
Definition: thread.h:625
ucommon::Thread::tid
pthread_t tid
Definition: thread.h:659
ucommon::RWLock::reader::object
const void * object
Definition: thread.h:115
__DELETE_COPY
#define __DELETE_COPY(x)
Definition: platform.h:160
ucommon::Thread::Local::clear
void clear()
Definition: thread.h:708
ucommon::RWLock::writers
unsigned writers
Definition: thread.h:89
ucommon::RWLock::reader::operator=
void operator=(const void *pointer)
Definition: thread.h:154
ucommon::RWLock::writer::object
const void * object
Definition: thread.h:178
ucommon::LinkedObject
Definition: linked.h:55
timers.h
ucommon::autoprotect::operator*
T & operator*() const
Definition: thread.h:629
ucommon::RecursiveMutex::lockers
unsigned lockers
Definition: thread.h:376
ucommon::Thread::priority
int priority
Definition: thread.h:661
ucommon::ReusableAllocator::next
ReusableObject * next(ReusableObject *object)
Definition: thread.h:435
timeout
static shell::numericopt timeout('t', "--timeout", _TEXT("optional keyboard input timeout"), "seconds", 0)
ucommon::autoprotect::operator=
autoprotect & operator=(const T *object)
Definition: thread.h:620
ucommon::autoprotect::set
void set(const T *object)
Definition: thread.h:612
ucommon::AutoProtect::release
void release(void)
Definition: thread.cpp:445
ucommon::ExclusiveProtocol
Definition: access.h:55
access.h
ucommon::Mutex::autolock
autoexclusive< Mutex > autolock
Definition: thread.h:471
ucommon::Thread::Local::list
static LinkedObject * list
Definition: thread.h:688
__PROTOCOL
#define __PROTOCOL
Definition: platform.h:112
ucommon::RecursiveMutex::waiting
unsigned waiting
Definition: thread.h:375
ucommon::RecursiveMutex
Definition: thread.h:369
ucommon::Timer::inf
static const timeout_t inf
Definition: timers.h:80
ucommon::Conditional
Definition: condition.h:227
ucommon::ReusableAllocator
Definition: thread.h:416
ucommon::TimedEvent::cond
pthread_cond_t cond
Definition: thread.h:284
ucommon::SharedProtocol
Definition: access.h:122
__THROW_DEREF
#define __THROW_DEREF(v)
Definition: platform.h:53
ucommon::AutoProtect
Definition: thread.h:556
ucommon::TimedEvent
Definition: thread.h:278
ucommon::RecursiveMutex::autolock
autoexclusive< RecursiveMutex > autolock
Definition: thread.h:383
ucommon::Mutex::unlock
void unlock(void)
Definition: thread.h:500
ucommon::Mutex
Definition: thread.h:459
memory.h
ucommon::Thread::stack
stacksize_t stack
Definition: thread.h:660
ucommon::JoinableThread
Definition: thread.h:823
ucommon::TimedEvent::mutex
pthread_mutex_t mutex
Definition: thread.h:287
ucommon::Thread
Definition: thread.h:645
ucommon::Thread::Local
Definition: thread.h:682
ucommon::ReusableObject::getNext
ReusableObject * getNext(void)
Definition: linked.h:164