2018-02-20 21:21:38 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2018 - pancake */
|
2009-07-21 11:13:24 +00:00
|
|
|
|
|
|
|
#include <r_th.h>
|
|
|
|
|
2017-04-09 00:17:54 +00:00
|
|
|
#if __WINDOWS__ && !defined(__CYGWIN__)
|
|
|
|
static DWORD WINAPI _r_th_launcher(void *_th) {
|
|
|
|
#else
|
2010-03-25 09:18:59 +00:00
|
|
|
static void *_r_th_launcher(void *_th) {
|
2017-04-09 00:17:54 +00:00
|
|
|
#endif
|
2009-07-21 11:13:24 +00:00
|
|
|
int ret;
|
2016-11-04 22:07:02 +00:00
|
|
|
RThread *th = _th;
|
2016-07-12 20:15:19 +00:00
|
|
|
th->ready = true;
|
2010-03-25 09:18:59 +00:00
|
|
|
#if __UNIX__
|
2016-11-04 22:07:02 +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
|
2016-11-04 22:07:02 +00:00
|
|
|
if (th->delay < 0) {
|
|
|
|
r_th_lock_wait (th->lock);
|
|
|
|
}
|
2010-03-25 09:18:59 +00:00
|
|
|
#endif
|
2018-07-11 18:35:45 +00:00
|
|
|
r_th_lock_enter (th->lock);
|
2009-07-21 11:13:24 +00:00
|
|
|
do {
|
2016-11-04 22:07:02 +00:00
|
|
|
r_th_lock_leave (th->lock);
|
2016-07-12 20:15:19 +00:00
|
|
|
th->running = true;
|
2016-11-04 22:07:02 +00:00
|
|
|
ret = th->fun (th);
|
2018-08-23 11:49:28 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
// th has been freed
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
th->running = false;
|
2016-11-04 22:07:02 +00:00
|
|
|
r_th_lock_enter (th->lock);
|
|
|
|
} while (ret);
|
2009-07-21 11:13:24 +00:00
|
|
|
#if HAVE_PTHREAD
|
2016-11-04 22:07:02 +00:00
|
|
|
pthread_exit (&ret);
|
2009-07-21 11:13:24 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-25 09:18:59 +00:00
|
|
|
R_API int r_th_push_task(struct r_th_t *th, void *user) {
|
2016-07-12 20:15:19 +00:00
|
|
|
int ret = true;
|
2009-07-21 11:13:24 +00:00
|
|
|
th->user = user;
|
2016-11-04 22:07:02 +00:00
|
|
|
r_th_lock_leave (th->lock);
|
2009-07-21 11:13:24 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-21 00:22:17 +00:00
|
|
|
R_API R_TH_TID r_th_self(void) {
|
2018-08-05 09:57:00 +00:00
|
|
|
#if HAVE_PTHREAD
|
2018-04-19 22:25:33 +00:00
|
|
|
return pthread_self ();
|
|
|
|
#elif __WINDOWS__
|
|
|
|
return (HANDLE)GetCurrentThreadId ();
|
|
|
|
#else
|
|
|
|
#pragma message("Not implemented on windows")
|
|
|
|
return (R_TH_TID)-1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-11 10:09:12 +00:00
|
|
|
R_API bool r_th_setname(RThread *th, const char *name) {
|
|
|
|
#if __linux__
|
|
|
|
if (pthread_setname_np (th->tid, name) != 0) {
|
|
|
|
eprintf ("Failed to set thread name\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#elif __FreeBSD__ || __OpenBSD__ || __DragonFly__
|
|
|
|
pthread_set_name_np (th->tid, name);
|
|
|
|
return true;
|
|
|
|
#elif __NetBSD__
|
|
|
|
if (pthread_setname_np (th->tid, "%s", (void *)name) != 0) {
|
|
|
|
eprintf ("Failed to set thread name\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
#pragma message("warning r_th_setname not implemented")
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:07:02 +00:00
|
|
|
R_API RThread *r_th_new(R_TH_FUNCTION(fun), void *user, int delay) {
|
2014-08-28 10:08:46 +00:00
|
|
|
RThread *th = R_NEW0 (RThread);
|
2010-05-20 15:40:58 +00:00
|
|
|
if (th) {
|
2017-04-07 22:52:49 +00:00
|
|
|
th->lock = r_th_lock_new (false);
|
2016-07-12 20:15:19 +00:00
|
|
|
th->running = false;
|
2010-05-20 15:40:58 +00:00
|
|
|
th->fun = fun;
|
|
|
|
th->user = user;
|
|
|
|
th->delay = delay;
|
2016-07-12 20:15:19 +00:00
|
|
|
th->breaked = false;
|
|
|
|
th->ready = false;
|
2010-05-20 15:40:58 +00:00
|
|
|
#if HAVE_PTHREAD
|
2018-04-19 22:25:33 +00:00
|
|
|
pthread_cond_init (&th->_cond, NULL);
|
|
|
|
pthread_mutex_init (&th->_mutex, NULL);
|
2016-11-04 22:07:02 +00:00
|
|
|
pthread_create (&th->tid, NULL, _r_th_launcher, th);
|
2017-02-26 15:51:03 +00:00
|
|
|
#elif __WINDOWS__ && !defined(__CYGWIN__)
|
2017-04-09 00:17:54 +00:00
|
|
|
th->tid = CreateThread (NULL, 0, _r_th_launcher, th, 0, 0);
|
2010-05-20 15:40:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
2009-07-21 11:13:24 +00:00
|
|
|
return th;
|
|
|
|
}
|
|
|
|
|
2016-11-04 22:07:02 +00:00
|
|
|
R_API void r_th_break(RThread *th) {
|
2016-07-12 20:15:19 +00:00
|
|
|
th->breaked = true;
|
2009-07-21 11:13:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:21:38 +00:00
|
|
|
R_API bool r_th_kill(RThread *th, bool force) {
|
2018-08-22 03:07:21 +00:00
|
|
|
if (!th || !th->tid) {
|
2017-12-18 12:27:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
th->breaked = true;
|
2017-12-18 12:27:26 +00:00
|
|
|
r_th_break (th);
|
|
|
|
r_th_wait (th);
|
2009-07-21 11:13:24 +00:00
|
|
|
#if HAVE_PTHREAD
|
2011-09-25 04:57:13 +00:00
|
|
|
#ifdef __ANDROID__
|
2011-09-26 00:10:25 +00:00
|
|
|
pthread_kill (th->tid, 9);
|
2011-09-25 04:57:13 +00:00
|
|
|
#else
|
2010-03-25 09:18:59 +00:00
|
|
|
pthread_cancel (th->tid);
|
2011-09-25 04:57:13 +00:00
|
|
|
#endif
|
2017-04-09 00:17:54 +00:00
|
|
|
#elif __WINDOWS__ && !defined(__CYGWIN__)
|
|
|
|
TerminateThread (th->tid, -1);
|
2009-07-21 11:13:24 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-25 02:29:18 +00:00
|
|
|
// running in parent
|
2018-04-19 22:25:33 +00:00
|
|
|
R_API bool r_th_pause(RThread *th, bool enable) {
|
2018-04-25 02:29:18 +00:00
|
|
|
if (!th) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-19 22:25:33 +00:00
|
|
|
if (enable) {
|
|
|
|
#if HAVE_PTHREAD
|
2018-04-25 02:29:18 +00:00
|
|
|
pthread_mutex_trylock (&th->_mutex);
|
2018-04-19 22:25:33 +00:00
|
|
|
#else
|
|
|
|
#pragma message("warning r_th_pause not implemented")
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#if HAVE_PTHREAD
|
2018-04-25 02:29:18 +00:00
|
|
|
// pthread_cond_signal (&th->_cond);
|
2018-04-19 22:25:33 +00:00
|
|
|
pthread_mutex_unlock (&th->_mutex);
|
|
|
|
#else
|
|
|
|
#pragma message("warning r_th_pause not implemented")
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 02:29:18 +00:00
|
|
|
// running in thread
|
|
|
|
R_API bool r_th_try_pause(RThread *th) {
|
2018-04-25 10:38:45 +00:00
|
|
|
if (!th) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if HAVE_PTHREAD
|
|
|
|
// pthread_mutex_lock (&th->_mutex);
|
|
|
|
// pthread_mutex_unlock (&th->_mutex);
|
|
|
|
if (pthread_mutex_trylock (&th->_mutex) != -1) {
|
|
|
|
pthread_mutex_unlock (&th->_mutex);
|
|
|
|
} else {
|
|
|
|
// oops
|
2018-04-25 02:29:18 +00:00
|
|
|
}
|
2018-04-25 10:38:45 +00:00
|
|
|
// pthread_cond_wait (&th->_cond, &th->_mutex);
|
|
|
|
#else
|
|
|
|
#pragma message("warning r_th_try_pause not implemented")
|
|
|
|
#endif
|
|
|
|
return true;
|
2018-04-25 02:29:18 +00:00
|
|
|
}
|
|
|
|
|
2016-11-04 22:07:02 +00:00
|
|
|
R_API bool r_th_start(RThread *th, int enable) {
|
|
|
|
bool ret = true;
|
2009-07-21 11:13:24 +00:00
|
|
|
if (enable) {
|
|
|
|
if (!th->running) {
|
|
|
|
// start thread
|
2016-11-04 22:07:02 +00:00
|
|
|
while (!th->ready) {
|
|
|
|
/* spinlock */
|
|
|
|
}
|
2010-05-20 15:40:58 +00:00
|
|
|
r_th_lock_leave (th->lock);
|
2009-07-21 11:13:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (th->running) {
|
|
|
|
// stop thread
|
2018-04-19 22:25:33 +00:00
|
|
|
//r_th_kill (th, 0);
|
2010-05-20 15:40:58 +00:00
|
|
|
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) {
|
2016-07-12 20:15:19 +00:00
|
|
|
int ret = false;
|
2015-07-14 11:55:58 +00:00
|
|
|
void *thret;
|
2010-06-23 15:30:16 +00:00
|
|
|
if (th) {
|
2017-04-09 00:17:54 +00:00
|
|
|
#if HAVE_PTHREAD
|
2010-06-23 15:30:16 +00:00
|
|
|
ret = pthread_join (th->tid, &thret);
|
2017-04-09 00:17:54 +00:00
|
|
|
#elif __WINDOWS__ && !defined(__CYGWIN__)
|
|
|
|
ret = WaitForSingleObject (th->tid, INFINITE);
|
|
|
|
#endif
|
2016-07-12 20:15:19 +00:00
|
|
|
th->running = false;
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
2009-07-21 11:13:24 +00:00
|
|
|
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) {
|
2018-02-20 21:24:29 +00:00
|
|
|
if (!th) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-09 00:17:54 +00:00
|
|
|
#if __WINDOWS__ && !defined(__CYGWIN__)
|
|
|
|
CloseHandle (th->tid);
|
|
|
|
#endif
|
2010-05-20 15:40:58 +00:00
|
|
|
r_th_lock_free (th->lock);
|
2010-03-25 09:18:59 +00:00
|
|
|
free (th);
|
2009-07-21 11:13:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-01 03:46:33 +00:00
|
|
|
|
2018-08-23 11:49:28 +00:00
|
|
|
R_API void *r_th_kill_free(struct r_th_t *th) {
|
|
|
|
if (!th) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
r_th_kill (th, true);
|
|
|
|
r_th_free (th);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-11-01 03:46:33 +00:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
// Thread Pipes
|
|
|
|
typedef struct r_th_pipe_t {
|
|
|
|
RList *msglist;
|
|
|
|
RThread *th;
|
|
|
|
//RThreadLock *lock;
|
|
|
|
} RThreadPipe;
|
|
|
|
|
|
|
|
r_th_pipe_new();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|