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);
873 Semaphore(
unsigned count,
unsigned avail);
894 void set(
unsigned count);
930 pthread_mutex_t mlock;
932 virtual void _lock(
void);
933 virtual void _unlock(
void);
959 guard(
const void *
object);
971 void set(
const void *
object);
1002 {pthread_mutex_lock(&mlock);};
1008 {pthread_mutex_lock(&mlock);};
1014 {pthread_mutex_unlock(&mlock);};
1020 {pthread_mutex_unlock(&mlock);};
1027 {pthread_mutex_lock(lock);};
1034 {pthread_mutex_unlock(lock);};
1042 static void indexing(
unsigned size);
1049 static void protect(
const void *
pointer);
1100 {
return object == NULL;};
1106 inline operator bool()
const
1107 {
return object != NULL;};
1115 void operator=(
const void *
object);
1133 pthread_mutex_t mutex;
1266 Thread(
size_t stack = 0);
1277 virtual bool is_active(
void);
1286 void setPriority(
void);
1292 static void yield(
void);
1306 static Thread *
get(void);
1311 virtual void run(
void) = 0;
1326 virtual void exit(
void);
1331 static void init(
void);
1338 static void policy(
int polid);
1344 static void concurrency(
int level);
1352 static bool equal(pthread_t thread1, pthread_t thread2);
1358 static pthread_t
self(void);
1360 inline operator bool()
1361 {
return is_active();}
1363 inline bool operator!()
1364 {
return !is_active();}
1366 inline bool isRunning(
void)
1367 {
return is_active();}
1386 volatile bool running;
1388 volatile bool joining;
1411 virtual void run(
void) = 0;
1423 void start(
int priority = 0);
1470 virtual void run(
void) = 0;
1479 void start(
int priority = 0);
1626 {SharedPointer::replace(
object);};
1672 {LockedPointer::replace(
object);};
1715 {
return *(
static_cast<T&
>(object));};
1722 {
return static_cast<T*
>(object);};
1728 inline T*
get(void)
const
1729 {
return static_cast<T*
>(object);};
1757 {
return *(
static_cast<const T&
>(ptr->pointer));};
1764 {
return static_cast<const T*
>(ptr->pointer);};
1770 inline const T*
get(void)
const
1771 {
return static_cast<const T*
>(ptr->pointer);};
1800 {
return *(
static_cast<T&
>(auto_protect::object));};
1807 {
return static_cast<T*
>(auto_protect::object);};
1813 inline T*
get(void)
const
1814 {
return static_cast<T*
>(auto_protect::object);};
1823 {thread->
start(priority);}
1831 {thread->
start(priority);}
1886 {semaphore.
wait(timeout);}
1987 {
return lock.
modify(timeout);}
1995 {
return lock.
access(timeout);}
2018 inline bool _sync_protect_(
const void *obj)
2020 Mutex::protect(obj);
2024 inline bool _sync_release_(
const void *obj)
2030 inline bool _rw_reader_(
const void *obj)
2032 ThreadLock::reader(obj);
2036 inline bool _rw_writer_(
const void *obj)
2038 ThreadLock::writer(obj);
2042 inline bool _rw_release_(
const void *obj)
2048 #define ENTER_EXCLUSIVE \
2049 do { static pthread_mutex_t __sync__ = PTHREAD_MUTEX_INITIALIZER; \
2050 pthread_mutex_lock(&__sync__);
2052 #define LEAVE_EXCLUSIVE \
2053 pthread_mutex_unlock(&__sync__);} while(0);
2055 #define SYNC(obj) for(bool _sync_flag_ = _sync_protect_(obj); _sync_flag_; _sync_flag_ = _sync_release_(obj))
2057 #define SHARED(obj) for(bool _sync_flag_ = _rw_reader_(obj); _sync_flag_; _sync_flag_ = _rw_release_(obj))
2059 #define EXCLUSIVE(obj) for(bool _sync_flag_ = _rw_writer_(obj); _sync_flag_; _sync_flag_ = _rw_release_(obj))
void start(int priority=0)
Start execution of detached context.
The conditional is a common base for other thread synchronizing classes.
Auto-pointer support class for shared singleton objects.
void operator--(void)
Convenience operator to release a counting semaphore.
Portable recursive exclusive lock.
A general purpose smart pointer helper class.
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
barrier barrier_t
Convenience type for using thread barriers.
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
A generic and portable implimentation of Read/Write locking.
Realtime timers and timer queues.
SharedPointer * ptr
Shared lock for protected singleton.
void background(void)
Start execution of child context as background thread.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
void access(void)
Acquire access (shared read) lock.
void modify(void)
Acquire write (exclusive modify) lock.
static void release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
void signal(void)
Signal the conditional to release one waiting thread.
ThreadLock rwlock_t
Convenience type for using read/write locks.
A templated smart pointer instance for lock protected objects.
virtual void exit(void)
Exit the thread context.
shared_instance(shared_pointer< T > &pointer)
Construct shared access instance of shared typed singleton from matching shared_pointer.
void release(void)
Release acquired lock.
Common base class for all objects that can be formed into a linked list.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
An exclusive locking access interface base.
void access(condlock_t &lock)
Convenience function for shared access to a conditional lock.
static const time_t reset
A value to use when resetting.
Templated shared pointer for singleton shared objects of specific type.
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it's protocol.
void wait(void)
Wait until the semphore usage count is less than the thread limit.
void operator=(T *object)
Replace existing typed singleton object through assignment.
void commit(void)
Complete exclusive mode write scheduling.
An object pointer that uses mutex to assure thread-safe singleton use.
void operator++(void)
Convenience operator to wait on a counting semaphore.
static void acquire(pthread_mutex_t *lock)
Convenience function to acquire os native mutex lock directly.
T * dup(void)
Create a duplicate reference counted instance of the current typed object.
Guard class to apply scope based mutex locking to objects.
An abstract class for defining classes that operate as a thread.
void wait(void)
Wait at the barrier until the count of threads waiting is reached.
void unlock(void)
Unlock the conditional's supporting mutex.
virtual void share(void)
Return an exclusive access lock back to share mode.
shared_pointer()
Created shared locking for typed singleton pointer.
void lock(void)
Acquire or increase locking.
bool operator!() const
Test if the pointer is not set.
void broadcast(void)
Signal the conditional to release all waiting threads.
const T * dup(void)
Acquire a shared (duplocate) reference to the typed singleton object.
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it's protocol.
ConditionalAccess accesslock_t
Convenience type for scheduling access.
void lock(void)
Lock the conditional's supporting mutex.
void operator=(T *object)
Replace existing object through assignment.
void release(void)
Release the semaphore after waiting for it.
A common base class for all managed objects.
void broadcast(void)
Signal the conditional to release all broadcast threads.
An optimized and convertable shared lock.
virtual void exclusive(void)
Convert read lock into exclusive (write/modify) access.
void release(void)
Release or decrease locking.
void acquire(void)
Acquire mutex lock.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
Reusable objects for forming private heaps.
void modify(condlock_t &lock)
Convenience function to exclusively aquire a conditional lock.
locked_instance(locked_pointer< T > &pointer)
Construct locked instance of typed object from matching locked_pointer.
virtual void run(void)=0
Abstract interface for thread context run method.
Typed smart locked pointer class.
Semaphore semaphore_t
Convenience type for using counting semaphores.
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
void operator=(void *pointer)
Set guard to mutex lock a new object.
void waitBroadcast(void)
Wait (block) until broadcast.
void commit(void)
Commit changes / release a modify lock.
Generic non-recursive exclusive lock class.
Guard class to apply scope based exclusive locking to objects.
A child thread object that may be joined by parent.
T * operator*()
Create a duplicate reference counted instance of the current typed object by pointer reference...
mutex_pointer()
Create a pointer with no reference.
void operator=(ObjectProtocol *object)
Replace existing object through assignment.
void waitSignal(void)
Wait (block) until signalled.
Generic smart pointer class.
void start(int priority=0)
Start execution of child context.
const T & operator*() const
Access shared typed singleton object this instance locks and references.
void release(rexlock_t &lock)
Convenience function to release a shared recursive mutex lock.
void operator=(const void *pointer)
Set guard to read lock a new object.
locked_pointer()
Create an instance of a typed locked pointer.
A templated smart pointer instance for shared singleton typed objects.
T & operator*() const
Extract instance of locked typed object by pointer reference.
void unlock(void)
Release acquired lock.
bool exclusive(rwlock_t &lock, timeout_t timeout=Timer::inf)
Convenience function for exclusive write access to a read/write lock.
Timer class to use when scheduling realtime events.
shared_instance()
Construct empty instance to reference shared typed singleton.
TimedEvent timedevent_t
Convenience type for using timed events.
Auto-pointer support class for locked objects.
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
virtual bool is_active(void)
Check if running.
The shared pointer is used to manage a singleton instance of shared object.
void start(DetachedThread *thread, int priority=0)
Convenience function to start a detached thread.
The conditional rw seperates scheduling for optizming behavior or rw locks.
void lock(void)
Lock the conditional's supporting mutex.
void lock(void)
Acquire mutex lock.
T * operator->() const
Reference member of object we are pointing to.
A portable implimentation of "barrier" thread sychronization.
An exclusive locking protocol interface base.
Class for resource bound memory pools between threads.
A mutex locked object smart pointer helper class.
ConditionalLock condlock_t
Convenience type for using conditional locks.
ObjectProtocol * object
locked object protected by locked_release
void wait(void)
Wait (block) until signalled.
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
bool modify(timeout_t timeout=Timer::inf)
Request modify (write) access through the lock.
Guard class to apply scope based access locking to objects.
unsigned long timeout_t
Typedef for millisecond timer values.
const T * operator->() const
Access member of shared typed singleton object this instance locks and references.
void replace(T *object)
Replace existing typed object with a new one for next request.
A detached thread object that is stand-alone.
T & operator*() const
Reference object we are pointing to through pointer indirection.
Mutex mutex_t
Convenience type for using exclusive mutex locks.
static pthread_condattr_t * initializer(void)
Support function for getting conditional attributes for realtime scheduling.
void unlock(void)
Unlock the conditional's supporting mutex.
locked_instance()
Construct empty locked instance of typed object.
static void release(pthread_mutex_t *lock)
Convenience function to release os native mutex lock directly.
void release(void)
Release a shared lock.
void release(void)
Release access mode read scheduling.
void wait(semaphore_t &semaphore, timeout_t timeout=Timer::inf)
Convenience function to wait on a semaphore.
void commit(accesslock_t &lock)
Convenience function to commit an exclusive access lock.
void modify(void)
Exclusive mode write thread scheduling.
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it's protocol.
Templated locked pointer for referencing locked objects of specific type.
void signal(void)
Signal the conditional to release one signalled thread.
mutex_pointer(T *object)
Create a pointer with a reference to a heap object.
void lock(rexlock_t &lock)
Convenience function to lock a shared recursive mutex lock.
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
void replace(T *object)
Replace existing typed singleton instance with new one.
Locking protocol classes for member function automatic operations.
A portable counting semaphore class.
bool share(rwlock_t &lock, timeout_t timeout=Timer::inf)
Convenience function for shared read access to a read/write lock.
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
void operator=(const void *pointer)
Set guard to read lock a new object.
void access(void)
Access mode shared thread scheduling.
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Private heaps, pools, and associations.
T * operator*()
Access shared lock typed singleton object by pointer reference.
void commit(condlock_t &lock)
Convenience function to commit and release an exclusively locked conditional lock.
static void sync(Timer &timer)
Sleep current thread until the specified timer expires.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
Event notification to manage scheduled realtime threads.
ReusableObject * next(ReusableObject *object)
Get next reusable object in the pool.
T * operator->() const
Access member of instance of locked typed object by member reference.
virtual void _share(void)=0
Access interface to share lock the object.