Go to the documentation of this file.
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;
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;
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;
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;
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) {
986 inline size_t peek(Buffer &b,
void *o) {
virtual void onTimer(void)
A derivable method to call when the timout is expired.
size_t post(void *buf, timeout_t timeout=0)
Post an object into the buffer and enable a waiting thread to receive it.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
A buffer class that holds a known capacity of fixed sized objects defined during creation.
void set(void)
Set (update) the timer with current time.
void unlock(void)
Unlock the conditional's supporting mutex.
static void yield(void)
Yield execution context of the current thread.
void modify(void)
Exclusive mode write thread scheduling.
void decTimer(timeout_t timeout)
Adjust a timeout based on the current time reference value either from object creation or the last se...
void incTimer(timeout_t timeout)
Set a timeout based on the current time reference value either from object creation or the last setTi...
void wait(void)
A simple wait until triggered.
~ReadLock()
Post the semaphore automatically.
virtual size_t onWait(void *buf)=0
Invoke derived class object request from buffer.
Common namespace for all ucommon objects.
~SemaphoreLock()
Post the semaphore automatically.
MutexCounter()
Create and optionally name a mutex protected counter.
FixedBuffer(size_t capacity, size_t objsize)
Create a buffer of known capacity for objects of a specified size.
virtual ~ThreadQueue()
Destroy the queue.
static const size_t timeout
value to return when a timed operation returned with a timeout.
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...
size_t onPost(void *buf)
Post an object of the appropriate size into the buffer.
~WriteLock()
Post the semaphore automatically.
FixedBuffer(const FixedBuffer &fb)
Create a copy of an existing fixed size buffer and duplicate it's contents.
MutexCounter(int initial)
Create and optionally name a mutex protected counter with an initial value.
Event notification to manage scheduled realtime threads.
size_t peek(void *buf)
Peek at the current content (first object) in the buffer.
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 getUsed(void) const
Return the current capacity in use for the buffer.
void release(void)
Release the semaphore after waiting for it.
Buffer(size_t capacity)
Create a buffer object of known capacity.
bool isValid(void)
New virtual to test if buffer is a valid object.
virtual void startQueue(void)
Start of dequeing.
void reset(void)
Reset triggered conditional.
This class is used to access non-reentrant date and time functions in the standard C library.
Common C++ generic string class.
void sleepTimer(void)
Sleep until the current timer expires.
A generic and portable implementation of Read/Write locking.
Somewhat generic queue processing class to establish a producer consumer queue.
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
void post(const void *data, unsigned len)
Put some unspecified data into this queue.
virtual size_t onPost(void *buf)=0
Invoke derived class posting of object to buffer.
void setTimer(timeout_t timeout)
Set the queue timeout.
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
virtual bool isValid(void)
New virtual to test if buffer is a valid object.
virtual ~FixedBuffer()
Destroy the fixed buffer and free the memory used to store objects.
void release(void)
Release the lock.
virtual void stopQueue(void)
End of dequeing, we expect the queue is empty for now.
virtual ~Buffer()
In derived functions, may be used to free the actual memory used to hold buffered data.
~MutexLock()
Release the mutex automatically.
virtual size_t onPeek(void *buf)=0
Invoke derived class buffer peeking method.
timeout_t getTimer(void) const
This is used by service threads to determine how much time remains before the timer expires based on ...
void endTimer(void)
This is used to "disable" the service thread from expiring the timer object.
void lock(void)
Lock the conditional's supporting mutex.
void wait(void)
Wait until the semphore usage count is less than the thread limit.
A portable counting semaphore class.
void access(void)
Access mode shared thread scheduling.
virtual void runQueue(void *data)=0
Virtual callback method to handle processing of a queued data items.
The SemaphoreLock class is used to protect a section of code through a semaphore so that only x insta...
ReadLock(ThreadLock &_tl)
Wait for read access.
size_t wait(void *buf, timeout_t timeout=0)
Let one or more threads wait for an object to become available in the buffer.
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.
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...
size_t getSize(void) const
Return the capacity of the buffer as specified at creation.
ThreadQueue(const char *id, int pri, size_t stack=0)
Create instance of our queue and give it a process priority.
The conditional is a common base for other thread synchronizing classes.
A copy-on-write string class that operates by reference count.
size_t onWait(void *buf)
Wait for and return a fixed object in the buffer.
size_t onPeek(void *buf)
Return the first object in the buffer.
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
Portable recursive exclusive lock.
void signal(void)
Signal pending event.
MutexLock(Mutex &_mutex)
Acquire the mutex.
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
WriteLock(ThreadLock &_tl)
Wait for write access.