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(); 62 class __EXPORT Mutex :
protected ucommon::RecursiveMutex
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);
104 class __EXPORT MutexCounter :
public Mutex
107 __DELETE_COPY(MutexCounter);
110 volatile int counter;
124 MutexCounter(
int initial);
153 class __EXPORT MutexLock
158 __DELETE_COPY(MutexLock);
166 inline MutexLock( Mutex& _mutex ) : mutex( _mutex ) {
174 inline ~MutexLock() {
179 class __EXPORT ThreadLock :
protected ucommon::RWLock
182 __DELETE_COPY(ThreadLock);
185 inline ThreadLock() :
ucommon::RWLock() {}
187 inline void readLock(
void) {
188 ucommon::RWLock::access();
191 inline void writeLock(
void) {
192 ucommon::RWLock::modify();
195 inline void tryReadLock(
void) {
196 ucommon::RWLock::access(0);
199 inline void tryWriteLock(
void) {
200 ucommon::RWLock::modify(0);
203 inline void unlock(
void) {
204 ucommon::RWLock::release();
228 class __EXPORT ReadLock
233 __DELETE_COPY(ReadLock);
241 inline ReadLock( ThreadLock& _tl ) : tl( _tl ) {
273 class __EXPORT WriteLock
278 __DELETE_COPY(WriteLock);
286 inline WriteLock( ThreadLock& _tl ) : tl( _tl ) {
293 inline ~WriteLock() {
298 class __EXPORT Conditional :
private ucommon::Conditional
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) {
311 ucommon::Conditional::lock();
314 inline void leaveMutex(
void) {
315 ucommon::Conditional::unlock();
319 class __EXPORT Semaphore :
protected ucommon::Semaphore
322 inline Semaphore(
unsigned size = 0) :
ucommon::Semaphore(size) {}
324 inline bool wait(timeout_t timeout) {
325 return ucommon::Semaphore::wait(timeout);
328 inline void wait(
void) {
329 ucommon::Semaphore::wait();
332 inline void post(
void) {
333 ucommon::Semaphore::release();
356 class __EXPORT SemaphoreLock
365 inline SemaphoreLock( Semaphore& _sem ) : sem( _sem ) {
372 inline ~SemaphoreLock() {
377 class __EXPORT Event :
private ucommon::TimedEvent
380 __DELETE_COPY(Event);
383 inline Event() :
ucommon::TimedEvent() {}
385 inline void wait(
void) {
386 ucommon::TimedEvent::wait();
389 inline bool wait(timeout_t timeout) {
390 return ucommon::TimedEvent::wait(timeout);
393 inline void signal(
void) {
394 ucommon::TimedEvent::signal();
397 inline void reset(
void) {
398 ucommon::TimedEvent::reset();
401 inline void set(timeout_t timeout = 0) {
402 ucommon::TimedEvent::set(timeout);
406 class __EXPORT Thread :
protected ucommon::JoinableThread
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) {
463 ucommon::Thread::yield();
466 inline static void sleep(timeout_t msec = TIMEOUT_INF) {
467 ucommon::Thread::sleep(msec);
470 bool isRunning(
void);
479 static Throw getException(
void);
486 static void setException(Throw mode);
491 inline pthread_t getId(
void)
const {
525 class __EXPORT SysTime
528 __DELETE_DEFAULTS(SysTime);
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);
562 class __EXPORT TimerPort
566 struct timeval timer;
572 __DELETE_COPY(TimerPort);
591 void setTimer(timeout_t timeout = 0);
602 void incTimer(timeout_t timeout);
613 void decTimer(timeout_t timeout);
619 void sleepTimer(
void);
639 timeout_t getTimer(
void)
const;
650 timeout_t getElapsed(
void)
const;
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;
719 virtual size_t onPeek(
void *buf) = 0;
726 virtual size_t onWait(
void *buf) = 0;
733 virtual size_t onPost(
void *buf) = 0;
740 static const size_t timeout;
746 Buffer(
size_t capacity);
757 inline size_t getSize(
void)
const {
767 inline size_t getUsed(
void)
const {
780 size_t wait(
void *buf, timeout_t timeout = 0);
790 size_t post(
void *buf, timeout_t timeout = 0);
798 size_t peek(
void *buf);
804 virtual bool isValid(
void);
814 class __EXPORT FixedBuffer :
public Buffer
817 char *buf, *head, *tail;
826 size_t onPeek(
void *buf) __OVERRIDE;
833 size_t onWait(
void *buf) __OVERRIDE;
840 size_t onPost(
void *buf) __OVERRIDE;
850 FixedBuffer(
size_t capacity,
size_t objsize);
858 FixedBuffer(
const FixedBuffer &fb);
863 virtual ~FixedBuffer();
865 FixedBuffer &operator=(
const FixedBuffer &fb);
867 bool isValid(
void) __OVERRIDE;
885 class __EXPORT ThreadQueue : public Mutex, public Thread, public Semaphore
888 void run(
void) __FINAL;
890 __DELETE_COPY(ThreadQueue);
893 typedef struct _data {
902 data_t *first, *last;
909 virtual void final() __OVERRIDE;
915 virtual void startQueue(
void);
922 virtual void stopQueue(
void);
927 virtual void onTimer(
void);
937 virtual void runQueue(
void *data) = 0;
947 ThreadQueue(
const char *
id,
int pri,
size_t stack = 0);
952 virtual ~ThreadQueue();
961 void setTimer(timeout_t timeout);
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) {
Common C++ generic string class.
Common namespace for all ucommon objects.