Files
xahaud/db/task.h
Vinnie Falco c58450a44b Squashed 'src/sophia/' content from commit 9d29b94
git-subtree-dir: src/sophia
git-subtree-split: 9d29b94cd66d3bd67082642b9a1f6dea470f0730
2013-09-17 10:09:27 -07:00

71 lines
1.2 KiB
C

#ifndef SP_TASK_H_
#define SP_TASK_H_
/*
* sophia database
* sphia.org
*
* Copyright (c) Dmitry Simonenko
* BSD License
*/
typedef struct sptask sptask;
struct sptask {
volatile int run;
void *arg;
pthread_t id;
pthread_mutex_t l;
pthread_cond_t c;
};
static inline int
sp_taskstart(sptask *t, void*(*f)(void*), void *arg) {
t->run = 1;
t->arg = arg;
pthread_mutex_init(&t->l, NULL);
pthread_cond_init(&t->c, NULL);
return pthread_create(&t->id, NULL, f, t);
}
static inline int
sp_taskstop(sptask *t) {
pthread_mutex_lock(&t->l);
if (t->run == 0) {
pthread_mutex_unlock(&t->l);
return 0;
}
t->run = 0;
pthread_cond_signal(&t->c);
pthread_mutex_unlock(&t->l);
return pthread_join(t->id, NULL);
}
static inline void
sp_taskwakeup(sptask *t) {
pthread_mutex_lock(&t->l);
pthread_cond_signal(&t->c);
pthread_mutex_unlock(&t->l);
}
static inline int
sp_taskwait(sptask *t) {
pthread_mutex_lock(&t->l);
if (t->run == 0) {
pthread_mutex_unlock(&t->l);
return 0;
}
pthread_cond_wait(&t->c, &t->l);
pthread_mutex_unlock(&t->l);
return t->run;
}
static inline void
sp_taskdone(sptask *t) {
pthread_mutex_lock(&t->l);
t->run = 0;
pthread_mutex_unlock(&t->l);
}
#endif