From b6588c5f13421efee17c998bca70a05ebd05127a Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Fri, 5 Feb 2016 08:58:02 +1100 Subject: [PATCH] Implement a cksem_trywait function --- src/libckpool.c | 9 +++++++++ src/libckpool.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/libckpool.c b/src/libckpool.c index 59b132c9..25593c08 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -399,6 +399,15 @@ void _cksem_wait(sem_t *sem, const char *file, const char *func, const int line) } } +int _cksem_trywait(sem_t *sem, const char *file, const char *func, const int line) +{ + int ret = sem_trywait(sem); + + if (unlikely(ret && errno != EAGAIN && errno != EINTR)) + quitfrom(1, file, func, line, "Failed to sem_trywait errno=%d sem=0x%p", errno, sem); + return ret; +} + int _cksem_mswait(sem_t *sem, int ms, const char *file, const char *func, const int line) { ts_t abs_timeout, ts_now; diff --git a/src/libckpool.h b/src/libckpool.h index 141b2839..9d5875c4 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -452,12 +452,14 @@ void cklock_destroy(cklock_t *lock); void _cksem_init(sem_t *sem, const char *file, const char *func, const int line); void _cksem_post(sem_t *sem, const char *file, const char *func, const int line); void _cksem_wait(sem_t *sem, const char *file, const char *func, const int line); +int _cksem_trywait(sem_t *sem, const char *file, const char *func, const int line); int _cksem_mswait(sem_t *sem, int ms, const char *file, const char *func, const int line); void _cksem_destroy(sem_t *sem, const char *file, const char *func, const int line); #define cksem_init(SEM) _cksem_init(SEM, __FILE__, __func__, __LINE__) #define cksem_post(SEM) _cksem_post(SEM, __FILE__, __func__, __LINE__) #define cksem_wait(SEM) _cksem_wait(SEM, __FILE__, __func__, __LINE__) +#define cksem_trywait(SEM) _cksem_trywait(SEM, __FILE__, __func__, __LINE__) #define cksem_mswait(SEM, _timeout) _cksem_mswait(SEM, _timeout, __FILE__, __func__, __LINE__) #define cksem_destroy(SEM) _cksem_destroy(SEM, __FILE__, __func__, __LINE__)