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 <sc1.lim@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;
296 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
298 if (!private_loop->private_thread) {
299 for (i = 0; i < TDM_THREAD_CB_MAX; i++)
300 find_funcs[i] = NULL;
304 ret = _tdm_thread_exit(private_loop);
305 if (ret != TDM_ERROR_NONE)
306 pthread_cancel(private_loop->private_thread->event_thread);
308 private_display = private_loop->dpy;
310 /* before falling into the block of pthread_join, we have to unlock the mutex
311 * for subthread to use the mutex.
313 _pthread_mutex_unlock(&private_display->lock);
314 pthread_join(private_loop->private_thread->event_thread, NULL);
315 private_loop->private_thread->event_thread_joined = 1;
316 TDM_INFO("Joined a TDM event thread");
318 pthread_mutex_unlock(&cb_list_lock);
321 if (private_loop->private_thread->sub_event_source)
322 tdm_event_loop_source_remove(private_loop->private_thread->sub_event_source);
324 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[0], link) {
325 _tdm_thread_free_cb(cb);
327 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[1], link) {
328 _tdm_thread_free_cb(cb);
331 if (private_loop->private_thread->pipe[0] >= 0)
332 close(private_loop->private_thread->pipe[0]);
333 if (private_loop->private_thread->pipe[1] >= 0)
334 close(private_loop->private_thread->pipe[1]);
336 if (private_loop->private_thread->sub_pipe[0] >= 0)
337 close(private_loop->private_thread->sub_pipe[0]);
338 if (private_loop->private_thread->sub_pipe[1] >= 0)
339 close(private_loop->private_thread->sub_pipe[1]);
341 pthread_cond_destroy(&private_loop->private_thread->event_cond);
343 free(private_loop->private_thread);
344 private_loop->private_thread = NULL;
345 keep_private_thread = NULL;
347 for (i = 0; i < TDM_THREAD_CB_MAX; i++)
348 find_funcs[i] = NULL;
350 TDM_INFO("Finish a TDM event thread");
354 tdm_thread_get_fd(tdm_private_loop *private_loop)
356 tdm_private_thread *private_thread;
359 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
360 TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
361 TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
363 private_thread = private_loop->private_thread;
365 /* seems like ticky. but easy way to use the same APIs for both threads */
366 in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
369 return private_thread->pipe[0];
371 return private_thread->sub_pipe[0];
375 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
377 tdm_private_thread *private_thread;
381 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
382 TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
383 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
384 TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
386 private_thread = private_loop->private_thread;
388 /* seems like ticky. but easy way to use the same APIs for both threads */
389 in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
392 pipe = private_thread->sub_pipe[1];
394 pipe = private_thread->pipe[1];
396 if (tdm_debug_module & TDM_DEBUG_THREAD)
397 TDM_INFO("fd(%d) type(%s), length(%d)", pipe, tdm_cb_type_str(base->type), base->length);
399 len = write(pipe, base, base->length);
400 if (len != base->length) {
401 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
402 return TDM_ERROR_OPERATION_FAILED;
405 if (tdm_debug_module & TDM_DEBUG_THREAD)
406 TDM_INFO("[%s] write fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
408 return TDM_ERROR_NONE;
412 tdm_thread_handle_cb(tdm_private_loop *private_loop)
414 tdm_private_thread *private_thread;
415 tdm_thread_cb_base *base;
418 int len, pipe, in_main;
419 tdm_error ret = TDM_ERROR_NONE;
421 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
422 TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
423 TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
425 private_thread = private_loop->private_thread;
427 /* seems like ticky. but easy way to use the same APIs for both threads */
428 in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
431 pipe = private_thread->pipe[0];
433 pipe = private_thread->sub_pipe[0];
436 len = read(pipe, buffer, sizeof buffer);
437 } while (len < 0 && errno == EINTR);
439 if (tdm_debug_module & TDM_DEBUG_THREAD)
440 TDM_INFO("[%s] read fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
443 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
444 return TDM_ERROR_OPERATION_FAILED;
448 return TDM_ERROR_NONE;
450 if (len < sizeof * base) {
451 TDM_ERR("read failed: len(%d)", len);
452 return TDM_ERROR_OPERATION_FAILED;
457 base = (tdm_thread_cb_base*)&buffer[i];
458 if (tdm_debug_module & TDM_DEBUG_THREAD)
459 TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length);
460 switch (base->type) {
461 case TDM_THREAD_CB_OUTPUT_COMMIT:
462 case TDM_THREAD_CB_OUTPUT_VBLANK:
463 case TDM_THREAD_CB_OUTPUT_STATUS:
464 case TDM_THREAD_CB_OUTPUT_DPMS:
465 case TDM_THREAD_CB_PP_DONE:
466 case TDM_THREAD_CB_CAPTURE_DONE:
467 case TDM_THREAD_CB_VBLANK_SW:
468 case TDM_THREAD_CB_VBLANK_CREATE:
469 case TDM_THREAD_CB_NEED_VALIDATE:
470 /* this event comes from other thread. so we don't need to propagate this to other thread */
471 ret = tdm_thread_cb_call(NULL, base, 0);
472 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
474 case TDM_THREAD_CB_EXIT:
475 private_thread->event_thread_exit = 1;
483 return TDM_ERROR_NONE;
487 tdm_thread_in_display_thread(pid_t tid)
489 if (!keep_private_thread)
492 /* DON'T check TDM_MUTEX_IS_LOCKED here */
494 return (keep_private_thread->display_tid == tid) ? 1 : 0;
498 tdm_thread_is_running(void)
500 /* DON'T check TDM_MUTEX_IS_LOCKED here */
502 return (keep_private_thread && !keep_private_thread->event_thread_joined) ? 1 : 0;
506 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
508 if (tdm_debug_module & TDM_DEBUG_THREAD)
509 TDM_INFO("cb(%p) removed", cb);
511 assert(LIST_IS_EMPTY(&cb->call_link));
517 static tdm_private_thread_cb *
518 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
519 void *cb_data, tdm_thread_cb func, void *user_data)
521 tdm_private_thread_cb *cb = NULL;
523 LIST_FOR_EACH_ENTRY(cb, list, link) {
524 if (cb->object == object &&
525 cb->cb_type == cb_type &&
526 cb->cb_data == cb_data &&
528 cb->user_data == user_data)
536 tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func)
538 TDM_RETURN_IF_FAIL(cb_type > 0);
540 if (func && find_funcs[cb_type])
541 TDM_NEVER_GET_HERE();
543 find_funcs[cb_type] = func;
547 tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
549 tdm_private_thread_cb *cb = NULL;
551 struct list_head *list;
553 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
554 TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER);
555 TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER);
556 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
558 caller_tid = syscall(SYS_gettid);
560 pthread_mutex_lock(&cb_list_lock);
562 if (tdm_thread_in_display_thread(caller_tid))
567 cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
569 pthread_mutex_unlock(&cb_list_lock);
570 TDM_ERR("can't be added twice with same data");
571 return TDM_ERROR_BAD_REQUEST;
574 cb = calloc(1, sizeof *cb);
576 pthread_mutex_unlock(&cb_list_lock);
577 TDM_ERR("calloc failed");
578 return TDM_ERROR_OUT_OF_MEMORY;
581 LIST_ADDTAIL(&cb->link, list);
582 LIST_INITHEAD(&cb->call_link);
585 cb->cb_type = cb_type;
586 cb->cb_data = cb_data;
588 cb->user_data = user_data;
589 cb->owner_tid = caller_tid;
591 if (tdm_debug_module & TDM_DEBUG_THREAD)
592 TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb);
594 pthread_mutex_unlock(&cb_list_lock);
596 return TDM_ERROR_NONE;
600 tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
602 tdm_private_thread_cb *cb;
603 struct list_head *list;
605 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
606 TDM_RETURN_IF_FAIL(object != NULL);
607 TDM_RETURN_IF_FAIL(cb_type > 0);
608 TDM_RETURN_IF_FAIL(func != NULL);
610 pthread_mutex_lock(&cb_list_lock);
613 cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
615 _tdm_thread_free_cb(cb);
618 cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
620 _tdm_thread_free_cb(cb);
622 pthread_mutex_unlock(&cb_list_lock);
625 /* when call a callback, we check both cb_base's type and cb_base's data,
626 * because a callback is added with cb_type and cb_data.
629 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base, unsigned int propagation)
631 tdm_private_display *private_display = tdm_display_get();
632 tdm_private_thread_cb *cb = NULL, *hh = NULL;
633 int handler_in_other_thread = 0;
635 struct list_head *list, *other_list;
636 struct list_head call_list;
637 static pid_t waiting_tid = 0;
640 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
641 TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
642 TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
643 TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER);
644 TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
646 caller_tid = syscall(SYS_gettid);
648 assert(find_funcs[cb_base->type] != NULL);
650 if (tdm_debug_module & TDM_DEBUG_THREAD)
651 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------",
652 tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
654 /* handling only output-status as sync. below logic can't handle two sync-type events */
655 if (cb_base->type == TDM_THREAD_CB_OUTPUT_STATUS)
656 assert(cb_base->sync == 1);
658 assert(cb_base->sync == 0);
661 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
663 TDM_WRN("%p gone", object);
664 return TDM_ERROR_NONE;
668 pthread_mutex_lock(&cb_list_lock);
670 if (tdm_thread_in_display_thread(caller_tid)) {
672 other_list = &cb_list[1];
674 other_list = &cb_list[0];
678 LIST_INITHEAD(&call_list);
680 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
681 if (cb->object != object ||
682 cb->cb_type != cb_base->type ||
683 cb->cb_data != cb_base->data)
686 LIST_ADDTAIL(&cb->call_link, &call_list);
689 if (!LIST_IS_EMPTY(&call_list)) {
690 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
691 LIST_DELINIT(&cb->call_link);
692 if (tdm_debug_module & TDM_DEBUG_THREAD)
693 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
694 pthread_mutex_unlock(&cb_list_lock);
695 cb->func(private_display, cb->object, cb_base, cb->user_data);
696 pthread_mutex_lock(&cb_list_lock);
700 pthread_mutex_unlock(&cb_list_lock);
703 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
704 if (cb->object != object ||
705 cb->cb_type != cb_base->type ||
706 cb->cb_data != cb_base->data)
709 handler_in_other_thread = 1;
714 if (!handler_in_other_thread) {
715 if (keep_private_thread) {
716 if (cb_base->sync && waiting_tid != 0) {
718 pthread_cond_signal(&keep_private_thread->event_cond);
719 if (tdm_debug_module & TDM_DEBUG_THREAD)
720 TDM_INFO("pthread broadcase");
723 if (tdm_debug_module & TDM_DEBUG_THREAD)
724 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
725 tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
726 return TDM_ERROR_NONE;
729 /* Once we reach here, it means that keep_private_thread is not NULL.
730 * Just make the crash. Avoiding it is not going to help us.
732 assert(keep_private_thread != NULL);
734 ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
735 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
737 /* waiting until all cb are done in another thread */
739 /* if waiting_tid is not 0, it means there are two sync-type events at the same time.
740 * and it would make deadlock issue.
742 assert(waiting_tid == 0);
744 if (tdm_debug_module & TDM_DEBUG_THREAD)
745 TDM_INFO("pthread wait");
747 /* pthread_cond_wait atomically release mutex, Upon successful return,
748 * the mutex shall have been locked and shall be owned by the calling thread
750 tdm_mutex_locked = 0;
751 waiting_tid = caller_tid;
752 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
753 tdm_mutex_locked = 1;
756 if (tdm_debug_module & TDM_DEBUG_THREAD)
757 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
758 tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
760 return TDM_ERROR_NONE;