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_
83class __EXPORT RWLock :
private ConditionalAccess,
public __PROTOCOL ExclusiveProtocol,
public __PROTOCOL SharedProtocol
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);
130 reader(
const void *
object);
142 void set(
const void *
object);
154 inline void operator=(
const void *pointer) {
165 static bool lock(
const void *
object, timeout_t timeout = Timer::inf);
175 class __EXPORT writer
180 __DELETE_COPY(writer);
193 writer(
const void *
object);
205 void set(
const void *
object);
217 inline void operator=(
const void *pointer) {
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);
256 static void indexing(
unsigned size);
262 static bool release(
const void *
object);
278class __EXPORT TimedEvent :
public Timer
284 mutable pthread_cond_t cond;
287 mutable pthread_mutex_t mutex;
289 __DELETE_COPY(TimedEvent);
323 TimedEvent(timeout_t timeout);
329 TimedEvent(time_t timeout);
349 bool wait(timeout_t timeout);
369class __EXPORT RecursiveMutex :
private Conditional,
public __PROTOCOL ExclusiveProtocol
372 __DELETE_COPY(RecursiveMutex);
379 virtual void _lock(
void) __OVERRIDE;
380 virtual void _unlock(
void) __OVERRIDE;
383 typedef autoexclusive<RecursiveMutex> autolock;
398 bool lock(timeout_t timeout);
416class __EXPORT ReusableAllocator :
protected Conditional
419 __DELETE_COPY(ReusableAllocator);
422 ReusableObject *freelist;
435 inline ReusableObject *next(ReusableObject *
object) {
436 return object->getNext();
443 void release(ReusableObject *
object);
459class __EXPORT Mutex :
public __PROTOCOL ExclusiveProtocol
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;
486 inline void acquire(
void) {
487 pthread_mutex_lock(&mlock);
493 inline void lock(
void) {
494 pthread_mutex_lock(&mlock);
500 inline void unlock(
void) {
501 pthread_mutex_unlock(&mlock);
507 inline void release(
void) {
508 pthread_mutex_unlock(&mlock);
515 inline static void acquire(pthread_mutex_t *lock) {
516 pthread_mutex_lock(lock);
523 inline static void release(pthread_mutex_t *lock) {
524 pthread_mutex_unlock(lock);
533 static void indexing(
unsigned size);
540 static bool protect(
const void *pointer);
546 static bool release(
const void *pointer);
556class __EXPORT AutoProtect
560 __DELETE_COPY(AutoProtect);
576 void set(
const void *
object);
588 AutoProtect(
const void *
object);
595 inline operator bool()
const {
596 return object != NULL;
599 inline bool operator!()
const {
600 return object == NULL;
605class autoprotect :
public AutoProtect
608 inline autoprotect() : AutoProtect() {};
610 inline autoprotect(
const T *
object) : AutoProtect(object) {};
612 inline void set(
const T *
object) {
613 AutoProtect::set(
object);
616 inline void release() {
617 AutoProtect::release();
620 inline autoprotect& operator=(
const T*
object) {
621 AutoProtect::set(
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;
668 Thread(
size_t stack = 0);
679 virtual bool is_active(
void)
const;
682 class __EXPORT Local :
public LinkedObject
688 static LinkedObject *list;
690 __DELETE_COPY(Local);
695 virtual void release(
void *instance) = 0;
697 virtual void *allocate();
704 void set(
void *instance);
708 inline void clear() {
719 void setPriority(
void);
725 static void yield(
void);
731 static void sleep(timeout_t timeout);
739 static Thread *get(
void);
744 virtual void run(
void) = 0;
759 virtual void exit(
void);
764 static void init(
void);
769 static size_t cache(
void);
776 static void policy(
int polid);
782 static void concurrency(
int level);
790 static bool equal(pthread_t thread1, pthread_t thread2);
796 static pthread_t self(
void);
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;
840 JoinableThread(
size_t size = 0);
846 virtual ~JoinableThread();
855 bool is_active(
void)
const __OVERRIDE;
857 virtual void run(
void) __OVERRIDE = 0;
869 void start(
int priority = 0);
875 inline void background(
void) {
887class __EXPORT DetachedThread :
public Thread
890 __DELETE_COPY(DetachedThread);
899 DetachedThread(
size_t size = 0);
916 void exit(
void) __OVERRIDE;
918 bool is_active(
void)
const __OVERRIDE;
920 virtual void run(
void) __OVERRIDE = 0;
929 void start(
int priority = 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.
Realtime timers and timer queues.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Condition classes for thread sychronization and timing.
Locking protocol classes for member function automatic operations.
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.
class __attribute__((visibility("default"))) JoinableThread class __attribute__((visibility("default"))) DetachedThread typedef TimedEvent timedevent_t
A child thread object that may be joined by parent.
RecursiveMutex rexlock_t
Convenience type for using recursive exclusive locks.