43#ifndef _UCOMMON_THREAD_H_
44#define _UCOMMON_THREAD_H_
46#ifndef _UCOMMON_CPR_H_
50#ifndef _UCOMMON_ACCESS_H_
54#ifndef _UCOMMON_TIMERS_H_
58#ifndef _UCOMMON_MEMORY_H_
62#ifndef _UCOMMON_CONDITION_H_
86 __DELETE_COPY(RWLock);
92 virtual void _share(
void) __OVERRIDE;
94 virtual void _lock(
void) __OVERRIDE;
96 virtual void _unlock(
void) __OVERRIDE;
98 virtual void _unshare(
void) __OVERRIDE;
101 typedef autoshared<RWLock> autoreader;
103 typedef autoexclusive<RWLock> autowriter;
112 class __EXPORT reader
117 __DELETE_COPY(reader);
142 void set(
const void *
object);
165 static bool lock(
const void *
object, timeout_t timeout = Timer::inf);
175 class __EXPORT writer
180 __DELETE_COPY(writer);
205 void set(
const void *
object);
228 static bool lock(
const void *
object, timeout_t timeout = Timer::inf);
241 bool modify(timeout_t timeout = Timer::inf);
248 bool access(timeout_t timeout = Timer::inf);
278class __EXPORT TimedEvent :
public Timer
284 mutable pthread_cond_t cond;
287 mutable pthread_mutex_t mutex;
289 __DELETE_COPY(TimedEvent);
372 __DELETE_COPY(RecursiveMutex);
379 virtual void _lock(
void) __OVERRIDE;
380 virtual void _unlock(
void) __OVERRIDE;
383 typedef autoexclusive<RecursiveMutex> autolock;
416class __EXPORT ReusableAllocator :
protected Conditional
419 __DELETE_COPY(ReusableAllocator);
462 __DELETE_COPY(Mutex);
465 mutable pthread_mutex_t mlock;
467 virtual void _lock(
void) __OVERRIDE;
468 virtual void _unlock(
void) __OVERRIDE;
471 typedef autoexclusive<Mutex> autolock;
487 pthread_mutex_lock(&mlock);
494 pthread_mutex_lock(&mlock);
501 pthread_mutex_unlock(&mlock);
508 pthread_mutex_unlock(&mlock);
516 pthread_mutex_lock(
lock);
524 pthread_mutex_unlock(
lock);
556class __EXPORT AutoProtect
560 __DELETE_COPY(AutoProtect);
576 void set(
const void *
object);
595 inline operator bool()
const {
596 return object != NULL;
599 inline bool operator!()
const {
600 return object == NULL;
608 inline autoprotect() : AutoProtect() {};
610 inline autoprotect(
const T *
object) : AutoProtect(object) {};
612 inline void set(
const T *
object) {
620 inline autoprotect& operator=(
const T*
object) {
625 inline T* operator->()
const {
626 return static_cast<T*
>(object);
629 inline T& operator*()
const {
630 __THROW_DEREF(
object);
631 return *(
static_cast<T*
>(object));
648 __DELETE_COPY(Thread);
658 enum {R_UNUSED} reserved;
690 __DELETE_COPY(Local);
695 virtual void release(
void *instance) = 0;
697 virtual void *allocate();
704 void set(
void *instance);
708 inline void clear() {
731 static void sleep(timeout_t timeout);
744 virtual void run(
void) = 0;
790 static bool equal(pthread_t thread1, pthread_t thread2);
798 inline operator bool()
const {
802 inline bool operator!()
const {
806 inline bool isRunning(
void)
const {
810 static void release(
void);
823class __EXPORT JoinableThread :
public Thread
826 __DELETE_COPY(JoinableThread);
832 volatile bool running;
834 volatile bool joining;
857 virtual void run(
void) __OVERRIDE = 0;
887class __EXPORT DetachedThread :
public Thread
890 __DELETE_COPY(DetachedThread);
920 virtual void run(
void) __OVERRIDE = 0;
952#define __AUTOLOCK(x) autolock __autolock__(x)
953#define __AUTOPROTECT(x) AutoProtect __autolock__(x)
954#define __SYNC(x) for(bool _sync_flag_ = Mutex::protect(x); _sync_flag_; _sync_flag_ = !Mutex::release(x))
Private heaps, pools, and associations.
Condition classes for thread sychronization and timing.
Locking protocol classes for member function automatic operations.
Realtime timers and timer queues.
Common namespace for all ucommon objects.
RWLock rwlock_t
Convenience type for using read/write locks.
Mutex mutex_t
Convenience type for using exclusive mutex locks.
TimedEvent timedevent_t
Convenience type for using timed events.
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.
An exclusive locking protocol interface base.
An exclusive locking access interface base.
Generic smart pointer class.
Common base class for all objects that can be formed into a linked list.
Reusable objects for forming private heaps.
ReusableObject * getNext(void)
Get next effective reusable object when iterating.
A generic and portable implementation of Read/Write locking.
virtual void _share(void)
Access interface to share lock the object.
bool access(timeout_t timeout=Timer::inf)
Request shared (read) access through the lock.
RWLock()
Create an instance of a rwlock.
static bool release(const void *object)
Release an arbitrary object that has been protected by a rwlock.
void release(void)
Release the lock.
static void indexing(unsigned size)
Specify hash table size for guard protection.
bool modify(timeout_t timeout=Timer::inf)
Request modify (write) access through the lock.
~reader()
Release mutex when guard falls out of scope.
reader()
Create an unitialized instance of guard.
reader(const void *object)
Construct a guard for a specific object.
void release(void)
Prematurely release a guard.
void set(const void *object)
Set guard to mutex lock a new object.
static bool lock(const void *object, timeout_t timeout=Timer::inf)
Shared access to an arbitrary object.
void operator=(const void *pointer)
Set guard to read lock a new object.
~writer()
Release mutex when guard falls out of scope.
writer(const void *object)
Construct a guard for a specific object.
void operator=(const void *pointer)
Set guard to read lock a new object.
void set(const void *object)
Set guard to mutex lock a new object.
static bool lock(const void *object, timeout_t timeout=Timer::inf)
Write protect access to an arbitrary object.
writer()
Create an unitialized instance of guard.
void release(void)
Prematurely release a guard.
Event notification to manage scheduled realtime threads.
TimedEvent(time_t timeout)
Create event handler and timer set to trigger a timeout.
void reset(void)
Reset triggered conditional.
void wait(void)
A simple wait until triggered.
void lock(void)
Lock the object for wait or to manipulate derived data.
bool sync(void)
Wait while locked.
TimedEvent(void)
Create event handler and timer for timing of events.
void signal(void)
Signal pending event.
bool wait(timeout_t timeout)
Wait to be signalled or until timer expires.
~TimedEvent()
Destroy timer and release pending events.
TimedEvent(timeout_t timeout)
Create event handler and timer set to trigger a timeout.
void release(void)
Release the object lock after waiting.
Portable recursive exclusive lock.
bool lock(timeout_t timeout)
Timed lock request.
void release(void)
Release or decrease locking.
RecursiveMutex()
Create rexlock.
void lock(void)
Acquire or increase locking.
ReusableObject * next(ReusableObject *object)
Get next reusable object in the pool.
ReusableAllocator()
Initialize reusable allocator through a conditional.
void release(ReusableObject *object)
Release resuable object.
Generic non-recursive exclusive lock class.
static void release(pthread_mutex_t *lock)
Convenience function to release os native mutex lock directly.
static void indexing(unsigned size)
Specify hash table size for guard protection.
Mutex()
Create a mutex lock.
static void acquire(pthread_mutex_t *lock)
Convenience function to acquire os native mutex lock directly.
static bool release(const void *pointer)
Specify a pointer/object/resource to release.
void release(void)
Release acquired lock.
void acquire(void)
Acquire mutex lock.
static bool protect(const void *pointer)
Specify pointer/object/resource to guard protect.
void lock(void)
Acquire mutex lock.
void unlock(void)
Release acquired lock.
~Mutex()
Destroy mutex lock, release waiting threads.
Guard class to apply scope based mutex locking to objects.
void release(void)
Prematurely release a guard.
void set(const void *object)
Set guard to mutex lock a new object.
~AutoProtect()
Release mutex when guard falls out of scope.
AutoProtect()
Create an unitialized instance of guard.
AutoProtect(const void *object)
Construct a guard for a specific object.
static size_t cache(void)
Get cache line size.
static bool equal(pthread_t thread1, pthread_t thread2)
Determine if two thread identifiers refer to the same thread.
static void policy(int polid)
Used to specify scheduling policy for threads above priority "0".
static void init(void)
Used to initialize threading library.
virtual ~Thread()
Destroy thread object, thread-specific data, and execution context.
virtual void exit(void)
Exit the thread context.
virtual void run(void)=0
Abstract interface for thread context run method.
Thread(size_t stack=0)
Create a thread object that will have a preset stack size.
void map(void)
Map thread for get method.
static void yield(void)
Yield execution context of the current thread.
static Thread * get(void)
Get mapped thread object.
virtual bool is_active(void) const
Check if running.
static pthread_t self(void)
Get current thread id.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
static void concurrency(int level)
Set concurrency level of process.
void setPriority(void)
Set thread priority without disrupting scheduling if possible.
void join(void)
Join thread with parent.
JoinableThread(size_t size=0)
Create a joinable thread with a known context stack size.
bool is_active(void) const
Check if running.
void start(int priority=0)
Start execution of child context.
virtual ~JoinableThread()
Delete child thread.
virtual void run(void)=0
Abstract interface for thread context run method.
void background(void)
Start execution of child context as background thread.
void exit(void)
Exit context of detached thread.
~DetachedThread()
Destroys object when thread context exits.
bool is_active(void) const
Check if running.
void start(int priority=0)
Start execution of detached context.
virtual void run(void)=0
Abstract interface for thread context run method.
DetachedThread(size_t size=0)
Create a detached thread with a known context stack size.
Timer()
Construct an untriggered timer set to the time of creation.