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;
654struct timespec *getTimeout(
struct timespec *spec, timeout_t timeout);
657#if !defined(_MSWINDOWS_) || defined(_MSTHREADS_)
658inline struct tm *localtime_r(
const time_t *t,
struct tm *b) {
659 return SysTime::getLocalTime(t, b);
662inline char *ctime_r(
const time_t *t,
char *buf) {
666inline struct tm *gmtime_r(
const time_t *t,
struct tm *b) {
667 return SysTime::getGMTTime(t, b);
670inline char *asctime_r(
const struct tm *tm,
char *b) {
675inline Thread *getThread(
void) {
676 return Thread::get();
701class __EXPORT Buffer :
public Mutex
703class __EXPORT
Buffer :
public Conditional
708 HANDLE sem_head, sem_tail;
780 size_t wait(
void *buf, timeout_t timeout = 0);
790 size_t post(
void *buf, timeout_t timeout = 0);
817 char *buf, *head, *tail;
885class __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;
971 void post(
const void *data,
unsigned len);
976inline size_t get(
Buffer &b,
void *o, timeout_t t = 0) {
981inline size_t put(Buffer &b,
void *o, timeout_t t = 0) {
986inline size_t peek(Buffer &b,
void *o) {
Common namespace for all ucommon objects.
void unlock(void)
Unlock the conditional's supporting mutex.
void lock(void)
Lock the conditional's supporting mutex.
The conditional is a common base for other thread synchronizing classes.
void access(void)
Access mode shared thread scheduling.
void modify(void)
Exclusive mode write thread scheduling.
A portable counting semaphore class.
void release(void)
Release the semaphore after waiting for it.
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.
A generic and portable implementation of Read/Write locking.
void release(void)
Release the lock.
Event notification to manage scheduled realtime threads.
void reset(void)
Reset triggered conditional.
void wait(void)
A simple wait until triggered.
void signal(void)
Signal pending event.
Portable recursive exclusive lock.
static void yield(void)
Yield execution context of the current thread.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
A child thread object that may be joined by parent.
void set(void)
Set (update) the timer with current time.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
MutexCounter(int initial)
Create and optionally name a mutex protected counter with an initial value.
MutexCounter()
Create and optionally name a mutex protected counter.
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
~MutexLock()
Release the mutex automatically.
MutexLock(Mutex &_mutex)
Acquire the mutex.
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
ReadLock(ThreadLock &_tl)
Wait for read access.
~ReadLock()
Post the semaphore automatically.
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
~WriteLock()
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...
~SemaphoreLock()
Post the semaphore automatically.
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
This class is used to access non-reentrant date and time functions in the standard C library.
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
void sleepTimer(void)
Sleep until the current timer expires.
void setTimer(timeout_t timeout=0)
Set a new start time for the object based on when this call is made and optionally activate the timer...
timeout_t getElapsed(void) const
This is used to determine how much time has elapsed since a timer port setTimer benchmark time was in...
void decTimer(timeout_t timeout)
Adjust a timeout based on the current time reference value either from object creation or the last se...
void endTimer(void)
This is used to "disable" the service thread from expiring the timer object.
timeout_t getTimer(void) const
This is used by service threads to determine how much time remains before the timer expires based on ...
void incTimer(timeout_t timeout)
Set a timeout based on the current time reference value either from object creation or the last setTi...
TimerPort()
Create a timer, mark it as inactive, and set the initial "start" time to the creation time of the tim...
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
size_t getSize(void) const
Return the capacity of the buffer as specified at creation.
virtual bool isValid(void)
New virtual to test if buffer is a valid object.
virtual size_t onPeek(void *buf)=0
Invoke derived class buffer peeking method.
size_t wait(void *buf, timeout_t timeout=0)
Let one or more threads wait for an object to become available in the buffer.
virtual size_t onPost(void *buf)=0
Invoke derived class posting of object to buffer.
virtual size_t onWait(void *buf)=0
Invoke derived class object request from buffer.
size_t post(void *buf, timeout_t timeout=0)
Post an object into the buffer and enable a waiting thread to receive it.
size_t getUsed(void) const
Return the current capacity in use for the buffer.
static const size_t timeout
value to return when a timed operation returned with a timeout.
size_t peek(void *buf)
Peek at the current content (first object) in the buffer.
virtual ~Buffer()
In derived functions, may be used to free the actual memory used to hold buffered data.
Buffer(size_t capacity)
Create a buffer object of known capacity.
A buffer class that holds a known capacity of fixed sized objects defined during creation.
bool isValid(void)
New virtual to test if buffer is a valid object.
FixedBuffer(size_t capacity, size_t objsize)
Create a buffer of known capacity for objects of a specified size.
size_t onPost(void *buf)
Post an object of the appropriate size into the buffer.
virtual ~FixedBuffer()
Destroy the fixed buffer and free the memory used to store objects.
size_t onWait(void *buf)
Wait for and return a fixed object in the buffer.
FixedBuffer(const FixedBuffer &fb)
Create a copy of an existing fixed size buffer and duplicate it's contents.
size_t onPeek(void *buf)
Return the first object in the buffer.
Somewhat generic queue processing class to establish a producer consumer queue.
void setTimer(timeout_t timeout)
Set the queue timeout.
virtual void startQueue(void)
Start of dequeing.
virtual void onTimer(void)
A derivable method to call when the timout is expired.
virtual void stopQueue(void)
End of dequeing, we expect the queue is empty for now.
virtual ~ThreadQueue()
Destroy the queue.
ThreadQueue(const char *id, int pri, size_t stack=0)
Create instance of our queue and give it a process priority.
virtual void runQueue(void *data)=0
Virtual callback method to handle processing of a queued data items.
void post(const void *data, unsigned len)
Put some unspecified data into this queue.
Common C++ generic string class.