diff --git a/src/libckpool.c b/src/libckpool.c index 5d507672..3c749880 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -101,9 +101,29 @@ void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, co int _mutex_trylock(pthread_mutex_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line) { + int ret; + TRYLOCK(lock, file, func, line); - int ret = pthread_mutex_trylock(lock); + ret = pthread_mutex_trylock(lock); DIDLOCK(ret, lock, file, func, line); + + return ret; +} + +int _mutex_timedlock(pthread_mutex_t *lock, int timeout, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line) +{ + tv_t now; + ts_t abs; + int ret; + + tv_time(&now); + tv_to_ts(&abs, &now); + abs.tv_sec += timeout; + + TRYLOCK(lock, file, func, line); + ret = pthread_mutex_timedlock(lock, &abs); + DIDLOCK(ret, lock, file, func, line); + return ret; } diff --git a/src/libckpool.h b/src/libckpool.h index 23c26c74..c56f4c24 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -138,6 +138,7 @@ static inline void flip_80(void *dest_p, const void *src_p) #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__) #define mutex_unlock(_lock) _mutex_unlock(_lock, __FILE__, __func__, __LINE__) #define mutex_trylock(_lock) _mutex_trylock(_lock, __FILE__, __func__, __LINE__) +#define mutex_timedlock(_lock, timeout) _mutex_timedlock(_lock, timeout, __FILE__, __func__, __LINE__) #define wr_lock(_lock) _wr_lock(_lock, __FILE__, __func__, __LINE__) #define wr_trylock(_lock) _wr_trylock(_lock, __FILE__, __func__, __LINE__) #define rd_lock(_lock) _rd_lock(_lock, __FILE__, __func__, __LINE__) @@ -355,6 +356,7 @@ void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, cons void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line); void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, const int line); int _mutex_trylock(pthread_mutex_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line); +int _mutex_timedlock(pthread_mutex_t *lock, int timeout, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line); void _wr_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line); int _wr_trylock(pthread_rwlock_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line); void _rd_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line);