dev 恢复thread_cond_timedwait的功能,及若干Linux问题

This commit is contained in:
zzy-linux 2023-10-25 22:21:54 +08:00
parent 25d0dac339
commit 4d3591d707
2 changed files with 32 additions and 11 deletions

View File

@ -26,6 +26,7 @@
#define socklen_t int #define socklen_t int
#elif LINUX_PART #elif LINUX_PART
#include <unistd.h> #include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <pthread.h> #include <pthread.h>

View File

@ -15,6 +15,8 @@ typedef DWORD TID;
typedef CRITICAL_SECTION MUTEX; typedef CRITICAL_SECTION MUTEX;
typedef CONDITION_VARIABLE COND; typedef CONDITION_VARIABLE COND;
#elif _OS_LINUX #elif _OS_LINUX
#include <unistd.h>
#include <errno.h>
#include <pthread.h> #include <pthread.h>
typedef pthread_t TID; typedef pthread_t TID;
typedef pthread_mutex_t MUTEX; typedef pthread_mutex_t MUTEX;
@ -23,6 +25,17 @@ typedef pthread_cond_t COND;
#error "Not Supported Operator System" #error "Not Supported Operator System"
#endif #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 #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -46,6 +59,7 @@ enum {
ERR_THREAD_COND_BROADCAST, ERR_THREAD_COND_BROADCAST,
ERR_THREAD_COND_WAIT, ERR_THREAD_COND_WAIT,
ERR_THREAD_COND_TIMEDWAIT, ERR_THREAD_COND_TIMEDWAIT,
ERR_THREAD_COND_TIME_OUT,
ERR_THREAD_ENUM_END 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_singal(COND* cond);
static inline int thread_cond_broadcast(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_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 tprintf(MUTEX* mutex, const char* format, ...);
static inline int tfprintf(MUTEX* mutex, FILE* const stream, 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; return ERR_THREAD_SUCCESS;
} }
//static inline int thread_cond_timedwait(COND cond, MUTEX mutex, ) { static inline int thread_cond_timedwait(COND* cond, MUTEX* mutex, int ms) {
//#if _OS_WIN #if _OS_WIN
// if (SignalObjectAndWait(cond, mutex, INFINITE, 0) == WAIT_FAILED) { return ERR_THREAD_COND_TIMEDWAIT; } int res = SignalObjectAndWait(cond, mutex, ms, 0);
// WAIT_TIMEOUT if (res == WAIT_TIMEOUT) { return -ERR_THREAD_COND_TIME_OUT; }
//#elif _OS_LINUX if (res == WAIT_FAILED) { return -ERR_THREAD_COND_TIMEDWAIT; }
// if (pthread_cond_timedwait(cond, mutex, ) != 0) { return ERR_THREAD_COND_TIMEDWAIT; } #elif _OS_LINUX
// ETIMEDOUT struct timespec outtime;
//#endif clock_gettime(CLOCK_MONOTONIC, &outtime);
// return ERR_THREAD_SUCCESS; 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 // thread safety stdio
static inline int tprintf(MUTEX* mutex, const char* format, ...) { static inline int tprintf(MUTEX* mutex, const char* format, ...) {