53 #ifndef _UCOMMON_THREAD_H_
54 #define _UCOMMON_THREAD_H_
56 #ifndef _UCOMMON_CPR_H_
60 #ifndef _UCOMMON_ACCESS_H_
64 #ifndef _UCOMMON_TIMERS_H_
68 #ifndef _UCOMMON_MEMORY_H_
91 #if defined(_MSCONDITIONAL_)
92 CRITICAL_SECTION mutex;
93 CONDITION_VARIABLE cond;
94 #elif defined(_MSWINDOWS_)
95 enum {SIGNAL = 0, BROADCAST = 1};
98 CRITICAL_SECTION mlock;
99 CRITICAL_SECTION mutex;
102 class __LOCAL attribute
105 pthread_condattr_t attr;
109 __LOCAL
static attribute attr;
113 pthread_mutex_t mutex;
131 bool wait(
struct timespec *timeout);
134 inline void lock(
void)
135 {EnterCriticalSection(&mutex);};
138 {LeaveCriticalSection(&mutex);};
142 void broadcast(
void);
149 {pthread_mutex_lock(&mutex);};
155 {pthread_mutex_unlock(&mutex);};
161 {pthread_cond_wait(&cond, &mutex);};
167 {pthread_cond_signal(&cond);};
173 {pthread_cond_broadcast(&cond);};
187 #if !defined(_MSWINDOWS_) && !defined(__PTH__)
194 {
return &attr.attr;};
203 static void set(
struct timespec *hires,
timeout_t timeout);
216 #if defined _MSCONDITIONAL_
217 CONDITION_VARIABLE bcast;
218 #elif !defined(_MSWINDOWS_)
219 pthread_cond_t bcast;
222 unsigned pending, waiting, sharing;
244 bool waitSignal(
struct timespec *timeout);
251 bool waitBroadcast(
struct timespec *timeout);
264 inline void lock(
void)
265 {EnterCriticalSection(&mutex);};
268 {LeaveCriticalSection(&mutex);};
270 void waitSignal(
void);
271 void waitBroadcast(
void);
284 {pthread_mutex_lock(&mutex);};
290 {pthread_mutex_unlock(&mutex);};
296 {pthread_cond_wait(&cond, &mutex);};
302 {pthread_cond_wait(&bcast, &mutex);};
309 {pthread_cond_signal(&cond);};
315 {pthread_cond_broadcast(&bcast);};
354 void limit_sharing(
unsigned max);
374 pthread_mutex_t mutex;
461 virtual void _lock(
void);
462 virtual void _unlock(
void);
504 virtual void _lock(
void);
505 virtual void _share(
void);
506 virtual void _unlock(
void);
544 void set(
const void *
object);
595 void set(
const void *
object);
636 static void indexing(
unsigned size);
645 static bool writer(
const void *
object,
timeout_t timeout = Timer::inf);
654 static bool reader(
const void *
object,
timeout_t timeout = Timer::inf);
660 static void release(
const void *
object);
728 virtual void _share(
void);
729 virtual void _unlock(
void);
731 Context *getContext(
void);
773 virtual void share(
void);
811 void set(
unsigned count);
827 unsigned operator++(
void);
829 unsigned operator--(
void);
856 unsigned count, waits, used;
858 virtual void _share(
void);
859 virtual void _unlock(
void);
886 void set(
unsigned count);
922 pthread_mutex_t mlock;
924 virtual void _lock(
void);
925 virtual void _unlock(
void);
951 guard(
const void *
object);
963 void set(
const void *
object);
994 {pthread_mutex_lock(&mlock);};
1000 {pthread_mutex_lock(&mlock);};
1006 {pthread_mutex_unlock(&mlock);};
1012 {pthread_mutex_unlock(&mlock);};
1019 {pthread_mutex_lock(lock);};
1026 {pthread_mutex_unlock(lock);};
1034 static void indexing(
unsigned size);
1041 static void protect(
const void *
pointer);
1092 {
return object == NULL;};
1098 inline operator bool()
const
1099 {
return object != NULL;};
1107 void operator=(
const void *
object);
1125 pthread_mutex_t mutex;
1258 Thread(
size_t stack = 0);
1269 virtual bool is_active(
void);
1278 void setPriority(
void);
1284 static void yield(
void);
1298 static Thread *
get(void);
1303 virtual void run(
void) = 0;
1318 virtual void exit(
void);
1323 static void init(
void);
1330 static void policy(
int polid);
1336 static void concurrency(
int level);
1344 static bool equal(pthread_t thread1, pthread_t thread2);
1350 static pthread_t
self(void);
1352 inline operator bool()
1353 {
return is_active();}
1355 inline bool operator!()
1356 {
return !is_active();}
1358 inline bool isRunning(
void)
1359 {
return is_active();}
1378 volatile bool running;
1380 volatile bool joining;
1403 virtual void run(
void) = 0;
1415 void start(
int priority = 0);
1462 virtual void run(
void) = 0;
1471 void start(
int priority = 0);
1618 {SharedPointer::replace(
object);};
1664 {LockedPointer::replace(
object);};
1707 {
return *(
static_cast<T&
>(object));};
1714 {
return static_cast<T*
>(object);};
1720 inline T*
get(void)
const
1721 {
return static_cast<T*
>(object);};
1749 {
return *(
static_cast<const T&
>(ptr->pointer));};
1756 {
return static_cast<const T*
>(ptr->pointer);};
1762 inline const T*
get(void)
const
1763 {
return static_cast<const T*
>(ptr->pointer);};
1792 {
return *(
static_cast<T&
>(auto_protect::object));};
1799 {
return static_cast<T*
>(auto_protect::object);};
1805 inline T*
get(void)
const
1806 {
return static_cast<T*
>(auto_protect::object);};
1815 {thread->
start(priority);}
1823 {thread->
start(priority);}
1878 {semaphore.
wait(timeout);}
1979 {
return lock.
modify(timeout);}
1987 {
return lock.
access(timeout);}
2010 inline bool _sync_protect_(
const void *obj)
2012 Mutex::protect(obj);
2016 inline bool _sync_release_(
const void *obj)
2022 inline bool _rw_reader_(
const void *obj)
2024 ThreadLock::reader(obj);
2028 inline bool _rw_writer_(
const void *obj)
2030 ThreadLock::writer(obj);
2034 inline bool _rw_release_(
const void *obj)
2040 #define ENTER_EXCLUSIVE \
2041 do { static pthread_mutex_t __sync__ = PTHREAD_MUTEX_INITIALIZER; \
2042 pthread_mutex_lock(&__sync__);
2044 #define LEAVE_EXCLUSIVE \
2045 pthread_mutex_unlock(&__sync__);} while(0);
2047 #define SYNC(obj) for(bool _sync_flag_ = _sync_protect_(obj); _sync_flag_; _sync_flag_ = _sync_release_(obj))
2049 #define SHARED(obj) for(bool _sync_flag_ = _rw_reader_(obj); _sync_flag_; _sync_flag_ = _rw_release_(obj))
2051 #define EXCLUSIVE(obj) for(bool _sync_flag_ = _rw_writer_(obj); _sync_flag_; _sync_flag_ = _rw_release_(obj))