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);};
166 inline void signal(
void)
167 {pthread_cond_signal(&cond);};
172 inline void broadcast(
void)
173 {pthread_cond_broadcast(&cond);};
187 #if !defined(_MSWINDOWS_) && !defined(__PTH__)
193 static inline pthread_condattr_t *initializer(
void)
194 {
return &attr.attr;};
203 static void gettimeout(
timeout_t timeout,
struct timespec *hires);
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);
260 {Conditional::gettimeout(timeout, hires);};
264 inline void lock(
void)
265 {EnterCriticalSection(&mutex);};
268 {LeaveCriticalSection(&mutex);};
270 void waitSignal(
void);
271 void waitBroadcast(
void);
274 {Conditional::signal();};
277 {Conditional::broadcast();};
284 {pthread_mutex_lock(&mutex);};
290 {pthread_mutex_unlock(&mutex);};
295 inline void waitSignal(
void)
296 {pthread_cond_wait(&cond, &mutex);};
301 inline void waitBroadcast(
void)
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;
467 {
return timed.
wait(timeout);};
512 unsigned getLocking(
void);
518 unsigned getWaiting(
void);
593 void set(
void *object);
644 void set(
void *object);
685 static void indexing(
unsigned size);
694 static bool writer(
void *
object,
timeout_t timeout = Timer::inf);
703 static bool reader(
void *
object,
timeout_t timeout = Timer::inf);
709 static void release(
void *
object);
720 unsigned getAccess(
void);
726 unsigned getModify(
void);
732 unsigned getWaiting(
void);
741 {
return lock.
modify(timeout);};
750 {
return lock.
access(timeout);};
824 Context *getContext(
void);
871 unsigned getReaders(
void);
876 unsigned getWaiters(
void);
956 void set(
unsigned count);
972 unsigned operator++(
void);
974 unsigned operator--(
void);
1003 {
return sync.
wait(timeout);};
1011 inline static void set(
barrier& sync,
unsigned count)
1026 unsigned count, waits, used;
1056 unsigned getCount(
void);
1062 unsigned getUsed(
void);
1068 void set(
unsigned count);
1078 inline void operator++(
void)
1084 inline void operator--(
void)
1101 {
return sync.
wait(timeout);};
1127 pthread_mutex_t mlock;
1156 gaurd(
void *
object);
1168 void set(
void *object);
1199 {pthread_mutex_lock(&mlock);};
1205 {pthread_mutex_lock(&mlock);};
1211 {pthread_mutex_unlock(&mlock);};
1217 {pthread_mutex_unlock(&mlock);};
1224 {pthread_mutex_lock(&lock.mlock);};
1231 {pthread_mutex_lock(&lock.mlock);};
1238 {pthread_mutex_unlock(&lock.mlock);};
1245 {pthread_mutex_unlock(&lock.mlock);};
1252 {pthread_mutex_lock(lock);};
1259 {pthread_mutex_lock(lock);};
1266 {pthread_mutex_unlock(lock);};
1273 {pthread_mutex_unlock(lock);};
1281 static void indexing(
unsigned size);
1288 static void protect(
void *
pointer);
1338 inline bool operator!()
const
1339 {
return object == NULL;};
1345 inline operator bool()
const
1346 {
return object != NULL;};
1354 void operator=(
void *
object);
1372 pthread_mutex_t mutex;
1520 void setPriority(
void);
1526 static void yield(
void);
1540 static Thread *
get(void);
1545 virtual void run(
void) = 0;
1560 virtual void exit(
void);
1565 static void init(
void);
1572 static void policy(
int polid);
1578 static void concurrency(
int level);
1586 static bool equal(pthread_t thread1, pthread_t thread2);
1592 static pthread_t
self(void);
1597 virtual bool isRunning(
void);
1599 inline operator bool()
1600 {
return isRunning();}
1619 volatile bool running;
1621 volatile bool joining;
1653 void start(
int priority = 0);
1659 inline void background(
void)
1705 void start(
int priority = 0);
1853 inline void replace(T *
object)
1854 {SharedPointer::replace(
object);};
1860 inline void operator=(T *
object)
1867 inline T *operator*()
1899 inline void replace(T *
object)
1900 {LockedPointer::replace(
object);};
1906 inline void operator=(T *
object)
1914 inline T *operator*()
1942 inline T& operator*()
const
1943 {
return *(
static_cast<T *
>(object));};
1949 inline T* operator->()
const
1950 {
return static_cast<T*
>(object);};
1956 inline T*
get(void)
const
1957 {
return static_cast<T*
>(object);};
1984 inline const T& operator*()
const
1985 {
return *(
static_cast<const T *
>(ptr->pointer));};
1991 inline const T* operator->()
const
1992 {
return static_cast<const T*
>(ptr->pointer);};
1998 inline const T*
get(void)
const
1999 {
return static_cast<const T*
>(ptr->pointer);};
2027 inline T& operator*()
const
2028 {
return *(
static_cast<T*
>(auto_protect::object));};
2034 inline T* operator->()
const
2035 {
return static_cast<T*
>(auto_protect::object);};
2041 inline T*
get(void)
const
2042 {
return static_cast<T*
>(auto_protect::object);};
2051 {thread->
start(priority);}
2059 {thread->
start(priority);}
2114 {semaphore.
wait(timeout);}
2215 {
return lock.
modify(timeout);}
2223 {
return lock.
access(timeout);}
2248 #define ENTER_EXCLUSIVE \
2249 do { static pthread_mutex_t __sync__ = PTHREAD_MUTEX_INITIALIZER; \
2250 pthread_mutex_lock(&__sync__);
2252 #define LEAVE_EXCLUSIVE \
2253 pthread_mutex_unlock(&__sync__);} while(0);