This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathevent_thread.c
307 lines (252 loc) · 6.81 KB
/
event_thread.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2017 Dariusz Stojaczyk. All Rights Reserved.
* The following source code is released under an MIT-style license,
* that can be found in the LICENSE file.
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include "event_thread.h"
#include "con_queue.h"
#include "log.h"
#define MAX_EVENT_THREADS 32
struct event {
void (*callback)(void *, void *);
void *arg1;
void *arg2;
};
enum event_thread_state {
EVENT_THREAD_RUNNING,
EVENT_THREAD_SLEEPING,
EVENT_THREAD_STOPPED,
};
struct event_thread {
enum event_thread_state state;
char *name;
void (*update_cb)(void *);
void (*stop_cb)(void *);
void *arg;
struct con_queue *events;
pthread_t tid;
sem_t sem;
};
static atomic_int g_thread_cnt;
static struct event_thread g_threads[MAX_EVENT_THREADS];
static struct event *
allocate_event(void (*callback)(void *, void *), void *arg1, void *arg2)
{
struct event *event;
event = calloc(1, sizeof(*event));
if (!event) {
LOG_FATAL("Failed to allocate memory for event.\n");
return NULL;
}
event->callback = callback;
event->arg1 = arg1;
event->arg2 = arg2;
return event;
}
int
event_thread_enqueue_event(struct event_thread *thread,
void (*callback)(void *, void *), void *arg1, void *arg2)
{
struct event *event;
if (!thread) {
LOG_FATAL("Trying to enqueue event to inexistent thread.\n");
return -1;
}
event = allocate_event(callback, arg1, arg2);
if (event == NULL) {
LOG_FATAL("Failed to allocate event for enqueuing.\n");
return -1;
}
con_queue_push(thread->events, event);
return 0;
}
static void
event_thread_destroy(struct event_thread *thread)
{
struct event *event;
while (con_queue_pop(thread->events, (void **) &event) == 0) {
free(event);
}
free(thread->events);
free(thread->name);
sem_destroy(&thread->sem);
}
static void
event_thread_set_state_cb(void *arg1, void *arg2)
{
struct event_thread *thread = arg1;
if (thread->state == EVENT_THREAD_STOPPED) {
/* thread will be stopped with current loop tick */
return;
}
thread->state = (enum event_thread_state)(intptr_t) arg2;
}
int
event_thread_pause(struct event_thread *thread)
{
if (thread->state == EVENT_THREAD_STOPPED) {
LOG_FATAL("Can't stop thread %p. It's not running.\n", thread);
return -1;
}
if (event_thread_enqueue_event(thread, event_thread_set_state_cb, thread,
(void *)(intptr_t) EVENT_THREAD_SLEEPING) != 0) {
LOG_FATAL("Failed to pause thread %p.\n", thread);
return -1;
}
return 0;
}
int
event_thread_kick(struct event_thread *thread)
{
if (event_thread_enqueue_event(thread, event_thread_set_state_cb, thread,
(void *)(intptr_t) EVENT_THREAD_RUNNING) != 0) {
LOG_FATAL("Failed to wake thread %p.\n", thread);
return -1;
}
sem_post(&thread->sem);
return 0;
}
static void *
event_thread_loop(void *arg)
{
struct event_thread *thread = arg;
struct event *event;
sigset_t sigset;
sem_init(&thread->sem, 0, 0);
while (thread->state != EVENT_THREAD_STOPPED) {
while (con_queue_pop(thread->events, (void **) &event) == 0) {
event->callback(event->arg1, event->arg2);
free(event);
}
if (thread->state == EVENT_THREAD_RUNNING && thread->update_cb) {
thread->update_cb(thread->arg);
}
if (thread->state == EVENT_THREAD_SLEEPING) {
sem_wait(&thread->sem);
}
}
if (thread->stop_cb) {
thread->stop_cb(thread->arg);
}
event_thread_destroy(thread);
pthread_exit(NULL);
return NULL;
}
struct event_thread *
event_thread_create(const char *name, void (*update_cb)(void *),
void (*stop_cb)(void *), void *arg)
{
struct event_thread *thread;
int thread_id, rc;
thread_id = atomic_fetch_add(&g_thread_cnt, 1);
if (thread_id >= MAX_EVENT_THREADS) {
LOG_FATAL("Reached the thread limit (%d).\n", MAX_EVENT_THREADS);
goto name_err;
}
thread = &g_threads[thread_id];
thread->state = EVENT_THREAD_RUNNING;
thread->name = strdup(name);
if (!thread->name) {
LOG_ERR("strdup() failed.\n");
goto err;
}
thread->events = calloc(1, sizeof(*thread->events) + 32 * sizeof(void *));
if (!thread->events) {
LOG_ERR("calloc() failed.\n");
goto name_err;
}
thread->events->size = 32;
thread->update_cb = update_cb;
thread->stop_cb = stop_cb;
thread->arg = arg;
rc = pthread_create(&thread->tid, NULL, event_thread_loop, thread);
if (rc != 0) {
LOG_ERR("pthread_create() failed: %s.\n", strerror(-rc));
goto events_err;
}
return thread;
events_err:
free(thread->events);
name_err:
free(thread->name);
err:
return NULL;
}
int
event_thread_stop(struct event_thread *thread)
{
if (thread->state == EVENT_THREAD_STOPPED) {
LOG_ERR("Thread %p is not running.\n", (void *)thread);
return -1;
}
if (event_thread_enqueue_event(thread, event_thread_set_state_cb, thread,
(void *)(intptr_t) EVENT_THREAD_STOPPED) != 0) {
LOG_ERR("Failed to stop thread %p.\n", (void *)thread);
return -1;
}
sem_post(&thread->sem);
return 0;
}
struct event_thread *
event_thread_self(void)
{
struct event_thread *thread_tmp;
int i;
for (i = 0; i < MAX_EVENT_THREADS; ++i) {
thread_tmp = &g_threads[i];
if (thread_tmp->tid == pthread_self()) {
return thread_tmp;
}
}
return NULL;
}
void
event_thread_lib_init(void)
{
atomic_init(&g_thread_cnt, 0);
}
void
event_thread_lib_wait(void)
{
struct event_thread *thread;
int i;
for (i = 0; i < MAX_EVENT_THREADS; ++i) {
thread = &g_threads[i];
if (thread->tid && thread->state != EVENT_THREAD_STOPPED) {
pthread_join(thread->tid, NULL);
event_thread_lib_wait();
return;
}
}
fflush(stdout);
}
static void *
event_thread_lib_shutdown_cb(void *arg)
{
struct event_thread *thread;
int i;
for (i = 0; i < MAX_EVENT_THREADS; ++i) {
thread = &g_threads[i];
if (thread->tid && thread->state != EVENT_THREAD_STOPPED) {
event_thread_stop(thread);
}
}
return NULL;
}
void
event_thread_lib_shutdown(void)
{
pthread_t tid;
if (pthread_create(&tid, NULL, event_thread_lib_shutdown_cb, NULL) != 0) {
LOG_FATAL("pthread_create() failed, cannot start shutdown thread.\n");
abort();
}
pthread_detach(tid);
}