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();
64 inline Mutex() : RecursiveMutex() {}
66 inline void enterMutex(
void)
69 inline void leaveMutex(
void)
72 inline bool tryEnterMutex(
void)
75 inline void enter(
void)
78 inline void leave(
void)
81 inline bool test(
void)
153 { mutex.enterMutex(); }
160 { mutex.leaveMutex(); }
166 inline ThreadLock() :
ucommon::ThreadLock() {}
168 inline void readLock(
void)
171 inline void writeLock(
void)
174 inline void tryReadLock(
void)
177 inline void tryWriteLock(
void)
269 inline Conditional() :
ucommon::Conditional() {}
273 void signal(
bool broadcast);
275 inline void enterMutex(
void)
278 inline void leaveMutex(
void)
285 inline Semaphore(
unsigned size = 0) :
ucommon::Semaphore(size) {}
290 inline void wait(
void)
293 inline void post(
void)
338 inline Event() :
ucommon::TimedEvent() {}
340 inline void wait(
void)
346 inline void signal(
void)
349 inline void reset(
void)
372 bool detached, terminated;
378 Thread(
int pri = 0,
size_t stack = 0);
382 inline void map(
void)
383 {JoinableThread::map();}
385 virtual void initial(
void);
386 virtual void notify(Thread *thread);
387 virtual void final(void);
388 virtual void run(
void) = 0;
390 void terminate(
void);
397 inline void join(
void)
398 {JoinableThread::join();}
400 inline void sync(
void)
403 static inline Thread *
get(void)
404 {
return (Thread *)JoinableThread::get();}
406 inline static void yield(
void)
409 inline static void sleep(
timeout_t msec = TIMEOUT_INF)
412 bool isRunning(
void);
421 static Throw getException(
void);
428 static void setException(Throw mode);
433 inline pthread_t getId(
void)
469 static time_t getTime(time_t *tloc = NULL);
470 static time_t time(time_t *tloc)
471 {
return getTime(tloc);}
473 static int getTimeOfDay(
struct timeval *tp);
474 static int gettimeofday(
struct timeval *tp,
struct timezone *)
475 {
return getTimeOfDay(tp);}
477 static struct tm *getLocalTime(
const time_t *clock,
struct tm *result);
478 static struct tm *locatime(
const time_t *clock,
struct tm *result)
479 {
return getLocalTime(clock, result);}
481 static struct tm *getGMTTime(
const time_t *clock,
struct tm *result);
482 static struct tm *gmtime(
const time_t *clock,
struct tm *result)
483 {
return getGMTTime(clock, result);}
499 struct timeval timer;
550 void sleepTimer(
void);
585 struct timespec *getTimeout(
struct timespec *spec,
timeout_t timeout);
588 #if !defined(_MSWINDOWS_) || defined(_MSTHREADS_)
589 inline struct tm *localtime_r(
const time_t *t,
struct tm *b)
590 {
return SysTime::getLocalTime(t, b);}
592 inline char *ctime_r(
const time_t *t,
char *buf)
595 inline struct tm *gmtime_r(
const time_t *t,
struct tm *b)
596 {
return SysTime::getGMTTime(t, b);}
598 inline char *asctime_r(
const struct tm *tm,
char *b)
599 {
return asctime(tm);}
602 inline Thread *getThread(
void)
603 {
return Thread::get();}
627 class __EXPORT Buffer :
public Mutex
629 class __EXPORT
Buffer :
public Conditional
634 HANDLE sem_head, sem_tail;
645 virtual size_t onPeek(
void *buf) = 0;
652 virtual size_t onWait(
void *buf) = 0;
659 virtual size_t onPost(
void *buf) = 0;
714 size_t post(
void *buf,
timeout_t timeout = 0);
722 size_t peek(
void *buf);
728 virtual bool isValid(
void);
741 char *buf, *head, *tail;
750 size_t onPeek(
void *buf);
757 size_t onWait(
void *buf);
764 size_t onPost(
void *buf);
809 class __EXPORT
ThreadQueue :
public Mutex,
public Thread,
public Semaphore
815 typedef struct _data {
824 data_t *first, *last;
831 virtual void final();
837 virtual void startQueue(
void);
844 virtual void stopQueue(
void);
849 virtual void onTimer(
void);
859 virtual void runQueue(
void *data) = 0;
869 ThreadQueue(
const char *
id,
int pri,
size_t stack = 0);
893 void post(
const void *data,
unsigned len);
899 {
return b.wait(o, t);}
903 {
return b.
post(o, t);}
The conditional is a common base for other thread synchronizing classes.
void wait(void)
Wait until the semphore usage count is less than the thread limit.
static void sleep(timeout_t timeout)
Sleep current thread for a specified time period.
void set(void)
Set (update) the timer with current time.
Somewhat generic queue processing class to establish a producer consumer queue.
void start(DetachedThread *thread, int priority=0)
Convenience function to start a detached thread.
The Mutex Counter is a counter variable which can safely be incremented or decremented by multiple th...
A copy-on-write string class that operates by reference count.
Common namespace for all ucommon objects.
void signal(void)
Signal pending event.
void release(rexlock_t &lock)
Convenience function to release a shared recursive mutex lock.
The WriteLock class is used to protect a section of code through a ThreadLock for "write" access to t...
static void yield(void)
Yield execution context of the current thread.
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...
size_t post(void *buf, timeout_t timeout=0)
Post an object into the buffer and enable a waiting thread to receive it.
void unlock(ExclusiveAccess &object)
Convenience function to unlock an exclusive object through it's protocol.
void release(void)
Release the semaphore after waiting for 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 portable counting semaphore class.
A buffer class that holds a known capacity of fixed sized objects defined during creation.
~SemaphoreLock()
Post the semaphore automatically.
void wait(semaphore_t &semaphore, timeout_t timeout=Timer::inf)
Convenience function to wait on a semaphore.
Event notification to manage scheduled realtime threads.
~WriteLock()
Post the semaphore automatically.
size_t peek(Buffer &b, void *o)
Common C++ generic string class.
Portable recursive exclusive lock.
The ReadLock class is used to protect a section of code through a ThreadLock for "read" access to the...
void wait(void)
A simple wait until triggered.
~MutexLock()
Release the mutex automatically.
void release(void)
Release the lock.
static const size_t timeout
value to return when a timed operation returned with a timeout.
A generic and portable implimentation of Read/Write locking.
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 lock(rexlock_t &lock)
Convenience function to lock a shared recursive mutex lock.
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...
The buffer class represents an IPC service that is built upon a buffer of fixed capacity that can be ...
~ReadLock()
Post the semaphore automatically.
void reset(void)
Reset triggered conditional.
unsigned long timeout_t
Typedef for millisecond timer values.
void access(void)
Access mode shared thread scheduling.
WriteLock(ThreadLock &_tl)
Wait for write access.
size_t put(Buffer &b, void *o, timeout_t t=0)
SemaphoreLock(Semaphore &_sem)
Wait for the semaphore.