From 2e85b7ad9da23d930974a665ab64a75555bae55b Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Thu, 29 Oct 2020 21:10:03 +0800 Subject: [PATCH] add functions: fc_timedwait_xxx --- src/pthread_func.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/pthread_func.h b/src/pthread_func.h index c89bccf..0740864 100644 --- a/src/pthread_func.h +++ b/src/pthread_func.h @@ -23,6 +23,7 @@ #include #include #include "common_define.h" +#include "sched_thread.h" #ifdef __cplusplus extern "C" { @@ -60,6 +61,36 @@ void destroy_pthread_lock_cond_pair(pthread_lock_cond_pair_t *lcp); } while (0) +#define lcp_timedwait_sec(lcp, timeout) \ + fc_timedwait_sec(&(lcp)->lock, &(lcp)->cond, timeout) + +#define lcp_timedwait_ms(lcp, timeout_ms) \ + fc_timedwait_ms(&(lcp)->lock, &(lcp)->cond, timeout_ms) + +static inline void fc_timedwait_sec(pthread_mutex_t *lock, + pthread_cond_t *cond, const int timeout) +{ + struct timespec ts; + + PTHREAD_MUTEX_LOCK(lock); + ts.tv_sec = get_current_time() + timeout; + ts.tv_nsec = 0; + pthread_cond_timedwait(cond, lock, &ts); + PTHREAD_MUTEX_UNLOCK(lock); +} + +static inline void fc_timedwait_ms(pthread_mutex_t *lock, + pthread_cond_t *cond, const int timeout_ms) +{ + struct timespec ts; + + PTHREAD_MUTEX_LOCK(lock); + ts.tv_sec = get_current_time() + timeout_ms / 1000; + ts.tv_nsec = (timeout_ms % 1000) * (1000 * 1000); + pthread_cond_timedwait(cond, lock, &ts); + PTHREAD_MUTEX_UNLOCK(lock); +} + int create_work_threads(int *count, void *(*start_func)(void *), void **args, pthread_t *tids, const int stack_size);