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();
65 inline Mutex() : RecursiveMutex() {}
67 inline void enterMutex(
void)
70 inline void leaveMutex(
void)
73 inline bool tryEnterMutex(
void)
76 inline void enter(
void)
79 inline void leave(
void)
82 inline bool test(
void)
154 { mutex.enterMutex(); }
161 { mutex.leaveMutex(); }
167 inline ThreadLock() :
ucommon::ThreadLock() {}
169 inline void readLock(
void)
172 inline void writeLock(
void)
175 inline void tryReadLock(
void)
178 inline void tryWriteLock(
void)
270 inline Conditional() :
ucommon::Conditional() {}
272 bool wait(timeout_t timeout,
bool locked =
false);
274 void signal(
bool broadcast);
276 inline void enterMutex(
void)
279 inline void leaveMutex(
void)
286 inline Semaphore(
unsigned size = 0) :
ucommon::Semaphore(size) {}
288 inline bool wait(timeout_t timeout)
291 inline void wait(
void)
294 inline void post(
void)
339 inline Event() :
ucommon::TimedEvent() {}
341 inline void wait(
void)
344 inline bool wait(timeout_t timeout)
347 inline void signal(
void)
350 inline void reset(
void)
353 inline void set(timeout_t timeout = 0)
373 bool detached, terminated;
379 Thread(
int pri = 0,
size_t stack = 0);
383 inline void map(
void)
384 {JoinableThread::map();}
386 virtual void initial(
void);
387 virtual void notify(Thread *thread);
388 virtual void final(void);
389 virtual void run(
void) = 0;
391 void terminate(
void);
398 inline void join(
void)
399 {JoinableThread::join();}
401 inline void sync(
void)
404 static inline Thread *
get(void)
405 {
return (Thread *)JoinableThread::get();}
407 inline static void yield(
void)
410 inline static void sleep(timeout_t msec = TIMEOUT_INF)
413 bool isRunning(
void);
422 static Throw getException(
void);
429 static void setException(Throw mode);
434 inline pthread_t getId(
void)
470 static time_t getTime(time_t *tloc = NULL);
471 static time_t time(time_t *tloc)
472 {
return getTime(tloc);}
474 static int getTimeOfDay(
struct timeval *tp);
475 static int gettimeofday(
struct timeval *tp,
struct timezone *)
476 {
return getTimeOfDay(tp);}
478 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
479 static struct tm *locatime(
const time_t *clock,
struct tm *result)
480 {
return getLocalTime(clock, result);}
482 static struct tm *getGMTTime(
const time_t *clock,
struct tm *result);
483 static struct tm *gmtime(
const time_t *clock,
struct tm *result)
484 {
return getGMTTime(clock, result);}
500 struct timeval timer;
523 void setTimer(timeout_t timeout = 0);
534 void incTimer(timeout_t timeout);
545 void decTimer(timeout_t timeout);
551 void sleepTimer(
void);
571 timeout_t getTimer(
void)
const;
582 timeout_t getElapsed(
void)
const;
586 struct timespec *getTimeout(
struct timespec *spec, timeout_t timeout);
589 #if !defined(_MSWINDOWS_) || defined(_MSTHREADS_)
590 inline struct tm *localtime_r(
const time_t *t,
struct tm *b)
591 {
return SysTime::getLocalTime(t, b);}
593 inline char *ctime_r(
const time_t *t,
char *buf)
596 inline struct tm *gmtime_r(
const time_t *t,
struct tm *b)
597 {
return SysTime::getGMTTime(t, b);}
599 inline char *asctime_r(
const struct tm *tm,
char *b)
600 {
return asctime(tm);}
603 inline Thread *getThread(
void)
604 {
return Thread::get();}
628 class __EXPORT Buffer :
public Mutex
630 class __EXPORT
Buffer :
public Conditional
635 HANDLE sem_head, sem_tail;
646 virtual size_t onPeek(
void *buf) = 0;
653 virtual size_t onWait(
void *buf) = 0;
660 virtual size_t onPost(
void *buf) = 0;
705 size_t wait(
void *buf, timeout_t timeout = 0);
715 size_t post(
void *buf, timeout_t timeout = 0);
723 size_t peek(
void *buf);
729 virtual bool isValid(
void);
742 char *buf, *head, *tail;
751 size_t onPeek(
void *buf);
758 size_t onWait(
void *buf);
765 size_t onPost(
void *buf);
810 class __EXPORT
ThreadQueue :
public Mutex,
public Thread,
public Semaphore
816 typedef struct _data {
825 data_t *first, *last;
832 virtual void final();
838 virtual void startQueue(
void);
845 virtual void stopQueue(
void);
850 virtual void onTimer(
void);
860 virtual void runQueue(
void *data) = 0;
870 ThreadQueue(
const char *
id,
int pri,
size_t stack = 0);
884 void setTimer(timeout_t timeout);
894 void post(
const void *data,
unsigned len);
899 inline size_t get(
Buffer &b,
void *o, timeout_t t = 0)
900 {
return b.wait(o, t);}
904 {
return b.
post(o, t);}
The conditional is a common base for other thread synchronizing classes.
void reset(void)
Reset triggered conditional.
Portable recursive exclusive lock.
Event notification to manage scheduled realtime threads.
Somewhat generic queue processing class to establish a producer consumer queue.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
void wait(void)
Wait until the semphore usage count is less than the thread limit.
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...
size_t getUsed(void)
Return the current capacity in use for the buffer.
ReadLock(ThreadLock &_tl)
Wait for read access.
void start(DetachedThread *thread, int priority=0)
Convenience function to start a detached thread.
void lock(void)
Lock the conditional's supporting mutex.
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
void release(void)
Release the semaphore after waiting for it.
void wait(void)
A simple wait until triggered.
size_t post(void *buf, timeout_t timeout=0)
Post an object into the buffer and enable a waiting thread to receive it.
MutexLock(Mutex &_mutex)
Acquire the mutex.
This class is used to access non-reentrant date and time functions in the standard C library...
A child thread object that may be joined by parent.
A buffer class that holds a known capacity of fixed sized objects defined during creation.
~SemaphoreLock()
Post the semaphore automatically.
Common namespace for all ucommon objects.
Common C++ generic string class.
~WriteLock()
Post the semaphore automatically.
size_t peek(Buffer &b, void *o)
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
A generic and portable implimentation of Read/Write locking.
void wait(semaphore_t &semaphore, timeout_t timeout=Timer::inf)
Convenience function to wait on a semaphore.
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
~MutexLock()
Release the mutex automatically.
void set(void)
Set (update) the timer with current time.
static const size_t timeout
value to return when a timed operation returned with a timeout.
The SemaphoreLock class is used to protect a section of code through a semaphore so that only x insta...
size_t getSize(void)
Return the capacity of the buffer as specified at creation.
void unlock(void)
Unlock the conditional's supporting mutex.
size_t peek(void *buf)
Peek at the current content (first object) in the buffer.
void modify(void)
Exclusive mode write thread scheduling.
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
void release(rexlock_t &lock)
Convenience function to release a shared recursive mutex lock.
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
~ReadLock()
Post the semaphore automatically.
A portable counting semaphore class.
void signal(void)
Signal pending event.
void access(void)
Access mode shared thread scheduling.
WriteLock(ThreadLock &_tl)
Wait for write access.
void lock(rexlock_t &lock)
Convenience function to lock a shared recursive mutex lock.
static void yield(void)
Yield execution context of the current thread.
size_t put(Buffer &b, void *o, timeout_t t=0)
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
void release(void)
Release the lock.