54 #ifndef _UCOMMON_THREAD_H_
55 #define _UCOMMON_THREAD_H_
57 #ifndef _UCOMMON_CPR_H_
61 #ifndef _UCOMMON_ACCESS_H_
65 #ifndef _UCOMMON_TIMERS_H_
69 #ifndef _UCOMMON_MEMORY_H_
92 #if defined(_MSTHREADS_)
93 mutable CRITICAL_SECTION mutex;
94 mutable CONDITION_VARIABLE cond;
97 class __LOCAL attribute
100 pthread_condattr_t attr;
104 __LOCAL
static attribute attr;
107 mutable pthread_cond_t cond;
108 mutable pthread_mutex_t mutex;
119 bool wait(timeout_t timeout);
126 bool wait(
struct timespec *timeout);
129 inline void lock(
void)
130 {EnterCriticalSection(&mutex);};
133 {LeaveCriticalSection(&mutex);};
137 void broadcast(
void);
144 {pthread_mutex_lock(&mutex);}
150 {pthread_mutex_unlock(&mutex);}
156 {pthread_cond_wait(&cond, &mutex);}
162 {pthread_cond_signal(&cond);}
168 {pthread_cond_broadcast(&cond);}
181 friend class autolock;
184 class __EXPORT autolock
188 CRITICAL_SECTION *mutex;
190 pthread_mutex_t *mutex;
195 mutex = &
object->mutex;
197 EnterCriticalSection(mutex);
199 pthread_mutex_lock(mutex);
205 LeaveCriticalSection(mutex);
207 pthread_mutex_unlock(mutex);
212 #if !defined(_MSTHREADS_) && !defined(__PTH__)
228 static void set(
struct timespec *hires, timeout_t timeout);
241 #if defined _MSTHREADS_
242 CONDITION_VARIABLE bcast;
244 mutable pthread_cond_t bcast;
247 unsigned pending, waiting, sharing;
254 bool waitSignal(timeout_t timeout);
261 bool waitBroadcast(timeout_t timeout);
269 bool waitSignal(
struct timespec *timeout);
276 bool waitBroadcast(
struct timespec *timeout);
284 inline static void set(
struct timespec *hires, timeout_t timeout)
289 inline void lock(
void)
290 {EnterCriticalSection(&mutex);};
293 {LeaveCriticalSection(&mutex);};
295 void waitSignal(
void);
297 void waitBroadcast(
void);
299 inline void signal(
void)
302 inline void broadcast(
void)
310 {pthread_mutex_lock(&mutex);}
316 {pthread_mutex_unlock(&mutex);}
322 {pthread_cond_wait(&cond, &mutex);}
328 {pthread_cond_wait(&bcast, &mutex);}
335 {pthread_cond_signal(&cond);}
341 {pthread_cond_broadcast(&bcast);}
380 void limit_sharing(
unsigned max);
397 mutable pthread_cond_t cond;
400 mutable pthread_mutex_t mutex;
460 bool wait(timeout_t timeout);
487 virtual void _lock(
void);
488 virtual void _unlock(
void);
504 bool lock(timeout_t timeout);
530 virtual void _lock(
void);
531 virtual void _share(
void);
532 virtual void _unlock(
void);
570 void set(
const void *
object);
621 void set(
const void *
object);
662 static void indexing(
unsigned size);
671 static bool writer(
const void *
object, timeout_t timeout =
Timer::inf);
680 static bool reader(
const void *
object, timeout_t timeout =
Timer::inf);
686 static bool release(
const void *
object);
754 virtual void _share(
void);
755 virtual void _unlock(
void);
757 Context *getContext(
void);
799 virtual void share(
void);
837 void set(
unsigned count);
853 unsigned operator++(
void);
855 unsigned operator--(
void);
868 bool wait(timeout_t timeout);
882 unsigned count, waits, used;
884 virtual void _share(
void);
885 virtual void _unlock(
void);
899 Semaphore(
unsigned count,
unsigned avail);
914 bool wait(timeout_t timeout);
920 void set(
unsigned count);
956 mutable pthread_mutex_t mlock;
958 virtual void _lock(
void);
959 virtual void _unlock(
void);
962 friend class autolock;
964 class __EXPORT autolock
967 pthread_mutex_t *mutex;
970 inline autolock(
const Mutex *
object) {
971 mutex = &
object->mlock;
972 pthread_mutex_lock(this->mutex);
976 pthread_mutex_unlock(this->mutex);
1003 guard(
const void *
object);
1015 void set(
const void *
object);
1046 {pthread_mutex_lock(&mlock);}
1052 {pthread_mutex_lock(&mlock);}
1058 {pthread_mutex_unlock(&mlock);}
1064 {pthread_mutex_unlock(&mlock);}
1071 {pthread_mutex_lock(lock);}
1078 {pthread_mutex_unlock(lock);}
1086 static void indexing(
unsigned size);
1093 static bool protect(
const void *
pointer);
1144 {
return object == NULL;}
1150 inline operator bool()
const
1151 {
return object != NULL;}
1159 void operator=(
const void *
object);
1177 mutable pthread_mutex_t mutex;
1300 enum {R_UNUSED} reserved;
1310 Thread(
size_t stack = 0);
1321 virtual bool is_active(
void)
const;
1330 void setPriority(
void);
1336 static void yield(
void);
1342 static void sleep(timeout_t timeout);
1350 static Thread *
get(void);
1355 virtual void run(
void) = 0;
1370 virtual void exit(
void);
1375 static void init(
void);
1382 static void policy(
int polid);
1388 static void concurrency(
int level);
1396 static bool equal(pthread_t thread1, pthread_t thread2);
1402 static pthread_t
self(void);
1404 inline operator bool()
const
1405 {
return is_active();}
1407 inline bool operator!()
const
1408 {
return !is_active();}
1410 inline bool isRunning(
void)
const
1411 {
return is_active();}
1430 volatile bool running;
1432 volatile bool joining;
1453 bool is_active(
void)
const;
1455 virtual void run(
void) = 0;
1467 void start(
int priority = 0);
1512 bool is_active(
void)
const;
1514 virtual void run(
void) = 0;
1523 void start(
int priority = 0);
1759 {
return *(
static_cast<T&
>(
object));}
1766 {
return static_cast<T*
>(
object);}
1772 inline T*
get(void)
const
1773 {
return static_cast<T*
>(
object);}
1801 {
return *(
static_cast<const T&
>(
ptr->pointer));}
1808 {
return static_cast<const T*
>(
ptr->pointer);}
1814 inline const T*
get(void)
const
1815 {
return static_cast<const T*
>(
ptr->pointer);}
1844 {
return *(
static_cast<T&
>(auto_protect::object));}
1851 {
return static_cast<T*
>(auto_protect::object);}
1857 inline T*
get(void)
const
1858 {
return static_cast<T*
>(auto_protect::object);}
1867 {thread->
start(priority);}
1875 {thread->
start(priority);}
1930 {semaphore.
wait(timeout);}
2031 {
return lock.
modify(timeout);}
2039 {
return lock.
access(timeout);}
2062 #define __AUTOPROTECT(x) Mutex::guard __autolock__(x)
2064 #define __AUTOLOCK__ autolock __autolock__(this);
2066 #define __SYNC__ for(bool _sync_flag_ = Mutex::protect(this); _sync_flag_; _sync_flag_ = !Mutex::release(this))
2068 #define __SHARED__ for(bool _sync_flag_ = ThreadLock::reader(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
2070 #define ___EXCLUSIVE__ for(bool _sync_flag_ = ThreadLock::writer(this); _sync_flag_; _sync_flag_ = !ThreadLock::release(this))
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
void access(SharedAccess &object)
Convenience function to access (lock) shared object through it's protocol.
Semaphore semaphore_t
Convenience type for using counting semaphores.
void start(int priority=0)
Start execution of detached context.
The conditional is a common base for other thread synchronizing classes.
void exclusive(SharedAccess &object)
Convenience function to exclusive lock shared object through it's protocol.
Auto-pointer support class for shared singleton objects.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
void operator--(void)
Convenience operator to release a counting semaphore.
Portable recursive exclusive lock.
A general purpose smart pointer helper class.
Event notification to manage scheduled realtime threads.
void commit(accesslock_t &lock)
Convenience function to commit an exclusive access lock.
SharedPointer * ptr
Shared lock for protected singleton.
void background(void)
Start execution of child context as background thread.
TimedEvent timedevent_t
Convenience type for using timed events.
void access(void)
Acquire access (shared read) lock.
void modify(void)
Acquire write (exclusive modify) lock.
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.
shared_instance(shared_pointer< T > &pointer)
Construct shared access instance of shared typed singleton from matching shared_pointer.
void replace(SharedObject *object)
Replace existing singleton instance with new one.
void release(void)
Release acquired lock.
Common base class for all objects that can be formed into a linked list.
An exclusive locking access interface base.
Templated shared pointer for singleton shared objects of specific type.
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.
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
ConditionalLock condlock_t
Convenience type for using conditional locks.
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.
ConditionalAccess accesslock_t
Convenience type for scheduling access.
Guard class to apply scope based mutex locking to objects.
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.
void modify(accesslock_t &lock)
Convenience function to exclusively schedule conditional access.
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.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
void lock(void)
Lock the conditional's supporting mutex.
Private heaps, pools, and associations.
void operator=(T *object)
Replace existing object through assignment.
void release(void)
Release the semaphore after waiting for it.
Locking protocol classes for member function automatic operations.
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.
barrier barrier_t
Convenience type for using thread barriers.
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.
Reusable objects for forming private heaps.
locked_instance(locked_pointer< T > &pointer)
Construct locked instance of typed object from matching locked_pointer.
void acquire(mutex_t &mutex)
Convenience function to acquire a mutex.
Typed smart locked pointer class.
Realtime timers and timer queues.
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.
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.
locked_pointer()
Create an instance of a typed locked pointer.
A templated smart pointer instance for shared singleton typed objects.
void operator=(const void *pointer)
Set guard to read lock a new object.
T & operator*() const
Extract instance of locked typed object by pointer reference.
void unlock(void)
Release acquired lock.
shared_instance()
Construct empty instance to reference shared typed singleton.
ObjectProtocol * dup(void)
Create a duplicate reference counted instance of the current object.
Auto-pointer support class for locked objects.
Common namespace for all ucommon objects.
Guard class to apply scope based exclusive locking to objects.
The shared pointer is used to manage a singleton instance of shared object.
The conditional rw seperates scheduling for optizming behavior or rw locks.
void lock(void)
Lock the conditional's supporting mutex.
A generic and portable implimentation of Read/Write locking.
void lock(void)
Acquire mutex lock.
T * operator->() const
Reference member of object we are pointing to.
void lock(ExclusiveAccess &object)
Convenience function to exclusively lock an object through it's protocol.
A portable implimentation of "barrier" thread sychronization.
void replace(ObjectProtocol *object)
Replace existing object with a new one for next request.
An exclusive locking protocol interface base.
Class for resource bound memory pools between threads.
A mutex locked object smart pointer helper class.
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
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.
Timer class to use when scheduling realtime events.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
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.
Mutex mutex_t
Convenience type for using exclusive mutex locks.
T & operator*() const
Reference object we are pointing to through pointer indirection.
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 operator=(const void *pointer)
Set guard to read lock a new object.
void release(void)
Release a shared lock.
void release(void)
Release access mode read scheduling.
void modify(void)
Exclusive mode write thread scheduling.
static const timeout_t inf
A value to use for infinite time.
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.
T &() max(T &o1, T &o2)
Convenience function to return max of two objects.
Guard class to apply scope based access locking to objects.
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.
An abstract class for defining classes that operate as a thread.
void share(SharedAccess &object)
Convenience function to restore shared locking for object through it's protocol.
A portable counting semaphore class.
static bool release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
void access(void)
Access mode shared thread scheduling.
void start(JoinableThread *thread, int priority=0)
Convenience function to start a joinable thread.
T * operator*()
Access shared lock typed singleton object by pointer reference.
bool modify(timeout_t timeout=Timer::inf)
Request modify (write) access through the lock.
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
SharedObject * share(void)
Acquire a shared reference to the singleton object.
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.