23 #ifndef COMMONCPP_THREAD_H_
24 #define COMMONCPP_THREAD_H_
26 #ifndef COMMONCPP_CONFIG_H_
27 #include <commoncpp/config.h>
30 #ifndef COMMONCPP_STRING_H_
34 #define ENTER_CRITICAL enterMutex();
35 #define LEAVE_CRITICAL leaveMutex();
42 inline Mutex() : RecursiveMutex() {};
44 inline void enterMutex(
void)
47 inline void leaveMutex(
void)
50 inline bool tryEnterMutex(
void)
53 inline void enter(
void)
56 inline void leave(
void)
59 inline bool test(
void)
131 { mutex.enterMutex(); }
138 { mutex.leaveMutex(); }
144 inline ThreadLock() : ucommon::ThreadLock() {};
146 inline void readLock(
void)
149 inline void writeLock(
void)
152 inline void tryReadLock(
void)
155 inline void tryWriteLock(
void)
251 void signal(
bool broadcast);
253 inline void enterMutex(
void)
256 inline void leaveMutex(
void)
263 inline Semaphore(
unsigned size=0) : ucommon::Semaphore(size) {};
268 inline void post(
void)
313 inline Event() : TimedEvent() {};
315 inline void wait(
void)
324 inline void reset(
void)
344 bool detached, terminated;
350 Thread(
int pri = 0,
size_t stack = 0);
354 inline void map(
void)
357 virtual void initial(
void);
358 virtual void notify(Thread *thread);
359 virtual void final(void);
360 virtual void run(
void) = 0;
362 void terminate(
void);
369 inline void join(
void)
372 inline void sync(
void)
375 static inline Thread *
get(void)
378 inline static void yield(
void)
384 bool isRunning(
void);
393 static Throw getException(
void);
400 static void setException(Throw mode);
405 inline pthread_t getId(
void)
441 static time_t getTime(time_t *tloc = NULL);
442 static time_t time(time_t *tloc)
443 {
return getTime(tloc); };
445 static int getTimeOfDay(
struct timeval *tp);
446 static int gettimeofday(
struct timeval *tp,
struct timezone *)
447 {
return getTimeOfDay(tp); };
449 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
450 static struct tm *locatime(
const time_t *clock,
struct tm *result)
451 {
return getLocalTime(clock, result); };
453 static struct tm *getGMTTime(
const time_t *clock,
struct tm *result);
454 static struct tm *gmtime(
const time_t *clock,
struct tm *result)
455 {
return getGMTTime(clock, result);};
471 struct timeval timer;
522 void sleepTimer(
void);
557 struct timespec *getTimeout(
struct timespec *spec,
timeout_t timeout);
560 inline struct tm *localtime_r(
const time_t *t,
struct tm *b)
561 {
return SysTime::getLocalTime(t, b);}
563 inline char *ctime_r(
const time_t *t,
char *buf)
566 inline struct tm *gmtime_r(
const time_t *t,
struct tm *b)
567 {
return SysTime::getGMTTime(t, b);}
569 inline char *asctime_r(
const struct tm *tm,
char *b)
570 {
return asctime(tm);}
572 inline Thread *getThread(
void)
573 {
return Thread::get();}
597 class __EXPORT Buffer :
public Mutex
599 class __EXPORT
Buffer :
public Conditional
604 HANDLE sem_head, sem_tail;
615 virtual size_t onPeek(
void *buf) = 0;
622 virtual size_t onWait(
void *buf) = 0;
629 virtual size_t onPost(
void *buf) = 0;
692 size_t peek(
void *buf);
711 char *buf, *head, *tail;
779 class __EXPORT
ThreadQueue :
public Mutex,
public Thread,
public Semaphore
785 typedef struct _data {
794 data_t *first, *last;
801 virtual void final();
807 virtual void startQueue(
void);
814 virtual void stopQueue(
void);
819 virtual void onTimer(
void);
829 virtual void runQueue(
void *data) = 0;
839 ThreadQueue(
const char *
id,
int pri,
size_t stack = 0);
863 void post(
const void *data,
unsigned len);
869 {
return b.wait(o, t);}
873 {
return b.
post(o, t);}
virtual bool isValid(void)
New virtual to test if buffer is a valid object.
Semaphore(unsigned count=0)
Construct a semaphore with an initial count of threads to permit.
void release(void)
Release the semaphore after waiting for it.
virtual void run(void)=0
Abstract interface for thread context run method.
void unlock(void)
Unlock the conditional's supporting mutex.
Common C++ generic string class.
virtual size_t onPost(void *buf)=0
Invoke derived class posting of object to buffer.
void reset(void)
Reset triggered conditional.
A portable counting semaphore class.
static void yield(void)
Yield execution context of the current thread.
void modify(void)
Exclusive mode write thread scheduling.
void lock(void)
Lock the conditional's supporting mutex.
Event notification to manage scheduled realtime threads.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
void join(void)
Join thread with parent.
The conditional is a common base for other thread synchronizing classes.
void wait(void)
Wait (block) until signalled.
virtual size_t onPeek(void *buf)=0
Invoke derived class buffer peeking method.
void signal(void)
Signal the conditional to release one waiting thread.
void access(void)
Access mode shared thread scheduling.
This class is used to access non-reentrant date and time functions in the standard C library...
A generic and portable implimentation of Read/Write locking.
size_t getUsed(void)
Return the current capacity in use for the buffer.
ReadLock(ThreadLock &_tl)
Wait for read access.
Somewhat generic queue processing class to establish a producer consumer queue.
void signal(void)
Signal pending event.
Portable recursive exclusive lock.
Buffer(size_t capacity)
Create a buffer object of known capacity.
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.
void unlock(void)
Unlock the conditional's supporting mutex.
Thread(size_t stack=0)
Create a thread object that will have a preset stack size.
A buffer class that holds a known capacity of fixed sized objects defined during creation.
void release(void)
Release the lock.
virtual size_t onWait(void *buf)=0
Invoke derived class object request from buffer.
~SemaphoreLock()
Post the semaphore automatically.
size_t peek(Buffer &b, void *o)
unsigned long timeout_t
Typedef for millisecond timer values.
WriteLock(ThreadLock &_tl)
Wait for write access.
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
virtual void exit(void)
Exit the thread context.
ThreadLock()
Create an instance of a rwlock.
~MutexLock()
Release the mutex automatically.
virtual ~Thread()
Destroy thread object, thread-specific data, and execution context.
static const size_t timeout
value to return when a timed operation returned with a timeout.
A child thread object that may be joined by parent.
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.
A copy-on-write string class that operates by reference count.
void start(int priority=0)
Start execution of child context.
Timer ports are used to provide synchronized timing events when managed under a "service thread" such...
size_t peek(void *buf)
Peek at the current content (first object) in the buffer.
The MutexLock class is used to protect a section of code so that at any given time only a single thre...
void wait(void)
Wait until the semphore usage count is less than the thread limit.
void release(void)
Release or decrease locking.
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
static Thread * get(void)
Get mapped thread object.
~ReadLock()
Post the semaphore automatically.
~WriteLock()
Post the semaphore automatically.
void wait(void)
A simple wait until triggered.
void map(void)
Map thread for get method.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
void lock(void)
Acquire or increase locking.
virtual ~Buffer()
In derived functions, may be used to free the actual memory used to hold buffered data...
size_t put(Buffer &b, void *o, timeout_t t=0)
Conditional()
Initialize and construct conditional.
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.