dev 恢复thread_cond_timedwait的功能,及若干Linux问题
This commit is contained in:
parent
25d0dac339
commit
4d3591d707
@ -26,6 +26,7 @@
|
||||
#define socklen_t int
|
||||
#elif LINUX_PART
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <pthread.h>
|
||||
|
@ -15,6 +15,8 @@ typedef DWORD TID;
|
||||
typedef CRITICAL_SECTION MUTEX;
|
||||
typedef CONDITION_VARIABLE COND;
|
||||
#elif _OS_LINUX
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
typedef pthread_t TID;
|
||||
typedef pthread_mutex_t MUTEX;
|
||||
@ -23,6 +25,17 @@ typedef pthread_cond_t COND;
|
||||
#error "Not Supported Operator System"
|
||||
#endif
|
||||
|
||||
#ifndef ZZY_SLEEP
|
||||
#define ZZY_SLEEP
|
||||
#if _OS_WIN
|
||||
#define sleeps(s) Sleep(s*1000)
|
||||
#define sleepms(ms) Sleep(ms)
|
||||
#elif _OS_LINUX
|
||||
#define sleeps(s) sleep(s)
|
||||
#define sleepms(ms) usleep(ms*1000)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -46,6 +59,7 @@ enum {
|
||||
ERR_THREAD_COND_BROADCAST,
|
||||
ERR_THREAD_COND_WAIT,
|
||||
ERR_THREAD_COND_TIMEDWAIT,
|
||||
ERR_THREAD_COND_TIME_OUT,
|
||||
|
||||
ERR_THREAD_ENUM_END
|
||||
};
|
||||
@ -65,7 +79,7 @@ static inline int thread_cond_destroy(COND* cond);
|
||||
static inline int thread_cond_singal(COND* cond);
|
||||
static inline int thread_cond_broadcast(COND* cond);
|
||||
static inline int thread_cond_wait(COND* cond, MUTEX* mutex);
|
||||
static inline int thread_cond_timedwait(COND* cond, MUTEX* mutex);
|
||||
static inline int thread_cond_timedwait(COND* cond, MUTEX* mutex, int ms);
|
||||
|
||||
static inline int tprintf(MUTEX* mutex, const char* format, ...);
|
||||
static inline int tfprintf(MUTEX* mutex, FILE* const stream, const char* format, ...);
|
||||
@ -202,16 +216,22 @@ static inline int thread_cond_wait(COND* cond, MUTEX* mutex) {
|
||||
return ERR_THREAD_SUCCESS;
|
||||
}
|
||||
|
||||
//static inline int thread_cond_timedwait(COND cond, MUTEX mutex, ) {
|
||||
//#if _OS_WIN
|
||||
// if (SignalObjectAndWait(cond, mutex, INFINITE, 0) == WAIT_FAILED) { return ERR_THREAD_COND_TIMEDWAIT; }
|
||||
// WAIT_TIMEOUT
|
||||
//#elif _OS_LINUX
|
||||
// if (pthread_cond_timedwait(cond, mutex, ) != 0) { return ERR_THREAD_COND_TIMEDWAIT; }
|
||||
// ETIMEDOUT
|
||||
//#endif
|
||||
// return ERR_THREAD_SUCCESS;
|
||||
//}
|
||||
static inline int thread_cond_timedwait(COND* cond, MUTEX* mutex, int ms) {
|
||||
#if _OS_WIN
|
||||
int res = SignalObjectAndWait(cond, mutex, ms, 0);
|
||||
if (res == WAIT_TIMEOUT) { return -ERR_THREAD_COND_TIME_OUT; }
|
||||
if (res == WAIT_FAILED) { return -ERR_THREAD_COND_TIMEDWAIT; }
|
||||
#elif _OS_LINUX
|
||||
struct timespec outtime;
|
||||
clock_gettime(CLOCK_MONOTONIC, &outtime);
|
||||
outtime.tv_sec += ms/1000;
|
||||
outtime.tv_nsec += (ms % 1000) * 1000 * 1000;
|
||||
int res = pthread_cond_timedwait(cond, mutex, &outtime);
|
||||
if (res == ETIMEDOUT) { return -ERR_THREAD_COND_TIME_OUT; }
|
||||
if (res != 0) { return -ERR_THREAD_COND_TIMEDWAIT; }
|
||||
#endif
|
||||
return ERR_THREAD_SUCCESS;
|
||||
}
|
||||
|
||||
// thread safety stdio
|
||||
static inline int tprintf(MUTEX* mutex, const char* format, ...) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user