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 mutable CRITICAL_SECTION mutex;
93 mutable CONDITION_VARIABLE cond;
94 #elif defined(_MSTHREADS_)
95 enum {SIGNAL = 0, BROADCAST = 1};
98 mutable CRITICAL_SECTION mlock;
99 mutable CRITICAL_SECTION mutex;
102 class __LOCAL attribute
105 pthread_condattr_t attr;
109 __LOCAL
static attribute attr;
112 mutable pthread_cond_t cond;
113 mutable 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 friend class autolock;
189 class __EXPORT autolock
193 CRITICAL_SECTION *mutex;
195 pthread_mutex_t *mutex;
200 mutex = &
object->mutex;
202 EnterCriticalSection(mutex);
204 pthread_mutex_lock(mutex);
210 LeaveCriticalSection(mutex);
212 pthread_mutex_unlock(mutex);
217 #if !defined(_MSTHREADS_) && !defined(__PTH__)
233 static void set(
struct timespec *hires,
timeout_t timeout);
246 #if defined _MSCONDITIONAL_
247 CONDITION_VARIABLE bcast;
248 #elif !defined(_MSTHREADS_)
249 mutable pthread_cond_t bcast;
252 unsigned pending, waiting, sharing;
274 bool waitSignal(
struct timespec *timeout);
281 bool waitBroadcast(
struct timespec *timeout);
294 inline void lock(
void)
295 {EnterCriticalSection(&mutex);};
298 {LeaveCriticalSection(&mutex);};
300 void waitSignal(
void);
301 void waitBroadcast(
void);
303 inline void signal(
void)
306 inline void broadcast(
void)
314 {pthread_mutex_lock(&mutex);}
320 {pthread_mutex_unlock(&mutex);}
326 {pthread_cond_wait(&cond, &mutex);}
332 {pthread_cond_wait(&bcast, &mutex);}
339 {pthread_cond_signal(&cond);}
345 {pthread_cond_broadcast(&bcast);}
384 void limit_sharing(
unsigned max);
401 mutable pthread_cond_t cond;
404 mutable pthread_mutex_t mutex;
491 virtual void _lock(
void);
492 virtual void _unlock(
void);
534 virtual void _lock(
void);
535 virtual void _share(
void);
536 virtual void _unlock(
void);
574 void set(
const void *
object);
625 void set(
const void *
object);
666 static void indexing(
unsigned size);
690 static bool release(
const void *
object);
758 virtual void _share(
void);
759 virtual void _unlock(
void);
761 Context *getContext(
void);
803 virtual void share(
void);
841 void set(
unsigned count);
857 unsigned operator++(
void);
859 unsigned operator--(
void);
886 unsigned count, waits, used;
888 virtual void _share(
void);
889 virtual void _unlock(
void);
903 Semaphore(
unsigned count,
unsigned avail);
924 void set(
unsigned count);
960 mutable pthread_mutex_t mlock;
962 virtual void _lock(
void);
963 virtual void _unlock(
void);
966 friend class autolock;
968 class __EXPORT autolock
971 pthread_mutex_t *mutex;
974 inline autolock(
const Mutex *
object) {
975 mutex = &
object->mlock;
976 pthread_mutex_lock(this->mutex);
980 pthread_mutex_unlock(this->mutex);
1007 guard(
const void *
object);
1019 void set(
const void *
object);
1050 {pthread_mutex_lock(&mlock);}
1056 {pthread_mutex_lock(&mlock);}
1062 {pthread_mutex_unlock(&mlock);}
1068 {pthread_mutex_unlock(&mlock);}
1075 {pthread_mutex_lock(lock);}
1082 {pthread_mutex_unlock(lock);}
1090 static void indexing(
unsigned size);
1097 static bool protect(
const void *
pointer);
1148 {
return object == NULL;}
1154 inline operator bool()
const
1155 {
return object != NULL;}
1163 void operator=(
const void *
object);
1181 mutable pthread_mutex_t mutex;
1304 enum {R_UNUSED} reserved;
1314 Thread(
size_t stack = 0);
1325 virtual bool is_active(
void)
const;
1334 void setPriority(
void);
1340 static void yield(
void);
1354 static Thread *
get(void);
1359 virtual void run(
void) = 0;
1374 virtual void exit(
void);
1379 static void init(
void);
1386 static void policy(
int polid);
1392 static void concurrency(
int level);
1400 static bool equal(pthread_t thread1, pthread_t thread2);
1406 static pthread_t
self(void);
1408 inline operator bool()
const
1409 {
return is_active();}
1411 inline bool operator!()
const
1412 {
return !is_active();}
1414 inline bool isRunning(
void)
const
1415 {
return is_active();}
1434 volatile bool running;
1436 volatile bool joining;
1457 bool is_active(
void)
const;
1459 virtual void run(
void) = 0;
1471 void start(
int priority = 0);
1516 bool is_active(
void)
const;
1518 virtual void run(
void) = 0;
1527 void start(
int priority = 0);
1763 {
return *(
static_cast<T&
>(
object));}
1770 {
return static_cast<T*
>(
object);}
1776 inline T*
get(void)
const
1777 {
return static_cast<T*
>(
object);}
1805 {
return *(
static_cast<const T&
>(
ptr->pointer));}
1812 {
return static_cast<const T*
>(
ptr->pointer);}
1818 inline const T*
get(void)
const
1819 {
return static_cast<const T*
>(
ptr->pointer);}
1848 {
return *(
static_cast<T&
>(auto_protect::object));}
1855 {
return static_cast<T*
>(auto_protect::object);}
1861 inline T*
get(void)
const
1862 {
return static_cast<T*
>(auto_protect::object);}
1871 {thread->
start(priority);}
1879 {thread->
start(priority);}
1934 {semaphore.
wait(timeout);}
2035 {
return lock.
modify(timeout);}
2043 {
return lock.
access(timeout);}
2066 #define __AUTOLOCK__ autolock __autolock__(this);
2068 #define __SYNC__ for(bool _sync_flag_ = Mutex::protect(this); _sync_flag_; _sync_flag_ = !Mutex::release(this))
2070 #define __SHARED__ for(bool _sync_flag_ = ThreadLock::reader(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
2072 #define ___EXCLUSIVE__ for(bool _sync_flag_ = ThreadLock::writer(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
void operator=(void *pointer)
Set guard to mutex lock a new object.
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it's protocol.
void modify(void)
Acquire write (exclusive modify) lock.
void unlock(void)
Unlock the conditional's supporting mutex.
ReusableObject * next(ReusableObject *object)
Get next reusable object in the pool.
shared_pointer()
Created shared locking for typed singleton pointer.
const T * operator->() const
Access member of shared typed singleton object this instance locks and references.
void lock(void)
Lock the conditional's supporting mutex.
An abstract class for defining classes that operate as a thread.
Auto-pointer support class for locked objects.
void wait(void)
Wait (block) until signalled.
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
void commit(void)
Commit changes / release a modify lock.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
A common base class for all managed objects.
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
void operator=(const void *pointer)
Set guard to read lock a new object.
bool operator!() const
Test if the pointer is not set.
static void acquire(pthread_mutex_t *lock)
Convenience function to acquire os native mutex lock directly.
Generic smart pointer class.
unsigned long timeout_t
Typedef for millisecond timer values.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
void operator=(T *object)
Replace existing object through assignment.
T * operator->() const
Access member of instance of locked typed object by member reference.
void unlock(void)
Release acquired lock.
ConditionalLock condlock_t
Convenience type for using conditional locks.
void acquire(void)
Acquire mutex lock.
bool modify(timeout_t timeout=Timer::inf)
Request modify (write) access through the lock.
static void set(struct timespec *hires, timeout_t timeout)
Convert a millisecond timeout into use for high resolution conditional timers.
Auto-pointer support class for shared singleton objects.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
An exclusive locking protocol interface base.
A mutex locked object smart pointer helper class.
Portable recursive exclusive lock.
Common namespace for all ucommon objects.
void wait(void)
Wait until the semphore usage count is less than the thread limit.
void access(void)
Access mode shared thread scheduling.
Semaphore semaphore_t
Convenience type for using counting semaphores.
An optimized and convertable shared lock.
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it's protocol.
void waitSignal(void)
Wait (block) until signalled.
SharedObject * share(void)
Acquire a shared reference to the singleton object.
The shared pointer is used to manage a singleton instance of shared object.
ObjectProtocol * object
locked object protected by locked_release
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
locked_instance()
Construct empty locked instance of typed object.
void replace(T *object)
Replace existing typed singleton instance with new one.
void commit(accesslock_t &lock)
Convenience function to commit an exclusive access lock.
static const timeout_t inf
A value to use for infinite time.
T * operator*()
Access shared lock typed singleton object by pointer reference.
static pthread_condattr_t * initializer(void)
Support function for getting conditional attributes for realtime scheduling.
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.
A child thread object that may be joined by parent.
void wait(void)
Wait at the barrier until the count of threads waiting is reached.
static bool release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
virtual void share(void)
Return an exclusive access lock back to share mode.
Guard class to apply scope based access locking to objects.
A portable counting semaphore class.
Generic non-recursive exclusive lock class.
Common base class for all objects that can be formed into a linked list.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
void operator--(void)
Convenience operator to release a counting semaphore.
void unlock(void)
Unlock the conditional's supporting mutex.
void lock(void)
Acquire mutex lock.
Templated locked pointer for referencing locked objects of specific type.
Locking protocol classes for member function automatic operations.
T & operator*() const
Extract instance of locked typed object by pointer reference.
void replace(SharedObject *object)
Replace existing singleton instance with new one.
ThreadLock rwlock_t
Convenience type for using read/write locks.
void release(void)
Release a shared lock.
shared_instance()
Construct empty instance to reference shared typed singleton.
void waitBroadcast(void)
Wait (block) until broadcast.
void operator++(void)
Convenience operator to wait on a counting semaphore.
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
T * operator->() const
Reference member of object we are pointing to.
void replace(ObjectProtocol *object)
Replace existing object with a new one for next request.
T * dup(void)
Create a duplicate reference counted instance of the current typed object.
Templated shared pointer for singleton shared objects of specific type.
A general purpose smart pointer helper class.
Private heaps, pools, and associations.
void broadcast(void)
Signal the conditional to release all waiting threads.
T & operator*() const
Reference object we are pointing to through pointer indirection.
The conditional rw seperates scheduling for optizming behavior or rw locks.
barrier barrier_t
Convenience type for using thread barriers.
void modify(void)
Exclusive mode write thread scheduling.
A portable implimentation of "barrier" thread sychronization.
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
Reusable objects for forming private heaps.
TimedEvent timedevent_t
Convenience type for using timed events.
void start(int priority=0)
Start execution of child context.
const T * dup(void)
Acquire a shared (duplocate) reference to the typed singleton object.
shared_instance(shared_pointer< T > &pointer)
Construct shared access instance of shared typed singleton from matching shared_pointer.
void broadcast(void)
Signal the conditional to release all broadcast threads.
void operator=(ObjectProtocol *object)
Replace existing object through assignment.
void background(void)
Start execution of child context as background thread.
void release(void)
Release acquired lock.
void start(int priority=0)
Start execution of detached context.
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it's protocol.
ObjectProtocol * dup(void)
Create a duplicate reference counted instance of the current object.
void operator=(const void *pointer)
Set guard to read lock a new object.
A generic and portable implimentation of Read/Write locking.
A detached thread object that is stand-alone.
void lock(void)
Acquire or increase locking.
const T & operator*() const
Access shared typed singleton object this instance locks and references.
Timer class to use when scheduling realtime events.
A templated smart pointer instance for shared singleton typed objects.
void lock(void)
Lock the conditional's supporting mutex.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Mutex mutex_t
Convenience type for using exclusive mutex locks.
Typed smart locked pointer class.
void release(void)
Release the semaphore after waiting for it.
void replace(T *object)
Replace existing typed object with a new one for next request.
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
A templated smart pointer instance for lock protected objects.
void release(void)
Release or decrease locking.
mutex_pointer(T *object)
Create a pointer with a reference to a heap object.
SharedPointer * ptr
Shared lock for protected singleton.
ConditionalAccess accesslock_t
Convenience type for scheduling access.
An object pointer that uses mutex to assure thread-safe singleton use.
void release(void)
Release access mode read scheduling.
static void release(pthread_mutex_t *lock)
Convenience function to release os native mutex lock directly.
void operator=(T *object)
Replace existing typed singleton object through assignment.
void signal(void)
Signal the conditional to release one signalled thread.
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
T * operator*()
Create a duplicate reference counted instance of the current typed object by pointer reference...
void signal(void)
Signal the conditional to release one waiting thread.
mutex_pointer()
Create a pointer with no reference.
locked_pointer()
Create an instance of a typed locked pointer.
virtual void exclusive(void)
Convert read lock into exclusive (write/modify) access.
void commit(void)
Complete exclusive mode write scheduling.
void access(void)
Acquire access (shared read) lock.
The conditional is a common base for other thread synchronizing classes.
locked_instance(locked_pointer< T > &pointer)
Construct locked instance of typed object from matching locked_pointer.
Event notification to manage scheduled realtime threads.
An exclusive locking access interface base.
Guard class to apply scope based exclusive locking to objects.
Class for resource bound memory pools between threads.
Guard class to apply scope based mutex locking to objects.