Revert "output: seperate output-status and output-dpms change"
[platform/core/uifw/libtdm.git] / src / tdm_thread.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm_private.h"
41
42 static tdm_private_thread *keep_private_thread;
43
44 struct _tdm_private_thread {
45         tdm_private_loop *private_loop;
46
47         pthread_cond_t event_cond;
48         pthread_t event_thread;
49
50         pid_t display_tid;
51         pid_t thread_tid;
52
53         /* 0: read, 1: write (tdm-thread -> display-thread) */
54         int pipe[2];
55
56         /* 0: read, 1: write (tdm-thread <- display-thread) */
57         int sub_pipe[2];
58         tdm_event_loop_source *sub_event_source;
59 };
60
61 typedef struct _tdm_private_thread_cb {
62         struct list_head link;
63         struct list_head call_link;
64
65         void *object;
66         tdm_thread_cb_type cb_type;
67         void *cb_data;
68         tdm_thread_cb func;
69         void *user_data;
70
71         pid_t owner_tid;
72 } tdm_private_thread_cb;
73
74 static tdm_thread_find_object find_funcs[TDM_THREAD_CB_MAX] = {0, };
75
76 /* 0: for display thread, 1: for tdm thread */
77 static struct list_head cb_list[2];
78 static pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
79
80 static void _tdm_thread_free_cb(tdm_private_thread_cb *cb);
81
82 static tdm_error
83 _tdm_thread_handle_events(int fd, tdm_event_loop_mask mask, void *user_data)
84 {
85         tdm_private_loop *private_loop = user_data;
86         tdm_error ret;
87
88         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
89         TDM_RETURN_VAL_IF_FAIL(private_loop != NULL, TDM_ERROR_OPERATION_FAILED);
90         TDM_RETURN_VAL_IF_FAIL(private_loop->dpy != NULL, TDM_ERROR_OPERATION_FAILED);
91
92         ret = tdm_thread_handle_cb(private_loop);
93
94         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret);
95
96         return TDM_ERROR_NONE;
97 }
98
99 static void*
100 _tdm_thread_main(void *data)
101 {
102         tdm_private_thread *private_thread = (tdm_private_thread*)data;
103         tdm_private_loop *private_loop = private_thread->private_loop;
104         int fd;
105         struct pollfd fds;
106         int ret;
107         tdm_error error;
108
109         /* Not lock/unlock for the private_thread and private_loop structure
110          * because they won't be destroyed as long as tdm thread is running.
111          * When they're destroyed, we have already exit the tdm thread.
112          */
113         private_thread->thread_tid = syscall(SYS_gettid);
114
115         TDM_INFO("display_tid:%d, thread_tid: %d",
116                          private_thread->display_tid, private_thread->thread_tid);
117
118         /* fd SHOULD be the same with sub_pipe[0] to make sure that using
119          * tdm_thread_get_fd, tdm_thread_send_cb, tdm_thread_handle_cb in
120          * both threads is fine. Otherwise, assert.
121          */
122         tdm_display_lock(private_loop->dpy);
123         assert(tdm_thread_get_fd(private_loop) == private_thread->sub_pipe[0]);
124
125         private_thread->sub_event_source =
126                 tdm_event_loop_add_fd_handler(private_loop->dpy,
127                                                                           private_thread->sub_pipe[0],
128                                                                           TDM_EVENT_LOOP_READABLE,
129                                                                           _tdm_thread_handle_events,
130                                                                           private_loop,
131                                                                           &error);
132         TDM_GOTO_IF_FAIL(error == TDM_ERROR_NONE, exit_thread);
133         TDM_GOTO_IF_FAIL(private_thread->sub_event_source != NULL, exit_thread);
134
135         pthread_cond_signal(&private_thread->event_cond);
136
137         /* mutex shall be locked by the thread calling pthread_cond_signal() */
138         tdm_display_unlock(private_loop->dpy);
139
140         fd = tdm_event_loop_get_fd(private_loop->dpy);
141         if (fd < 0) {
142                 TDM_ERR("couldn't get fd");
143                 goto exit_thread;
144         }
145
146         fds.events = POLLIN;
147         fds.fd = fd;
148         fds.revents = 0;
149
150         while (1) {
151                 if (tdm_debug_module & TDM_DEBUG_EVENT)
152                         TDM_INFO("server flush");
153                 tdm_event_loop_flush(private_loop->dpy);
154
155                 if (tdm_debug_module & TDM_DEBUG_EVENT)
156                         TDM_INFO("fd(%d) polling in", fd);
157
158                 ret = poll(&fds, 1, -1);
159
160                 if (tdm_debug_module & TDM_DEBUG_EVENT)
161                         TDM_INFO("fd(%d) polling out", fd);
162
163                 if (ret < 0) {
164                         if (errno == EINTR || errno == EAGAIN)  /* normal case */
165                                 continue;
166                         else {
167                                 TDM_ERR("poll failed: %m");
168                                 goto exit_thread;
169                         }
170                 }
171
172                 if (tdm_debug_module & TDM_DEBUG_EVENT)
173                         TDM_INFO("thread got events");
174
175                 if (tdm_event_loop_dispatch(private_loop->dpy) < 0)
176                         TDM_ERR("dispatch error");
177         }
178
179 exit_thread:
180         tdm_display_unlock(private_loop->dpy);
181         pthread_exit(NULL);
182 }
183
184 /* NOTE: tdm thread doesn't care about multi-thread. */
185 INTERN tdm_error
186 tdm_thread_init(tdm_private_loop *private_loop)
187 {
188         tdm_private_display *private_display;
189         tdm_private_thread *private_thread;
190         int thread, i;
191
192         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
193         TDM_RETURN_VAL_IF_FAIL(private_loop->dpy, TDM_ERROR_OPERATION_FAILED);
194
195         private_display = private_loop->dpy;
196         TDM_RETURN_VAL_IF_FAIL(private_display->private_loop, TDM_ERROR_OPERATION_FAILED);
197
198         if (private_loop->private_thread)
199                 return TDM_ERROR_NONE;
200
201         for (i = 0; i < TDM_THREAD_CB_MAX; i++)
202                 find_funcs[i] = NULL;
203
204         LIST_INITHEAD(&cb_list[0]);
205         LIST_INITHEAD(&cb_list[1]);
206
207         /* enable as default */
208         thread = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1);
209         if (!thread) {
210                 TDM_INFO("not using a TDM event thread");
211                 return TDM_ERROR_NONE;
212         }
213
214         private_thread = calloc(1, sizeof * private_thread);
215         if (!private_thread) {
216                 TDM_ERR("alloc failed");
217                 return TDM_ERROR_OUT_OF_MEMORY;
218         }
219
220         if (pthread_cond_init(&private_thread->event_cond, NULL)) {
221                 TDM_ERR("pthread_cond_init failed: %m");
222                 free(private_thread);
223                 return TDM_ERROR_OUT_OF_MEMORY;
224         }
225
226         if (pipe(private_thread->pipe) != 0) {
227                 TDM_ERR("pipe failed: %m");
228                 pthread_cond_destroy(&private_thread->event_cond);
229                 free(private_thread);
230                 return TDM_ERROR_OPERATION_FAILED;
231         }
232
233         if (pipe(private_thread->sub_pipe) != 0) {
234                 TDM_ERR("sub_pipe failed: %m");
235                 close(private_thread->pipe[0]);
236                 close(private_thread->pipe[1]);
237                 pthread_cond_destroy(&private_thread->event_cond);
238                 free(private_thread);
239                 return TDM_ERROR_OPERATION_FAILED;
240         }
241
242         keep_private_thread = private_thread;
243
244         private_thread->private_loop = private_loop;
245         private_loop->private_thread = private_thread;
246
247         private_thread->display_tid = syscall(SYS_gettid);
248
249         /* pthread_cond_wait atomically release mutex, Upon successful return,
250          * the mutex shall have been locked and shall be owned by the calling thread
251          */
252         tdm_mutex_locked = 0;
253         pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
254                                    private_thread);
255
256         /* wait until the tdm thread starts */
257         pthread_cond_wait(&private_thread->event_cond, &private_display->lock);
258         tdm_mutex_locked = 1;
259
260         TDM_INFO("using a TDM event thread. pipe(%d,%d) sub_pipe(%d,%d)",
261                          private_thread->pipe[0], private_thread->pipe[1],
262                          private_thread->sub_pipe[0], private_thread->sub_pipe[1]);
263
264         return TDM_ERROR_NONE;
265 }
266
267 INTERN void
268 tdm_thread_deinit(tdm_private_loop *private_loop)
269 {
270         tdm_private_display *private_display;
271         tdm_private_thread_cb *cb = NULL, *hh = NULL;
272         int i;
273
274         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
275
276         for (i = 0; i < TDM_THREAD_CB_MAX; i++)
277                 find_funcs[i] = NULL;
278
279         if (!private_loop->private_thread)
280                 return;
281
282         if (private_loop->private_thread->sub_event_source)
283                 tdm_event_loop_source_remove(private_loop->private_thread->sub_event_source);
284
285         pthread_cancel(private_loop->private_thread->event_thread);
286
287         private_display = private_loop->dpy;
288
289         /* before falling into the block of pthread_join, we have to unlock the mutex
290          * for subthread to use the mutex.
291          */
292         _pthread_mutex_unlock(&private_display->lock);
293         pthread_join(private_loop->private_thread->event_thread, NULL);
294
295         pthread_mutex_trylock(&cb_list_lock);
296         pthread_mutex_unlock(&cb_list_lock);
297         tdm_log_reset();
298
299         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[0], link) {
300                 _tdm_thread_free_cb(cb);
301         }
302         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[1], link) {
303                 _tdm_thread_free_cb(cb);
304         }
305
306         if (private_loop->private_thread->pipe[0] >= 0)
307                 close(private_loop->private_thread->pipe[0]);
308         if (private_loop->private_thread->pipe[1] >= 0)
309                 close(private_loop->private_thread->pipe[1]);
310
311         if (private_loop->private_thread->sub_pipe[0] >= 0)
312                 close(private_loop->private_thread->sub_pipe[0]);
313         if (private_loop->private_thread->sub_pipe[1] >= 0)
314                 close(private_loop->private_thread->sub_pipe[1]);
315
316         pthread_cond_destroy(&private_loop->private_thread->event_cond);
317
318         free(private_loop->private_thread);
319         private_loop->private_thread = NULL;
320         keep_private_thread = NULL;
321
322         TDM_INFO("Finish a TDM event thread");
323 }
324
325 INTERN int
326 tdm_thread_get_fd(tdm_private_loop *private_loop)
327 {
328         tdm_private_thread *private_thread;
329         int in_main;
330
331         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
332         TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
333         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
334
335         private_thread = private_loop->private_thread;
336
337         /* seems like ticky. but easy way to use the same APIs for both threads */
338         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
339
340         if (in_main)
341                 return private_thread->pipe[0];
342         else
343                 return private_thread->sub_pipe[0];
344 }
345
346 INTERN tdm_error
347 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
348 {
349         tdm_private_thread *private_thread;
350         ssize_t len;
351         int pipe, in_main;
352
353         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
354         TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
355         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
356         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
357
358         private_thread = private_loop->private_thread;
359
360         /* seems like ticky. but easy way to use the same APIs for both threads */
361         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
362
363         if (in_main)
364                 pipe = private_thread->sub_pipe[1];
365         else
366                 pipe = private_thread->pipe[1];
367
368         if (tdm_debug_module & TDM_DEBUG_THREAD)
369                 TDM_INFO("fd(%d) type(%s), length(%d)", pipe, tdm_cb_type_str(base->type), base->length);
370
371         len = write(pipe, base, base->length);
372         if (len != base->length) {
373                 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
374                 return TDM_ERROR_OPERATION_FAILED;
375         }
376
377         if (tdm_debug_module & TDM_DEBUG_THREAD)
378                 TDM_INFO("[%s] write fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
379
380         return TDM_ERROR_NONE;
381 }
382
383 INTERN tdm_error
384 tdm_thread_handle_cb(tdm_private_loop *private_loop)
385 {
386         tdm_private_thread *private_thread;
387         tdm_thread_cb_base *base;
388         char buffer[1024];
389         unsigned int i;
390         int len, pipe, in_main;
391         tdm_error ret = TDM_ERROR_NONE;
392
393         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
394         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
395         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
396
397         private_thread = private_loop->private_thread;
398
399         /* seems like ticky. but easy way to use the same APIs for both threads */
400         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
401
402         if (in_main)
403                 pipe = private_thread->pipe[0];
404         else
405                 pipe = private_thread->sub_pipe[0];
406
407         do {
408                 len = read(pipe, buffer, sizeof buffer);
409         } while (len < 0 && errno == EINTR);
410
411         if (tdm_debug_module & TDM_DEBUG_THREAD)
412                 TDM_INFO("[%s] read fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
413
414         if (len < 0) {
415                 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
416                 return TDM_ERROR_OPERATION_FAILED;
417         }
418
419         if (len == 0)
420                 return TDM_ERROR_NONE;
421
422         if (len < sizeof * base) {
423                 TDM_ERR("read failed: len(%d)", len);
424                 return TDM_ERROR_OPERATION_FAILED;
425         }
426
427         i = 0;
428         while (i < len) {
429                 base = (tdm_thread_cb_base*)&buffer[i];
430                 if (tdm_debug_module & TDM_DEBUG_THREAD)
431                         TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length);
432                 switch (base->type) {
433                 case TDM_THREAD_CB_OUTPUT_COMMIT:
434                 case TDM_THREAD_CB_OUTPUT_VBLANK:
435                 case TDM_THREAD_CB_OUTPUT_CHANGE:
436                 case TDM_THREAD_CB_PP_DONE:
437                 case TDM_THREAD_CB_CAPTURE_DONE:
438                 case TDM_THREAD_CB_VBLANK_SW:
439                 case TDM_THREAD_CB_VBLANK_CREATE:
440                 case TDM_THREAD_CB_NEED_VALIDATE:
441                         ret = tdm_thread_cb_call(NULL, base);
442                         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
443                         break;
444                 default:
445                         break;
446                 }
447                 i += base->length;
448         }
449
450         return TDM_ERROR_NONE;
451 }
452
453 INTERN int
454 tdm_thread_in_display_thread(pid_t tid)
455 {
456         if (!keep_private_thread)
457                 return 1;
458
459         /* DON'T check TDM_MUTEX_IS_LOCKED here */
460
461         return (keep_private_thread->display_tid == tid) ? 1 : 0;
462 }
463
464 INTERN int
465 tdm_thread_is_running(void)
466 {
467         /* DON'T check TDM_MUTEX_IS_LOCKED here */
468
469         return (keep_private_thread) ? 1 : 0;
470 }
471
472 static void
473 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
474 {
475         if (tdm_debug_module & TDM_DEBUG_THREAD)
476                 TDM_INFO("cb(%p) removed", cb);
477
478         assert(LIST_IS_EMPTY(&cb->call_link));
479
480         LIST_DEL(&cb->link);
481         free(cb);
482 }
483
484 static tdm_private_thread_cb *
485 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
486                                         void *cb_data, tdm_thread_cb func, void *user_data)
487 {
488         tdm_private_thread_cb *cb = NULL;
489
490         LIST_FOR_EACH_ENTRY(cb, list, link) {
491                 if (cb->object == object &&
492                         cb->cb_type == cb_type &&
493                         cb->cb_data == cb_data &&
494                         cb->func == func &&
495                         cb->user_data == user_data)
496                         return cb;
497         }
498
499         return NULL;
500 }
501
502 INTERN void
503 tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func)
504 {
505         TDM_RETURN_IF_FAIL(cb_type > 0);
506
507         if (func && find_funcs[cb_type])
508                 TDM_NEVER_GET_HERE();
509
510         find_funcs[cb_type] = func;
511 }
512
513 INTERN tdm_error
514 tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
515 {
516         tdm_private_thread_cb *cb = NULL;
517         pid_t caller_tid;
518         struct list_head *list;
519
520         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
521         TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER);
522         TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER);
523         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
524
525         caller_tid = syscall(SYS_gettid);
526
527         pthread_mutex_lock(&cb_list_lock);
528
529         if (tdm_thread_in_display_thread(caller_tid))
530                 list = &cb_list[0];
531         else
532                 list = &cb_list[1];
533
534         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
535         if (cb) {
536                 pthread_mutex_unlock(&cb_list_lock);
537                 TDM_ERR("can't be added twice with same data");
538                 return TDM_ERROR_BAD_REQUEST;
539         }
540
541         cb = calloc(1, sizeof *cb);
542         if (!cb) {
543                 pthread_mutex_unlock(&cb_list_lock);
544                 TDM_ERR("calloc failed");
545                 return TDM_ERROR_OUT_OF_MEMORY;
546         }
547
548         LIST_ADDTAIL(&cb->link, list);
549         LIST_INITHEAD(&cb->call_link);
550
551         cb->object = object;
552         cb->cb_type = cb_type;
553         cb->cb_data = cb_data;
554         cb->func = func;
555         cb->user_data = user_data;
556         cb->owner_tid = caller_tid;
557
558         if (tdm_debug_module & TDM_DEBUG_THREAD)
559                 TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb);
560
561         pthread_mutex_unlock(&cb_list_lock);
562
563         return TDM_ERROR_NONE;
564 }
565
566 INTERN void
567 tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
568 {
569         tdm_private_thread_cb *cb;
570         struct list_head *list;
571
572         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
573         TDM_RETURN_IF_FAIL(object != NULL);
574         TDM_RETURN_IF_FAIL(cb_type > 0);
575         TDM_RETURN_IF_FAIL(func != NULL);
576
577         pthread_mutex_lock(&cb_list_lock);
578
579         list = &cb_list[0];
580         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
581         if (cb)
582                 _tdm_thread_free_cb(cb);
583
584         list = &cb_list[1];
585         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
586         if (cb)
587                 _tdm_thread_free_cb(cb);
588
589         pthread_mutex_unlock(&cb_list_lock);
590 }
591
592 /* when call a callback, we check both cb_base's type and cb_base's data,
593  * because a callback is added with cb_type and cb_data.
594  */
595 INTERN tdm_error
596 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base)
597 {
598         tdm_private_display *private_display = tdm_display_get();
599         tdm_private_thread_cb *cb = NULL, *hh = NULL;
600         int handler_in_other_thread = 0;
601         pid_t caller_tid;
602         struct list_head *list, *other_list;
603         struct list_head call_list;
604         static pid_t waiting_tid = 0;
605         static tdm_thread_cb_type waiting_cb_type = TDM_THREAD_CB_NONE;
606         tdm_error ret;
607
608         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
609         TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
610         TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
611         TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER);
612         TDM_RETURN_VAL_IF_FAIL(cb_base->sync == 0 || cb_base->sync == 1, TDM_ERROR_INVALID_PARAMETER);
613         TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
614
615         caller_tid = syscall(SYS_gettid);
616
617         assert(find_funcs[cb_base->type] != NULL);
618
619         /* When there is the previous waiting sync call, the other type sync call CAN't be handlered */
620         if (waiting_tid > 0 && cb_base->sync == 1)
621                 assert(waiting_cb_type == cb_base->type);
622
623         if (!object) {
624                 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
625                 if (!object) {
626                         TDM_WRN("%p gone", object);
627                         return TDM_ERROR_NONE;
628                 }
629         }
630
631         pthread_mutex_lock(&cb_list_lock);
632
633         if (tdm_thread_in_display_thread(caller_tid)) {
634                 list = &cb_list[0];
635                 other_list = &cb_list[1];
636         } else {
637                 other_list = &cb_list[0];
638                 list = &cb_list[1];
639         }
640
641         LIST_INITHEAD(&call_list);
642
643         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
644                 if (cb->object != object ||
645                         cb->cb_type != cb_base->type ||
646                         cb->cb_data != cb_base->data)
647                         continue;
648
649                 LIST_ADDTAIL(&cb->call_link, &call_list);
650         }
651
652         if (!LIST_IS_EMPTY(&call_list)) {
653                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
654                         LIST_DELINIT(&cb->call_link);
655                         if (tdm_debug_module & TDM_DEBUG_THREAD)
656                                 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
657                         pthread_mutex_unlock(&cb_list_lock);
658                         cb->func(private_display, cb->object, cb_base, cb->user_data);
659                         pthread_mutex_lock(&cb_list_lock);
660                 }
661         }
662
663         pthread_mutex_unlock(&cb_list_lock);
664
665         assert(LIST_IS_EMPTY(&call_list));
666
667         if (waiting_tid == 0 || waiting_cb_type != cb_base->type) {
668                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
669                         if (cb->object != object ||
670                                 cb->cb_type != cb_base->type ||
671                                 cb->cb_data != cb_base->data)
672                                 continue;
673
674                         handler_in_other_thread = 1;
675                         break;
676                 }
677         }
678
679         if (!handler_in_other_thread) {
680                 if (keep_private_thread) {
681                         if (cb_base->sync) {
682                                 waiting_tid = 0;
683                                 waiting_cb_type = TDM_THREAD_CB_NONE;
684                                 pthread_cond_signal(&keep_private_thread->event_cond);
685                                 if (tdm_debug_module & TDM_DEBUG_THREAD)
686                                         TDM_INFO("pthread broadcase");
687                         }
688                 }
689                 if (tdm_debug_module & TDM_DEBUG_THREAD)
690                         TDM_INFO("'%s' thread_cb done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
691                 return TDM_ERROR_NONE;
692         }
693
694         /* Once we reach here, it means that keep_private_thread is not NULL.
695          * Just make the crash. Avoiding it is not going to help us.
696          */
697         ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
698         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
699
700         /* waiting until all cb are done in another thread */
701         if (cb_base->sync) {
702                 if (tdm_debug_module & TDM_DEBUG_THREAD)
703                         TDM_INFO("pthread wait");
704
705                 /* pthread_cond_wait atomically release mutex, Upon successful return,
706                  * the mutex shall have been locked and shall be owned by the calling thread
707                  */
708                 tdm_mutex_locked = 0;
709                 waiting_tid = caller_tid;
710                 waiting_cb_type = cb_base->type;
711
712                 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
713                 tdm_mutex_locked = 1;
714         }
715
716         if (tdm_debug_module & TDM_DEBUG_THREAD)
717                 TDM_INFO("'%s' thread_cb done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
718
719         return TDM_ERROR_NONE;
720 }