diff --git a/src/libckpool.c b/src/libckpool.c index 7c50b4ff..5d6cbb98 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -78,6 +78,43 @@ void join_pthread(pthread_t thread) pthread_join(thread, NULL); } +struct ck_completion { + sem_t sem; + void (*fn)(void *fnarg); + void *fnarg; +}; + +static void *completion_thread(void *arg) +{ + struct ck_completion *ckc = (struct ck_completion *)arg; + + ckc->fn(ckc->fnarg); + cksem_post(&ckc->sem); + + return NULL; +} + +bool ck_completion_timeout(void *fn, void *fnarg, int timeout) +{ + struct ck_completion ckc; + pthread_t pthread; + bool ret = false; + + cksem_init(&ckc.sem); + ckc.fn = fn; + ckc.fnarg = fnarg; + + pthread_create(&pthread, NULL, completion_thread, (void *)&ckc); + + ret = cksem_mswait(&ckc.sem, timeout); + if (!ret) + pthread_join(pthread, NULL); + else + pthread_cancel(pthread); + return !ret; +} + + /* Place holders for when we add lock debugging */ #define GETLOCK(_lock, _file, _func, _line) #define GOTLOCK(_lock, _file, _func, _line) diff --git a/src/libckpool.h b/src/libckpool.h index b98c8c2c..3401e11a 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -362,6 +362,7 @@ static inline void _json_set_bool(json_t *val, const char *key, bool boolean, void rename_proc(const char *name); void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg); void join_pthread(pthread_t thread); +bool ck_completion_timeout(void *fn, void *fnarg, int timeout); void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line); void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line);