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);}