From 4d3591d707f3a90d47c03e0b7da6dde284439fbd Mon Sep 17 00:00:00 2001 From: zzy-linux <2450266535@qq.com> Date: Wed, 25 Oct 2023 22:21:54 +0800 Subject: [PATCH] =?UTF-8?q?dev=20=E6=81=A2=E5=A4=8Dthread=5Fcond=5Ftimedwa?= =?UTF-8?q?it=E7=9A=84=E5=8A=9F=E8=83=BD,=E5=8F=8A=E8=8B=A5=E5=B9=B2Linux?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ssocket.h | 1 + include/tthread.h | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/ssocket.h b/include/ssocket.h index 6cafe03..ab5d0ce 100644 --- a/include/ssocket.h +++ b/include/ssocket.h @@ -26,6 +26,7 @@ #define socklen_t int #elif LINUX_PART #include +#include #include #include #include diff --git a/include/tthread.h b/include/tthread.h index a5b5fb1..bd91755 100644 --- a/include/tthread.h +++ b/include/tthread.h @@ -15,6 +15,8 @@ typedef DWORD TID; typedef CRITICAL_SECTION MUTEX; typedef CONDITION_VARIABLE COND; #elif _OS_LINUX +#include +#include #include 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, ...) {