43 #ifndef COMMONCPP_THREAD_H_
44 #define COMMONCPP_THREAD_H_
46 #ifndef COMMONCPP_CONFIG_H_
47 #include <commoncpp/config.h>
50 #ifndef COMMONCPP_STRING_H_
54 #define ENTER_CRITICAL enterMutex();
55 #define LEAVE_CRITICAL leaveMutex();
62 inline Mutex() : RecursiveMutex() {};
64 inline void enterMutex(
void)
67 inline void leaveMutex(
void)
70 inline bool tryEnterMutex(
void)
73 inline void enter(
void)
76 inline void leave(
void)
79 inline bool test(
void)
151 { mutex.enterMutex(); }
158 { mutex.leaveMutex(); }
166 inline void readLock(
void)
169 inline void writeLock(
void)
172 inline void tryReadLock(
void)
175 inline void tryWriteLock(
void)
271 void signal(
bool broadcast);
273 inline void enterMutex(
void)
276 inline void leaveMutex(
void)
288 inline void wait(
void)
291 inline void post(
void)
336 inline Event() :
ucommon::TimedEvent() {};
338 inline void wait(
void)
347 inline void reset(
void)
370 bool detached, terminated;
376 Thread(
int pri = 0,
size_t stack = 0);
380 inline void map(
void)
383 virtual void initial(
void);
384 virtual void notify(Thread *thread);
385 virtual void final(void);
386 virtual void run(
void) = 0;
388 void terminate(
void);
395 inline void join(
void)
398 inline void sync(
void)
401 static inline Thread *
get(void)
404 inline static void yield(
void)
410 bool isRunning(
void);
419 static Throw getException(
void);
426 static void setException(Throw mode);
431 inline pthread_t getId(
void)
467 static time_t getTime(time_t *tloc = NULL);
468 static time_t time(time_t *tloc)
469 {
return getTime(tloc); };
471 static int getTimeOfDay(
struct timeval *tp);
472 static int gettimeofday(
struct timeval *tp,
struct timezone *)
473 {
return getTimeOfDay(tp); };
475 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
476 static struct tm *locatime(
const time_t *clock,
struct tm *result)
477 {
return getLocalTime(clock, result); };
479 static struct tm *getGMTTime(
const time_t *clock,
struct tm *result);
480 static struct tm *gmtime(
const time_t *clock,
struct tm *result)
481 {
return getGMTTime(clock, result);};
497 struct timeval timer;
548 void sleepTimer(
void);
583 struct timespec *getTimeout(
struct timespec *spec,
timeout_t timeout);
586 inline struct tm *localtime_r(
const time_t *t,
struct tm *b)
587 {
return SysTime::getLocalTime(t, b);}
589 inline char *ctime_r(
const time_t *t,
char *buf)
592 inline struct tm *gmtime_r(
const time_t *t,
struct tm *b)
593 {
return SysTime::getGMTTime(t, b);}
595 inline char *asctime_r(
const struct tm *tm,
char *b)
596 {
return asctime(tm);}
598 inline Thread *getThread(
void)
599 {
return Thread::get();}
623 class __EXPORT Buffer :
public Mutex
625 class __EXPORT
Buffer :
public Conditional
630 HANDLE sem_head, sem_tail;
641 virtual size_t onPeek(
void *buf) = 0;
648 virtual size_t onWait(
void *buf) = 0;
655 virtual size_t onPost(
void *buf) = 0;
718 size_t peek(
void *buf);
737 char *buf, *head, *tail;
805 class __EXPORT
ThreadQueue :
public Mutex,
public Thread,
public Semaphore
811 typedef struct _data {
820 data_t *first, *last;
827 virtual void final();
833 virtual void startQueue(
void);
840 virtual void stopQueue(
void);
845 virtual void onTimer(
void);
855 virtual void runQueue(
void *data) = 0;
865 ThreadQueue(
const char *
id,
int pri,
size_t stack = 0);
889 void post(
const void *data,
unsigned len);
895 {
return b.wait(o, t);}
899 {
return b.
post(o, t);}
virtual bool isValid(void)
New virtual to test if buffer is a valid object.
The conditional is a common base for other thread synchronizing classes.
Common namespace for all ucommon objects.
Thread(size_t stack=0)
Create a thread object that will have a preset stack size.
Portable recursive exclusive lock.
virtual size_t onPost(void *buf)=0
Invoke derived class posting of object to buffer.
static Thread * get(void)
Get mapped thread object.
A generic and portable implimentation of Read/Write locking.
void reset(void)
Reset triggered conditional.
Somewhat generic queue processing class to establish a producer consumer queue.
Conditional()
Initialize and construct conditional.
void release(void)
Release the lock.
void signal(void)
Signal the conditional to release one waiting thread.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
virtual void exit(void)
Exit the thread context.
virtual ~Thread()
Destroy thread object, thread-specific data, and execution context.
void wait(void)
Wait until the semphore usage count is less than the thread limit.
virtual size_t onPeek(void *buf)=0
Invoke derived class buffer peeking method.
void unlock(void)
Unlock the conditional's supporting mutex.
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
void lock(void)
Acquire or increase locking.
size_t getUsed(void)
Return the current capacity in use for the buffer.
ReadLock(ThreadLock &_tl)
Wait for read access.
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 release(void)
Release or decrease locking.
void signal(void)
Signal pending event.
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.
Common C++ generic string class.
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.
A copy-on-write string class that operates by reference count.
void start(int priority=0)
Start execution of child context.
virtual size_t onWait(void *buf)=0
Invoke derived class object request from buffer.
~SemaphoreLock()
Post the semaphore automatically.
Semaphore(unsigned count=0)
Construct a semaphore with an initial count of threads to permit.
void join(void)
Join thread with parent.
virtual void run(void)=0
Abstract interface for thread context run method.
static void yield(void)
Yield execution context of the current thread.
~WriteLock()
Post the semaphore automatically.
size_t peek(Buffer &b, void *o)
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
void wait(void)
Wait (block) until signalled.
~MutexLock()
Release the mutex automatically.
static const size_t timeout
value to return when a timed operation returned with a timeout.
unsigned long timeout_t
Typedef for millisecond timer values.
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.
ThreadLock()
Create an instance of a rwlock.
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 set(void)
Set (update) the timer with current time.
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
~ReadLock()
Post the semaphore automatically.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
A portable counting semaphore class.
void access(void)
Access mode shared thread scheduling.
WriteLock(ThreadLock &_tl)
Wait for write access.
void wait(void)
A simple wait until triggered.
void map(void)
Map thread for get method.
virtual ~Buffer()
In derived functions, may be used to free the actual memory used to hold buffered data...
Event notification to manage scheduled realtime threads.
size_t put(Buffer &b, void *o, timeout_t t=0)
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.