2010-03-25 09:18:59 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2010 pancake<@nopcode.org> */
|
2009-07-21 11:13:24 +00:00
|
|
|
|
|
|
|
#include <r_th.h>
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
static void *_r_th_launcher(void *_th) {
|
2009-07-21 11:13:24 +00:00
|
|
|
int ret;
|
|
|
|
struct r_th_t *th = _th;
|
|
|
|
|
|
|
|
th->ready = R_TRUE;
|
2010-03-25 09:18:59 +00:00
|
|
|
#if __UNIX__
|
2009-07-21 11:13:24 +00:00
|
|
|
if (th->delay>0) sleep(th->delay);
|
|
|
|
else if (th->delay<0) r_th_lock_wait(&th->lock);
|
2010-03-25 09:18:59 +00:00
|
|
|
#else
|
|
|
|
if (th->delay<0) r_th_lock_wait(&th->lock);
|
|
|
|
#endif
|
2009-07-21 11:13:24 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
r_th_lock_leave(&th->lock);
|
|
|
|
th->running = R_TRUE;
|
|
|
|
ret = th->fun(th);
|
|
|
|
th->running = R_FALSE;
|
|
|
|
r_th_lock_enter(&th->lock);
|
|
|
|
} while(ret);
|
|
|
|
|
|
|
|
#if HAVE_PTHREAD
|
|
|
|
pthread_exit(&ret);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_init(struct r_th_t *th, R_TH_FUNCTION(fun), void *user, int delay) {
|
2009-07-21 11:13:24 +00:00
|
|
|
int ret = R_TRUE;
|
|
|
|
|
|
|
|
r_th_lock_init(&th->lock);
|
|
|
|
th->running = R_FALSE;
|
|
|
|
th->fun = fun;
|
|
|
|
th->user = user;
|
|
|
|
th->delay = delay;
|
|
|
|
th->breaked = R_FALSE;
|
|
|
|
th->ready = R_FALSE;
|
|
|
|
#if HAVE_PTHREAD
|
|
|
|
if (pthread_create(&th->tid, NULL, _r_th_launcher, th))
|
|
|
|
ret = R_FALSE;
|
|
|
|
#elif __WIN32__
|
|
|
|
th->tid = CreateThread(NULL, 0, _r_th_launcher, th, 0, &th->tid);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_push_task(struct r_th_t *th, void *user) {
|
2009-07-21 11:13:24 +00:00
|
|
|
int ret = R_TRUE;
|
|
|
|
th->user = user;
|
|
|
|
r_th_lock_leave(&th->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API struct r_th_t *r_th_new(R_TH_FUNCTION(fun), void *user, int delay) {
|
|
|
|
struct r_th_t *th = R_NEW (struct r_th_t);
|
|
|
|
r_th_init (th, fun, user, delay);
|
2009-07-21 11:13:24 +00:00
|
|
|
return th;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API void r_th_break(struct r_th_t *th) {
|
2009-07-21 11:13:24 +00:00
|
|
|
th->breaked = R_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_kill(struct r_th_t *th, int force) {
|
2009-07-21 11:13:24 +00:00
|
|
|
th->breaked = R_TRUE;
|
|
|
|
r_th_break(th);
|
|
|
|
r_th_wait(th);
|
|
|
|
#if HAVE_PTHREAD
|
2010-03-25 09:18:59 +00:00
|
|
|
pthread_cancel (th->tid);
|
2009-07-21 11:13:24 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_start(struct r_th_t *th, int enable) {
|
2009-07-21 11:13:24 +00:00
|
|
|
int ret = R_TRUE;
|
|
|
|
if (enable) {
|
|
|
|
if (!th->running) {
|
|
|
|
// start thread
|
2010-03-25 09:18:59 +00:00
|
|
|
while (!th->ready);
|
|
|
|
r_th_lock_leave (&th->lock);
|
2009-07-21 11:13:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (th->running) {
|
|
|
|
// stop thread
|
2010-03-25 09:18:59 +00:00
|
|
|
r_th_kill (th, 0);
|
|
|
|
r_th_lock_enter (&th->lock); // deadlock?
|
2009-07-21 11:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
th->running = enable;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_wait(struct r_th_t *th) {
|
|
|
|
int ret = R_FALSE;
|
2009-07-21 11:13:24 +00:00
|
|
|
#if HAVE_PTHREAD
|
|
|
|
void *thret;
|
2010-03-25 09:18:59 +00:00
|
|
|
ret = pthread_join (th->tid, &thret);
|
2009-07-21 11:13:24 +00:00
|
|
|
th->running = R_FALSE;
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_wait_async(struct r_th_t *th) {
|
2009-07-21 11:13:24 +00:00
|
|
|
return th->running;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API void *r_th_free(struct r_th_t *th) {
|
|
|
|
r_th_kill (th, R_TRUE);
|
|
|
|
free (th);
|
2009-07-21 11:13:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|