File: thread.c

package info (click to toggle)
radare2 0.9.6-3.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 17,496 kB
  • ctags: 45,959
  • sloc: ansic: 240,999; sh: 3,645; makefile: 2,520; python: 1,212; asm: 312; ruby: 214; awk: 209; perl: 188; lisp: 169; java: 23; xml: 17; php: 6
file content (116 lines) | stat: -rw-r--r-- 2,179 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* radare - LGPL - Copyright 2009-2012 - pancake */

#include <r_th.h>

static void *_r_th_launcher(void *_th) {
	int ret;
	struct r_th_t *th = _th;

	th->ready = R_TRUE;
#if __UNIX__
	if (th->delay>0) sleep(th->delay);
	else if (th->delay<0) r_th_lock_wait(th->lock);
#else
	if (th->delay<0) r_th_lock_wait(th->lock);
#endif
	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;
}

R_API int r_th_push_task(struct r_th_t *th, void *user) {
	int ret = R_TRUE;
	th->user = user;
	r_th_lock_leave(th->lock);
	return ret;
}

R_API struct r_th_t *r_th_new(R_TH_FUNCTION(fun), void *user, int delay) {
	RThread *th;
	
	th = R_NEW (RThread);
	if (th) {
		th->lock = r_th_lock_new();
		th->running = R_FALSE;
		th->fun = fun;	
		th->user = user;
		th->delay = delay;
		th->breaked = R_FALSE;
		th->ready = R_FALSE;
#if HAVE_PTHREAD
		pthread_create(&th->tid, NULL, _r_th_launcher, th);
#elif __WIN32__
		th->tid = CreateThread(NULL, 0, _r_th_launcher, th, 0, &th->tid);
#endif
	}
	return th;
}

R_API void r_th_break(struct r_th_t *th) {
	th->breaked = R_TRUE;
}

R_API int r_th_kill(struct r_th_t *th, int force) {
	th->breaked = R_TRUE;
	r_th_break(th);
	r_th_wait(th);
#if HAVE_PTHREAD
#ifdef __ANDROID__
	pthread_kill (th->tid, 9);
#else
	pthread_cancel (th->tid);
#endif
#endif
	return 0;
}

R_API int r_th_start(struct r_th_t *th, int enable) {
	int ret = R_TRUE;
	if (enable) {
		if (!th->running) {
			// start thread
			while (!th->ready);
			r_th_lock_leave (th->lock);
		}
	} else {
		if (th->running) {
			// stop thread
			r_th_kill (th, 0);
			r_th_lock_enter (th->lock); // deadlock?
		}
	}
	th->running = enable;
	return ret;
}

R_API int r_th_wait(struct r_th_t *th) {
	int ret = R_FALSE;
	void *thret;
#if HAVE_PTHREAD
	if (th) {
		ret = pthread_join (th->tid, &thret);
		th->running = R_FALSE;
	}
#endif
	return ret;
}

R_API int r_th_wait_async(struct r_th_t *th) {
	return th->running;
}

R_API void *r_th_free(struct r_th_t *th) {
	r_th_kill (th, R_TRUE);
	r_th_lock_free (th->lock);
	free (th);
	return NULL;
}