diff --git a/src/pthread_func.h b/src/pthread_func.h index c89bccfea4f35a3c69f6e3192c8e74639a81c6cb..074086467d2d229bdddf95d710a75bb1034cf059 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);