44 #ifndef COMMONCPP_THREAD_H_ 45 #define COMMONCPP_THREAD_H_ 47 #ifndef COMMONCPP_CONFIG_H_ 48 #include <commoncpp/config.h> 51 #ifndef COMMONCPP_STRING_H_ 55 #define ENTER_CRITICAL enterMutex(); 56 #define LEAVE_CRITICAL leaveMutex(); 68 inline Mutex() : RecursiveMutex() {}
70 inline void enterMutex(
void) {
71 RecursiveMutex::lock();
74 inline void leaveMutex(
void) {
75 RecursiveMutex::release();
78 inline bool tryEnterMutex(
void) {
79 return RecursiveMutex::lock(0l);
82 inline void enter(
void) {
83 RecursiveMutex::lock();
86 inline void leave(
void) {
87 RecursiveMutex::release();
90 inline bool test(
void) {
91 return RecursiveMutex::lock(0l);
110 volatile int counter;
182 __DELETE_COPY(ThreadLock);
185 inline ThreadLock() :
ucommon::RWLock() {}
187 inline void readLock(
void) {
191 inline void writeLock(
void) {
195 inline void tryReadLock(
void) {
199 inline void tryWriteLock(
void) {
203 inline void unlock(
void) {
301 __DELETE_COPY(Conditional);
304 inline Conditional() :
ucommon::Conditional() {}
306 bool wait(timeout_t timeout,
bool locked =
false);
308 void signal(
bool broadcast);
310 inline void enterMutex(
void) {
314 inline void leaveMutex(
void) {
322 inline Semaphore(
unsigned size = 0) :
ucommon::Semaphore(size) {}
324 inline bool wait(timeout_t timeout) {
328 inline void wait(
void) {
332 inline void post(
void) {
380 __DELETE_COPY(Event);
383 inline Event() :
ucommon::TimedEvent() {}
385 inline void wait(
void) {
389 inline bool wait(timeout_t timeout) {
393 inline void signal(
void) {
397 inline void reset(
void) {
401 inline void set(timeout_t timeout = 0) {
422 bool detached, terminated;
427 __DELETE_COPY(Thread);
430 Thread(
int pri = 0,
size_t stack = 0);
434 inline void map(
void) {
435 JoinableThread::map();
438 virtual void initial(
void);
439 virtual void notify(Thread *thread);
440 virtual void final(void);
441 virtual void run(
void) __OVERRIDE = 0;
443 void terminate(
void);
450 inline void join(
void) {
451 JoinableThread::join();
454 inline void sync(
void) {
458 static inline Thread *get(
void) {
459 return (Thread *)JoinableThread::get();
462 inline static void yield(
void) {
466 inline static void sleep(timeout_t msec = TIMEOUT_INF) {
470 bool isRunning(
void);
479 static Throw getException(
void);
486 static void setException(Throw mode);
491 inline pthread_t getId(
void)
const {
531 static time_t getTime(time_t *tloc = NULL);
532 static time_t time(time_t *tloc) {
533 return getTime(tloc);
536 static int getTimeOfDay(
struct timeval *tp);
537 static int gettimeofday(
struct timeval *tp,
struct timezone *) {
538 return getTimeOfDay(tp);
541 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
542 static struct tm *locatime(
const time_t *clock,
struct tm *result) {
543 return getLocalTime(clock, result);
546 static struct tm *getGMTTime(
const time_t *clock,
struct tm *result);
547 static struct tm *gmtime(
const time_t *clock,
struct tm *result) {
548 return getGMTTime(clock, result);
566 struct timeval timer;
591 void setTimer(timeout_t timeout = 0);
602 void incTimer(timeout_t timeout);
613 void decTimer(timeout_t timeout);
619 void sleepTimer(
void);
639 timeout_t getTimer(
void)
const;
650 timeout_t getElapsed(
void)
const;
654 struct timespec *getTimeout(
struct timespec *spec, timeout_t timeout);
657 #if !defined(_MSWINDOWS_) || defined(_MSTHREADS_) 658 inline struct tm *localtime_r(
const time_t *t,
struct tm *b) {
659 return SysTime::getLocalTime(t, b);
662 inline char *ctime_r(
const time_t *t,
char *buf) {
666 inline struct tm *gmtime_r(
const time_t *t,
struct tm *b) {
667 return SysTime::getGMTTime(t, b);
670 inline char *asctime_r(
const struct tm *tm,
char *b) {
675 inline Thread *getThread(
void) {
676 return Thread::get();
701 class __EXPORT Buffer :
public Mutex
703 class __EXPORT
Buffer :
public Conditional
708 HANDLE sem_head, sem_tail;
719 virtual size_t onPeek(
void *buf) = 0;
726 virtual size_t onWait(
void *buf) = 0;
733 virtual size_t onPost(
void *buf) = 0;
780 size_t wait(
void *buf, timeout_t timeout = 0);
790 size_t post(
void *buf, timeout_t timeout = 0);
798 size_t peek(
void *buf);
804 virtual bool isValid(
void);
817 char *buf, *head, *tail;
826 size_t onPeek(
void *buf) __OVERRIDE;
833 size_t onWait(
void *buf) __OVERRIDE;
840 size_t onPost(
void *buf) __OVERRIDE;
867 bool isValid(
void) __OVERRIDE;
885 class __EXPORT
ThreadQueue :
public Mutex,
public Thread,
public Semaphore
888 void run(
void) __FINAL;
893 typedef struct _data {
902 data_t *first, *last;
909 virtual void final() __OVERRIDE;
915 virtual void startQueue(
void);
922 virtual void stopQueue(
void);
927 virtual void onTimer(
void);
937 virtual void runQueue(
void *data) = 0;
947 ThreadQueue(
const char *
id,
int pri,
size_t stack = 0);
961 void setTimer(timeout_t timeout);
971 void post(
const void *data,
unsigned len);
976 inline size_t get(
Buffer &b,
void *o, timeout_t t = 0) {
981 inline size_t put(
Buffer &b,
void *o, timeout_t t = 0) {
A buffer class that holds a known capacity of fixed sized objects defined during creation.
The conditional is a common base for other thread synchronizing classes.
This class is used to access non-reentrant date and time functions in the standard C library.
~MutexLock()
Release the mutex automatically.
static const size_t timeout
value to return when a timed operation returned with a timeout.
size_t put(Buffer &b, void *o, timeout_t t=0)
~SemaphoreLock()
Post the semaphore automatically.
WriteLock(ThreadLock &_tl)
Wait for write access.
The SemaphoreLock class is used to protect a section of code through a semaphore so that only x insta...
MutexLock(Mutex &_mutex)
Acquire the mutex.
A copy-on-write string class that operates by reference count.
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
size_t peek(void *buf)
Peek at the current content (first object) in the buffer.
void release(void)
Release the lock.
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
void signal(void)
Signal pending event.
void lock(void)
Lock the conditional's supporting mutex.
ReadLock(ThreadLock &_tl)
Wait for read access.
void access(void)
Access mode shared thread scheduling.
~ReadLock()
Post the semaphore automatically.
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
void wait(void)
Wait until the semphore usage count is less than the thread limit.
A generic and portable implementation of Read/Write locking.
Somewhat generic queue processing class to establish a producer consumer queue.
static void yield(void)
Yield execution context of the current thread.
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
void set(void)
Set (update) the timer with current time.
void unlock(void)
Unlock the conditional's supporting mutex.
size_t post(void *buf, timeout_t timeout=0)
Post an object into the buffer and enable a waiting thread to receive it.
void modify(void)
Exclusive mode write thread scheduling.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
Common C++ generic string class.
Portable recursive exclusive lock.
size_t wait(void *buf, timeout_t timeout=0)
Let one or more threads wait for an object to become available in the buffer.
Common namespace for all ucommon objects.
void release(void)
Release the semaphore after waiting for it.
void reset(void)
Reset triggered conditional.
size_t getSize(void) const
Return the capacity of the buffer as specified at creation.
void wait(void)
A simple wait until triggered.
size_t getUsed(void) const
Return the current capacity in use for the buffer.
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
A child thread object that may be joined by parent.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
~WriteLock()
Post the semaphore automatically.
size_t get(Buffer &b, void *o, timeout_t t=0)
Event notification to manage scheduled realtime threads.
size_t peek(Buffer &b, void *o)
A portable counting semaphore class.