1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <boram1288.park@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
40 #include "tdm_private.h"
42 static tdm_private_thread *keep_private_thread;
44 struct _tdm_private_thread {
45 tdm_private_loop *private_loop;
47 pthread_cond_t event_cond;
48 pthread_t event_thread;
49 unsigned int event_thread_exit;
50 unsigned int event_thread_joined;
55 /* 0: read, 1: write (tdm-thread -> display-thread) */
58 /* 0: read, 1: write (tdm-thread <- display-thread) */
60 tdm_event_loop_source *sub_event_source;
63 typedef struct _tdm_private_thread_cb {
64 struct list_head link;
65 struct list_head call_link;
68 tdm_thread_cb_type cb_type;
74 } tdm_private_thread_cb;
76 static tdm_thread_find_object find_funcs[TDM_THREAD_CB_MAX] = {0, };
78 /* 0: for display thread, 1: for tdm thread */
79 static struct list_head cb_list[2];
80 static pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
82 static void _tdm_thread_free_cb(tdm_private_thread_cb *cb);
85 _tdm_thread_handle_events(int fd, tdm_event_loop_mask mask, void *user_data)
87 tdm_private_loop *private_loop = user_data;
90 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
91 TDM_RETURN_VAL_IF_FAIL(private_loop != NULL, TDM_ERROR_OPERATION_FAILED);
92 TDM_RETURN_VAL_IF_FAIL(private_loop->dpy != NULL, TDM_ERROR_OPERATION_FAILED);
94 ret = tdm_thread_handle_cb(private_loop);
96 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
98 return TDM_ERROR_NONE;
102 _tdm_thread_main(void *data)
104 tdm_private_thread *private_thread = (tdm_private_thread*)data;
105 tdm_private_loop *private_loop = private_thread->private_loop;
111 /* Not lock/unlock for the private_thread and private_loop structure
112 * because they won't be destroyed as long as tdm thread is running.
113 * When they're destroyed, we have already exit the tdm thread.
115 private_thread->thread_tid = syscall(SYS_gettid);
117 TDM_INFO("display_tid:%d, thread_tid: %d",
118 private_thread->display_tid, private_thread->thread_tid);
120 /* fd SHOULD be the same with sub_pipe[0] to make sure that using
121 * tdm_thread_get_fd, tdm_thread_send_cb, tdm_thread_handle_cb in
122 * both threads is fine. Otherwise, assert.
124 tdm_display_lock(private_loop->dpy);
125 assert(tdm_thread_get_fd(private_loop) == private_thread->sub_pipe[0]);
127 private_thread->sub_event_source =
128 tdm_event_loop_add_fd_handler(private_loop->dpy,
129 private_thread->sub_pipe[0],
130 TDM_EVENT_LOOP_READABLE,
131 _tdm_thread_handle_events,
134 TDM_GOTO_IF_FAIL(error == TDM_ERROR_NONE, exit_thread);
135 TDM_GOTO_IF_FAIL(private_thread->sub_event_source != NULL, exit_thread);
137 pthread_cond_signal(&private_thread->event_cond);
139 /* mutex shall be locked by the thread calling pthread_cond_signal() */
140 tdm_display_unlock(private_loop->dpy);
142 fd = tdm_event_loop_get_fd(private_loop->dpy);
144 TDM_ERR("couldn't get fd");
153 if (tdm_debug_module & TDM_DEBUG_EVENT)
154 TDM_INFO("server flush");
155 tdm_event_loop_flush(private_loop->dpy);
157 if (private_thread->event_thread_exit)
160 if (tdm_debug_module & TDM_DEBUG_EVENT)
161 TDM_INFO("fd(%d) polling in", fd);
163 ret = poll(&fds, 1, -1);
165 if (tdm_debug_module & TDM_DEBUG_EVENT)
166 TDM_INFO("fd(%d) polling out", fd);
169 if (errno == EINTR || errno == EAGAIN) /* normal case */
172 TDM_ERR("poll failed: %m");
177 if (tdm_debug_module & TDM_DEBUG_EVENT)
178 TDM_INFO("thread got events");
180 if (tdm_event_loop_dispatch(private_loop->dpy) < 0)
181 TDM_ERR("dispatch error");
185 tdm_display_unlock(private_loop->dpy);
190 _tdm_thread_exit(tdm_private_loop *private_loop)
192 tdm_thread_cb_base cb_base;
195 memset(&cb_base, 0, sizeof cb_base);
196 cb_base.type = TDM_THREAD_CB_EXIT;
197 cb_base.length = sizeof cb_base;
199 ret = tdm_thread_send_cb(private_loop, &cb_base);
200 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
205 /* NOTE: tdm thread doesn't care about multi-thread. */
207 tdm_thread_init(tdm_private_loop *private_loop)
209 tdm_private_display *private_display;
210 tdm_private_thread *private_thread;
213 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
214 TDM_RETURN_VAL_IF_FAIL(private_loop->dpy, TDM_ERROR_OPERATION_FAILED);
216 private_display = private_loop->dpy;
217 TDM_RETURN_VAL_IF_FAIL(private_display->private_loop, TDM_ERROR_OPERATION_FAILED);
219 if (private_loop->private_thread)
220 return TDM_ERROR_NONE;
222 for (i = 0; i < TDM_THREAD_CB_MAX; i++)
223 find_funcs[i] = NULL;
225 LIST_INITHEAD(&cb_list[0]);
226 LIST_INITHEAD(&cb_list[1]);
228 /* enable as default */
229 thread = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1);
231 TDM_INFO("not using a TDM event thread");
232 return TDM_ERROR_NONE;
235 private_thread = calloc(1, sizeof * private_thread);
236 if (!private_thread) {
237 TDM_ERR("alloc failed");
238 return TDM_ERROR_OUT_OF_MEMORY;
241 if (pthread_cond_init(&private_thread->event_cond, NULL)) {
242 TDM_ERR("pthread_cond_init failed: %m");
243 free(private_thread);
244 return TDM_ERROR_OUT_OF_MEMORY;
247 if (pipe(private_thread->pipe) != 0) {
248 TDM_ERR("pipe failed: %m");
249 pthread_cond_destroy(&private_thread->event_cond);
250 free(private_thread);
251 return TDM_ERROR_OPERATION_FAILED;
254 if (pipe(private_thread->sub_pipe) != 0) {
255 TDM_ERR("sub_pipe failed: %m");
256 close(private_thread->pipe[0]);
257 close(private_thread->pipe[1]);
258 pthread_cond_destroy(&private_thread->event_cond);
259 free(private_thread);
260 return TDM_ERROR_OPERATION_FAILED;
263 keep_private_thread = private_thread;
265 private_thread->private_loop = private_loop;
266 private_loop->private_thread = private_thread;
268 private_thread->display_tid = syscall(SYS_gettid);
270 /* pthread_cond_wait atomically release mutex, Upon successful return,
271 * the mutex shall have been locked and shall be owned by the calling thread
273 tdm_mutex_locked = 0;
274 pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
277 /* wait until the tdm thread starts */
278 pthread_cond_wait(&private_thread->event_cond, &private_display->lock);
279 tdm_mutex_locked = 1;
281 TDM_INFO("using a TDM event thread. pipe(%d,%d) sub_pipe(%d,%d)",
282 private_thread->pipe[0], private_thread->pipe[1],
283 private_thread->sub_pipe[0], private_thread->sub_pipe[1]);
285 return TDM_ERROR_NONE;
289 tdm_thread_deinit(tdm_private_loop *private_loop)
291 tdm_private_display *private_display;
292 tdm_private_thread_cb *cb = NULL, *hh = NULL;
295 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
297 if (!private_loop->private_thread)
300 ret = _tdm_thread_exit(private_loop);
301 if (ret != TDM_ERROR_NONE)
302 pthread_cancel(private_loop->private_thread->event_thread);
304 private_display = private_loop->dpy;
306 /* before falling into the block of pthread_join, we have to unlock the mutex
307 * for subthread to use the mutex.
309 _pthread_mutex_unlock(&private_display->lock);
310 pthread_join(private_loop->private_thread->event_thread, NULL);
311 private_loop->private_thread->event_thread_joined = 1;
312 TDM_INFO("Joined a TDM event thread");
314 pthread_mutex_unlock(&cb_list_lock);
317 if (private_loop->private_thread->sub_event_source)
318 tdm_event_loop_source_remove(private_loop->private_thread->sub_event_source);
320 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[0], link) {
321 _tdm_thread_free_cb(cb);
323 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[1], link) {
324 _tdm_thread_free_cb(cb);
327 if (private_loop->private_thread->pipe[0] >= 0)
328 close(private_loop->private_thread->pipe[0]);
329 if (private_loop->private_thread->pipe[1] >= 0)
330 close(private_loop->private_thread->pipe[1]);
332 if (private_loop->private_thread->sub_pipe[0] >= 0)
333 close(private_loop->private_thread->sub_pipe[0]);
334 if (private_loop->private_thread->sub_pipe[1] >= 0)
335 close(private_loop->private_thread->sub_pipe[1]);
337 pthread_cond_destroy(&private_loop->private_thread->event_cond);
339 free(private_loop->private_thread);
340 private_loop->private_thread = NULL;
341 keep_private_thread = NULL;
343 TDM_INFO("Finish a TDM event thread");
347 tdm_thread_get_fd(tdm_private_loop *private_loop)
349 tdm_private_thread *private_thread;
352 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
353 TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
354 TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
356 private_thread = private_loop->private_thread;
358 /* seems like ticky. but easy way to use the same APIs for both threads */
359 in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
362 return private_thread->pipe[0];
364 return private_thread->sub_pipe[0];
368 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
370 tdm_private_thread *private_thread;
374 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
375 TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
376 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
377 TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
379 private_thread = private_loop->private_thread;
381 /* seems like ticky. but easy way to use the same APIs for both threads */
382 in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
385 pipe = private_thread->sub_pipe[1];
387 pipe = private_thread->pipe[1];
389 if (tdm_debug_module & TDM_DEBUG_THREAD)
390 TDM_INFO("fd(%d) type(%s), length(%d)", pipe, tdm_cb_type_str(base->type), base->length);
392 len = write(pipe, base, base->length);
393 if (len != base->length) {
394 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
395 return TDM_ERROR_OPERATION_FAILED;
398 if (tdm_debug_module & TDM_DEBUG_THREAD)
399 TDM_INFO("[%s] write fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
401 return TDM_ERROR_NONE;
405 tdm_thread_handle_cb(tdm_private_loop *private_loop)
407 tdm_private_thread *private_thread;
408 tdm_thread_cb_base *base;
411 int len, pipe, in_main;
412 tdm_error ret = TDM_ERROR_NONE;
414 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
415 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
416 TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
418 private_thread = private_loop->private_thread;
420 /* seems like ticky. but easy way to use the same APIs for both threads */
421 in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
424 pipe = private_thread->pipe[0];
426 pipe = private_thread->sub_pipe[0];
429 len = read(pipe, buffer, sizeof buffer);
430 } while (len < 0 && errno == EINTR);
432 if (tdm_debug_module & TDM_DEBUG_THREAD)
433 TDM_INFO("[%s] read fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
436 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
437 return TDM_ERROR_OPERATION_FAILED;
441 return TDM_ERROR_NONE;
443 if (len < sizeof * base) {
444 TDM_ERR("read failed: len(%d)", len);
445 return TDM_ERROR_OPERATION_FAILED;
450 base = (tdm_thread_cb_base*)&buffer[i];
451 if (tdm_debug_module & TDM_DEBUG_THREAD)
452 TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length);
453 switch (base->type) {
454 case TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE:
455 case TDM_THREAD_CB_OUTPUT_DESTROY:
456 case TDM_THREAD_CB_OUTPUT_COMMIT:
457 case TDM_THREAD_CB_OUTPUT_VBLANK:
458 case TDM_THREAD_CB_OUTPUT_STATUS:
459 case TDM_THREAD_CB_OUTPUT_DPMS:
460 case TDM_THREAD_CB_PP_DONE:
461 case TDM_THREAD_CB_CAPTURE_DONE:
462 case TDM_THREAD_CB_VBLANK_SW:
463 case TDM_THREAD_CB_VBLANK_CREATE:
464 case TDM_THREAD_CB_HWC_COMMIT:
465 /* this event comes from other thread. so we don't need to propagate this to other thread */
466 ret = tdm_thread_cb_call(NULL, base, 0);
467 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
469 case TDM_THREAD_CB_EXIT:
470 private_thread->event_thread_exit = 1;
478 return TDM_ERROR_NONE;
482 tdm_thread_in_display_thread(pid_t tid)
484 if (!keep_private_thread)
487 /* DON'T check TDM_MUTEX_IS_LOCKED here */
489 return (keep_private_thread->display_tid == tid) ? 1 : 0;
493 tdm_thread_is_running(void)
495 /* DON'T check TDM_MUTEX_IS_LOCKED here */
497 return (keep_private_thread && !keep_private_thread->event_thread_joined) ? 1 : 0;
501 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
503 if (tdm_debug_module & TDM_DEBUG_THREAD)
504 TDM_INFO("cb(%p) removed", cb);
506 assert(LIST_IS_EMPTY(&cb->call_link));
512 static tdm_private_thread_cb *
513 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
514 void *cb_data, tdm_thread_cb func, void *user_data)
516 tdm_private_thread_cb *cb = NULL;
518 LIST_FOR_EACH_ENTRY(cb, list, link) {
519 if (cb->object == object &&
520 cb->cb_type == cb_type &&
521 cb->cb_data == cb_data &&
523 cb->user_data == user_data)
531 tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func)
533 TDM_RETURN_IF_FAIL(cb_type > 0);
535 if (func && find_funcs[cb_type])
536 TDM_NEVER_GET_HERE();
538 find_funcs[cb_type] = func;
542 tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
544 tdm_private_thread_cb *cb = NULL;
546 struct list_head *list;
548 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
549 TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER);
550 TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER);
551 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
553 caller_tid = syscall(SYS_gettid);
555 pthread_mutex_lock(&cb_list_lock);
557 if (tdm_thread_in_display_thread(caller_tid))
562 cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
564 pthread_mutex_unlock(&cb_list_lock);
565 TDM_ERR("can't be added twice with same data");
566 return TDM_ERROR_BAD_REQUEST;
569 cb = calloc(1, sizeof *cb);
571 pthread_mutex_unlock(&cb_list_lock);
572 TDM_ERR("calloc failed");
573 return TDM_ERROR_OUT_OF_MEMORY;
576 LIST_ADDTAIL(&cb->link, list);
577 LIST_INITHEAD(&cb->call_link);
580 cb->cb_type = cb_type;
581 cb->cb_data = cb_data;
583 cb->user_data = user_data;
584 cb->owner_tid = caller_tid;
586 if (tdm_debug_module & TDM_DEBUG_THREAD)
587 TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb);
589 pthread_mutex_unlock(&cb_list_lock);
591 return TDM_ERROR_NONE;
595 tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
597 tdm_private_thread_cb *cb;
598 struct list_head *list;
600 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
601 TDM_RETURN_IF_FAIL(object != NULL);
602 TDM_RETURN_IF_FAIL(cb_type > 0);
603 TDM_RETURN_IF_FAIL(func != NULL);
605 pthread_mutex_lock(&cb_list_lock);
608 cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
610 _tdm_thread_free_cb(cb);
613 cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
615 _tdm_thread_free_cb(cb);
617 pthread_mutex_unlock(&cb_list_lock);
620 /* when call a callback, we check both cb_base's type and cb_base's data,
621 * because a callback is added with cb_type and cb_data.
624 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base, unsigned int propagation)
626 tdm_private_display *private_display = tdm_display_get();
627 tdm_private_thread_cb *cb = NULL, *hh = NULL;
628 int handler_in_other_thread = 0;
630 struct list_head *list, *other_list;
631 struct list_head call_list;
632 static pid_t waiting_tid = 0;
635 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
636 TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
637 TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
638 TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER);
639 TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
641 caller_tid = syscall(SYS_gettid);
643 assert(find_funcs[cb_base->type] != NULL);
645 if (keep_private_thread && keep_private_thread->thread_tid != caller_tid) {
646 /* A sync-type event from display-thread to tdm-thread can't be handled.
647 * If sync-type events happen in both threads at the same time,
648 * it would make a deadlock issue.
650 assert(cb_base->sync != 1);
653 if (tdm_debug_module & TDM_DEBUG_THREAD)
654 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------",
655 tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
658 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
660 TDM_WRN("%p gone", object);
661 return TDM_ERROR_NONE;
665 pthread_mutex_lock(&cb_list_lock);
667 if (tdm_thread_in_display_thread(caller_tid)) {
669 other_list = &cb_list[1];
671 other_list = &cb_list[0];
675 LIST_INITHEAD(&call_list);
677 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
678 if (cb->object != object ||
679 cb->cb_type != cb_base->type ||
680 cb->cb_data != cb_base->data)
683 LIST_ADDTAIL(&cb->call_link, &call_list);
686 if (!LIST_IS_EMPTY(&call_list)) {
687 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
688 LIST_DELINIT(&cb->call_link);
689 if (tdm_debug_module & TDM_DEBUG_THREAD)
690 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
691 pthread_mutex_unlock(&cb_list_lock);
692 cb->func(private_display, cb->object, cb_base, cb->user_data);
693 pthread_mutex_lock(&cb_list_lock);
697 pthread_mutex_unlock(&cb_list_lock);
700 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
701 if (cb->object != object ||
702 cb->cb_type != cb_base->type ||
703 cb->cb_data != cb_base->data)
706 handler_in_other_thread = 1;
711 if (!handler_in_other_thread) {
712 if (keep_private_thread) {
714 * In case of 'cb_base->sync = 0' and 'waiting_tid != 0',
715 * probably it means one thread is waiting for signal of
718 if (!cb_base->sync && waiting_tid != 0) {
720 pthread_cond_signal(&keep_private_thread->event_cond);
721 if (tdm_debug_module & TDM_DEBUG_THREAD)
722 TDM_INFO("pthread broadcase");
725 if (tdm_debug_module & TDM_DEBUG_THREAD)
726 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
727 tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
728 return TDM_ERROR_NONE;
731 /* Once we reach here, it means that keep_private_thread is not NULL.
732 * Just make the crash. Avoiding it is not going to help us.
734 assert(keep_private_thread != NULL);
736 if (!cb_base->sync) {
737 ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
738 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
740 /* waiting until all cb are done in another thread */
743 * Sync type event from display-thread(main thread) can't be
744 * handled. In this way, if we call tdm_thread_send_cb() with
745 * 'cb_base->sync = 1', then libtdm will eveventually raise
746 * abort(). Please see commit '4abfab36' for more detail.
749 ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
750 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
753 /* if waiting_tid is not 0, it means there are two sync-type events at the same time.
754 * and it would make deadlock issue.
756 assert(waiting_tid == 0);
758 if (tdm_debug_module & TDM_DEBUG_THREAD)
759 TDM_INFO("pthread wait");
761 /* pthread_cond_wait atomically release mutex, Upon successful return,
762 * the mutex shall have been locked and shall be owned by the calling thread
764 tdm_mutex_locked = 0;
765 waiting_tid = caller_tid;
766 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
767 tdm_mutex_locked = 1;
770 if (tdm_debug_module & TDM_DEBUG_THREAD)
771 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
772 tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
774 return TDM_ERROR_NONE;