Browse Source

Add a completion timeout helper function

master
Con Kolivas 10 years ago
parent
commit
098930afd5
  1. 37
      src/libckpool.c
  2. 1
      src/libckpool.h

37
src/libckpool.c

@ -78,6 +78,43 @@ void join_pthread(pthread_t thread)
pthread_join(thread, NULL); 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 */ /* Place holders for when we add lock debugging */
#define GETLOCK(_lock, _file, _func, _line) #define GETLOCK(_lock, _file, _func, _line)
#define GOTLOCK(_lock, _file, _func, _line) #define GOTLOCK(_lock, _file, _func, _line)

1
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 rename_proc(const char *name);
void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg); void create_pthread(pthread_t *thread, void *(*start_routine)(void *), void *arg);
void join_pthread(pthread_t thread); 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_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); void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line);

Loading…
Cancel
Save