1 #include "tpl_utils_gthread.h"
16 tpl_gthread_func init_func;
27 tpl_bool_t is_eventfd;
28 tpl_gsource_functions *gsource_funcs;
30 tpl_gsource_type_t type;
31 tpl_gsource *finalizer;
32 tpl_bool_t intended_destroy;
38 __gsource_remove_and_destroy(tpl_gsource *source);
41 _tpl_gthread_init(gpointer data)
43 tpl_gthread *thread = (tpl_gthread *)data;
45 g_mutex_lock(&thread->thread_mutex);
47 if (thread->init_func)
48 thread->init_func(thread->func_data);
50 g_cond_signal(&thread->thread_cond);
51 g_mutex_unlock(&thread->thread_mutex);
53 g_main_loop_run(thread->loop);
59 tpl_gthread_create(const char *thread_name,
60 tpl_gthread_func init_func, void *func_data)
62 GMainContext *context = NULL;
63 GMainLoop *loop = NULL;
64 tpl_gthread *new_thread = NULL;
66 context = g_main_context_new();
68 TPL_ERR("Failed to create GMainContext");
72 loop = g_main_loop_new(context, FALSE);
74 TPL_ERR("Failed to create GMainLoop");
75 g_main_context_unref(context);
79 // context's ref count was increased in g_main_loop_new
80 g_main_context_unref(context);
82 new_thread = calloc(1, sizeof(tpl_gthread));
84 TPL_ERR("Failed to allocate tpl_gthread");
86 // context is also destroyed when loop is destroyed.
87 g_main_loop_unref(loop);
92 g_mutex_init(&new_thread->thread_mutex);
93 g_cond_init(&new_thread->thread_cond);
95 g_mutex_init(&new_thread->pause_mutex);
96 g_mutex_init(&new_thread->idle_mutex);
97 g_cond_init(&new_thread->idle_cond);
99 new_thread->is_idle = TPL_FALSE;
100 new_thread->paused = TPL_FALSE;
102 g_mutex_lock(&new_thread->thread_mutex);
104 new_thread->loop = loop;
105 new_thread->init_func = init_func;
106 new_thread->func_data = func_data;
107 new_thread->thread = g_thread_new(thread_name,
108 _tpl_gthread_init, new_thread);
109 g_cond_wait(&new_thread->thread_cond,
110 &new_thread->thread_mutex);
112 g_mutex_unlock(&new_thread->thread_mutex);
118 tpl_gthread_destroy(tpl_gthread *thread)
120 g_mutex_lock(&thread->thread_mutex);
122 g_main_loop_quit(thread->loop);
123 g_thread_join(thread->thread);
124 g_main_loop_unref(thread->loop);
128 g_mutex_unlock(&thread->thread_mutex);
130 g_mutex_clear(&thread->pause_mutex);
131 g_mutex_clear(&thread->idle_mutex);
132 g_cond_clear(&thread->idle_cond);
134 g_mutex_clear(&thread->thread_mutex);
135 g_cond_clear(&thread->thread_cond);
137 thread->func_data = NULL;
138 thread->thread = NULL;
145 _thread_source_prepare(GSource *source, gint *time)
147 tpl_gsource *gsource = (tpl_gsource *)source;
148 tpl_bool_t ret = TPL_FALSE;
150 if (gsource->type != SOURCE_TYPE_NORMAL)
153 if (gsource->gsource_funcs && gsource->gsource_funcs->prepare)
154 ret = gsource->gsource_funcs->prepare(gsource);
162 _thread_source_check(GSource *source)
164 tpl_gsource *gsource = (tpl_gsource *)source;
165 tpl_bool_t ret = TPL_FALSE;
167 if (gsource->type != SOURCE_TYPE_NORMAL)
170 if (gsource->gsource_funcs && gsource->gsource_funcs->check)
171 ret = gsource->gsource_funcs->check(gsource);
177 _thread_source_dispatch(GSource *source, GSourceFunc cb, gpointer data)
179 tpl_gsource *gsource = (tpl_gsource *)source;
180 gboolean ret = G_SOURCE_CONTINUE;
181 GIOCondition cond = g_source_query_unix_fd(source, gsource->tag);
182 tpl_gthread *thread = gsource->thread;
186 if (cond & G_IO_IN) {
188 uint64_t message = 0;
190 if (gsource->is_eventfd) {
191 s = read(gsource->fd, &message, sizeof(uint64_t));
192 if (s != sizeof(uint64_t)) {
193 TPL_ERR("Failed to read from event_fd(%d)",
198 if (gsource->gsource_funcs && gsource->gsource_funcs->dispatch)
199 ret = gsource->gsource_funcs->dispatch(gsource, message);
201 if (gsource->type == SOURCE_TYPE_FINALIZER &&
202 gsource->intended_destroy == TPL_TRUE) {
203 tpl_gsource *del_source = (tpl_gsource *)gsource->data;
204 if (!g_source_is_destroyed(&del_source->gsource)) {
205 g_mutex_lock(&thread->thread_mutex);
207 __gsource_remove_and_destroy(del_source);
208 __gsource_remove_and_destroy(gsource);
210 g_cond_signal(&thread->thread_cond);
211 g_mutex_unlock(&thread->thread_mutex);
213 return G_SOURCE_REMOVE;
218 if (cond && !(cond & G_IO_IN)) {
219 /* When some io errors occur, it is not considered as a critical error.
220 * There may be problems with the screen, but it does not affect the operation. */
221 TPL_WARN("Invalid GIOCondition occured. tpl_gsource(%p) fd(%d) cond(%d)",
222 gsource, gsource->fd, cond);
224 if (gsource->type == SOURCE_TYPE_DISPOSABLE) {
225 if (gsource->gsource_funcs && gsource->gsource_funcs->dispatch)
226 ret = gsource->gsource_funcs->dispatch(gsource, 0);
230 if (gsource->type == SOURCE_TYPE_DISPOSABLE) {
231 g_mutex_lock(&thread->thread_mutex);
232 __gsource_remove_and_destroy(gsource);
233 ret = G_SOURCE_REMOVE;
234 g_mutex_unlock(&thread->thread_mutex);
241 _thread_source_finalize(GSource *source)
243 tpl_gsource *gsource = (tpl_gsource *)source;
245 if (gsource->gsource_funcs && gsource->gsource_funcs->finalize)
246 gsource->gsource_funcs->finalize(gsource);
248 if (gsource->is_eventfd)
252 gsource->thread = NULL;
253 gsource->gsource_funcs = NULL;
254 gsource->data = NULL;
255 gsource->finalizer = NULL;
258 static GSourceFuncs _thread_source_funcs = {
259 .prepare = _thread_source_prepare,
260 .check = _thread_source_check,
261 .dispatch = _thread_source_dispatch,
262 .finalize = _thread_source_finalize,
266 tpl_gsource_create(tpl_gthread *thread, void *data, int fd,
267 tpl_gsource_functions *funcs, tpl_gsource_type_t type)
269 tpl_gsource *new_gsource = NULL;
271 new_gsource = (tpl_gsource *)g_source_new(&_thread_source_funcs,
272 sizeof(tpl_gsource));
274 TPL_ERR("Failed to create new tpl_gsource");
279 new_gsource->fd = eventfd(0, EFD_CLOEXEC);
280 if (new_gsource->fd < 0) {
281 TPL_ERR("Failed to create eventfd. errno(%d)", errno);
282 g_source_unref(&new_gsource->gsource);
286 new_gsource->is_eventfd = TPL_TRUE;
288 new_gsource->fd = fd;
289 new_gsource->is_eventfd = TPL_FALSE;
292 new_gsource->thread = thread;
293 new_gsource->gsource_funcs = funcs;
294 new_gsource->data = data;
295 new_gsource->type = type;
296 new_gsource->intended_destroy = TPL_FALSE;
298 if (new_gsource->type == SOURCE_TYPE_NORMAL) {
299 tpl_gsource *finalizer = tpl_gsource_create(thread, new_gsource, -1,
300 NULL, SOURCE_TYPE_FINALIZER);
301 new_gsource->finalizer = finalizer;
303 new_gsource->finalizer = NULL;
305 new_gsource->tag = g_source_add_unix_fd(&new_gsource->gsource,
308 g_source_attach(&new_gsource->gsource,
309 g_main_loop_get_context(thread->loop));
311 TPL_DEBUG("[GSOURCE_CREATE] tpl_gsource(%p) thread(%p) data(%p) fd(%d) type(%d)",
312 new_gsource, thread, data, new_gsource->fd, type);
318 __gsource_remove_and_destroy(tpl_gsource *source)
320 if (g_source_is_destroyed(&source->gsource))
323 TPL_DEBUG("[GSOURCE_DESTROY] tpl_gsource(%p) type(%d)",
324 source, source->type);
326 g_source_remove_unix_fd(&source->gsource, source->tag);
327 g_source_destroy(&source->gsource);
328 g_source_unref(&source->gsource);
332 tpl_gsource_destroy(tpl_gsource *source, tpl_bool_t destroy_in_thread)
334 tpl_gthread *thread = source->thread;
336 if (g_source_is_destroyed(&source->gsource)) {
337 TPL_WARN("gsource(%p) already has been destroyed.",
342 g_mutex_lock(&thread->thread_mutex);
343 if (source->type == SOURCE_TYPE_NORMAL &&
345 tpl_gsource *finalizer = source->finalizer;
347 if (destroy_in_thread) {
348 finalizer->intended_destroy = TPL_TRUE;
349 tpl_gsource_send_message(finalizer, 1);
351 __gsource_remove_and_destroy(finalizer);
352 source->finalizer = NULL;
356 if (!destroy_in_thread) {
357 __gsource_remove_and_destroy(source);
359 g_mutex_unlock(&thread->thread_mutex);
363 tpl_gsource_send_message(tpl_gsource *source, uint64_t message)
365 uint64_t value = message;
368 if (!source->is_eventfd) {
369 TPL_ERR("source is not using eventfd. source(%p) fd(%d)",
374 ret = write(source->fd, &value, sizeof(uint64_t));
376 TPL_ERR("failed to send devent. tpl_gsource(%p)",
382 tpl_gsource_get_data(tpl_gsource *source)
393 tpl_gsource_check_io_condition(tpl_gsource *source)
398 TPL_ERR("Invalid parameter tpl_gsource is null");
402 cond = g_source_query_unix_fd(&source->gsource, source->tag);
410 tpl_gmutex_init(tpl_gmutex *gmutex)
412 g_mutex_init(gmutex);
416 tpl_gmutex_clear(tpl_gmutex *gmutex)
418 g_mutex_clear(gmutex);
422 tpl_gmutex_lock(tpl_gmutex *gmutex)
424 g_mutex_lock(gmutex);
428 tpl_gmutex_unlock(tpl_gmutex *gmutex)
430 g_mutex_unlock(gmutex);
434 tpl_gcond_init(tpl_gcond *gcond)
440 tpl_gcond_clear(tpl_gcond *gcond)
446 tpl_gcond_wait(tpl_gcond *gcond, tpl_gmutex *gmutex)
448 g_cond_wait(gcond, gmutex);
452 tpl_gcond_timed_wait(tpl_gcond *gcond, tpl_gmutex *gmutex,
455 gint64 end_time = g_get_monotonic_time() +
456 (timeout_ms * G_TIME_SPAN_MILLISECOND);
457 if (!g_cond_wait_until(gcond, gmutex, end_time))
458 return TPL_ERROR_TIME_OUT;
460 return TPL_ERROR_NONE;
464 tpl_gcond_signal(tpl_gcond *gcond)
466 g_cond_signal(gcond);
470 _thread_idle_cb(gpointer data)
472 tpl_gthread *gthread = (tpl_gthread *)data;
474 TPL_DEBUG("THREAD IDLE CALLBACK");
476 g_mutex_lock(>hread->idle_mutex);
477 gthread->is_idle = TPL_TRUE;
478 g_cond_signal(>hread->idle_cond);
479 g_mutex_unlock(>hread->idle_mutex);
481 /* If the caller thread of tpl_gthread_wait_idle locked the pause_mutex,
482 * thread will be paused here until unlock */
483 g_mutex_lock(>hread->pause_mutex);
484 g_mutex_unlock(>hread->pause_mutex);
486 return G_SOURCE_REMOVE;
490 tpl_gthread_wait_idle(tpl_gthread *gthread)
492 TPL_CHECK_ON_NULL_RETURN(gthread);
494 GSource *idle_source = NULL;
498 TPL_DEBUG("[WAIT IDLE] BEGIN");
500 g_mutex_lock(>hread->idle_mutex);
501 gthread->is_idle = TPL_FALSE;
503 idle_source = g_idle_source_new();
504 if (idle_source == NULL) {
505 TPL_WARN("Failed to create and attach idle source");
506 g_mutex_unlock(>hread->idle_mutex);
510 g_source_set_priority(idle_source, G_PRIORITY_LOW);
511 g_source_set_callback(idle_source,
512 _thread_idle_cb, (gpointer)gthread,
514 g_source_attach(idle_source, g_main_loop_get_context(gthread->loop));
515 g_source_unref(idle_source);
518 end_time = g_get_monotonic_time() +
519 (200 * G_TIME_SPAN_MILLISECOND);
521 while (!gthread->is_idle) {
522 ret = g_cond_wait_until(>hread->idle_cond,
523 >hread->idle_mutex,
526 TPL_ERR("wait_idle timeout!");
530 g_mutex_unlock(>hread->idle_mutex);
532 TPL_DEBUG("[WAIT IDLE] END");
536 tpl_gthread_pause_in_idle(tpl_gthread *gthread)
538 TPL_CHECK_ON_NULL_RETURN(gthread);
540 g_mutex_lock(>hread->pause_mutex);
541 tpl_gthread_wait_idle(gthread);
542 gthread->paused = TPL_TRUE;
546 tpl_gthread_continue(tpl_gthread *gthread)
548 TPL_CHECK_ON_NULL_RETURN(gthread);
550 if (!gthread->paused) return;
551 gthread->paused = TPL_FALSE;
552 g_mutex_unlock(>hread->pause_mutex);