vblank: calculating SW target time properly
[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         tdm_log_reset();
296
297         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[0], link) {
298                 _tdm_thread_free_cb(cb);
299         }
300         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[1], link) {
301                 _tdm_thread_free_cb(cb);
302         }
303
304         if (private_loop->private_thread->pipe[0] >= 0)
305                 close(private_loop->private_thread->pipe[0]);
306         if (private_loop->private_thread->pipe[1] >= 0)
307                 close(private_loop->private_thread->pipe[1]);
308
309         if (private_loop->private_thread->sub_pipe[0] >= 0)
310                 close(private_loop->private_thread->sub_pipe[0]);
311         if (private_loop->private_thread->sub_pipe[1] >= 0)
312                 close(private_loop->private_thread->sub_pipe[1]);
313
314         pthread_cond_destroy(&private_loop->private_thread->event_cond);
315
316         free(private_loop->private_thread);
317         private_loop->private_thread = NULL;
318         keep_private_thread = NULL;
319
320         TDM_INFO("Finish a TDM event thread");
321 }
322
323 INTERN int
324 tdm_thread_get_fd(tdm_private_loop *private_loop)
325 {
326         tdm_private_thread *private_thread;
327         int in_main;
328
329         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
330         TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
331         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
332
333         private_thread = private_loop->private_thread;
334
335         /* seems like ticky. but easy way to use the same APIs for both threads */
336         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
337
338         if (in_main)
339                 return private_thread->pipe[0];
340         else
341                 return private_thread->sub_pipe[0];
342 }
343
344 INTERN tdm_error
345 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
346 {
347         tdm_private_thread *private_thread;
348         ssize_t len;
349         int pipe, in_main;
350
351         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
352         TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
353         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
354         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
355
356         private_thread = private_loop->private_thread;
357
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));
360
361         if (in_main)
362                 pipe = private_thread->sub_pipe[1];
363         else
364                 pipe = private_thread->pipe[1];
365
366         if (tdm_debug_module & TDM_DEBUG_THREAD)
367                 TDM_INFO("fd(%d) type(%s), length(%d)", pipe, tdm_cb_type_str(base->type), base->length);
368
369         len = write(pipe, base, base->length);
370         if (len != base->length) {
371                 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
372                 return TDM_ERROR_OPERATION_FAILED;
373         }
374
375         if (tdm_debug_module & TDM_DEBUG_THREAD)
376                 TDM_INFO("[%s] write fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
377
378         return TDM_ERROR_NONE;
379 }
380
381 INTERN tdm_error
382 tdm_thread_handle_cb(tdm_private_loop *private_loop)
383 {
384         tdm_private_thread *private_thread;
385         tdm_thread_cb_base *base;
386         char buffer[1024];
387         unsigned int i;
388         int len, pipe, in_main;
389         tdm_error ret = TDM_ERROR_NONE;
390
391         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
392         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
393         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
394
395         private_thread = private_loop->private_thread;
396
397         /* seems like ticky. but easy way to use the same APIs for both threads */
398         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
399
400         if (in_main)
401                 pipe = private_thread->pipe[0];
402         else
403                 pipe = private_thread->sub_pipe[0];
404
405         do {
406                 len = read(pipe, buffer, sizeof buffer);
407         } while (len < 0 && errno == EINTR);
408
409         if (tdm_debug_module & TDM_DEBUG_THREAD)
410                 TDM_INFO("[%s] read fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
411
412         if (len < 0) {
413                 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
414                 return TDM_ERROR_OPERATION_FAILED;
415         }
416
417         if (len == 0)
418                 return TDM_ERROR_NONE;
419
420         if (len < sizeof * base) {
421                 TDM_ERR("read failed: len(%d)", len);
422                 return TDM_ERROR_OPERATION_FAILED;
423         }
424
425         i = 0;
426         while (i < len) {
427                 base = (tdm_thread_cb_base*)&buffer[i];
428                 if (tdm_debug_module & TDM_DEBUG_THREAD)
429                         TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length);
430                 switch (base->type) {
431                 case TDM_THREAD_CB_OUTPUT_COMMIT:
432                 case TDM_THREAD_CB_OUTPUT_VBLANK:
433                 case TDM_THREAD_CB_OUTPUT_STATUS:
434                 case TDM_THREAD_CB_OUTPUT_DPMS:
435                 case TDM_THREAD_CB_PP_DONE:
436                 case TDM_THREAD_CB_CAPTURE_DONE:
437                 case TDM_THREAD_CB_VBLANK_SW:
438                 case TDM_THREAD_CB_VBLANK_CREATE:
439                 case TDM_THREAD_CB_NEED_VALIDATE:
440                         ret = tdm_thread_cb_call(NULL, base);
441                         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
442                         break;
443                 default:
444                         break;
445                 }
446                 i += base->length;
447         }
448
449         return TDM_ERROR_NONE;
450 }
451
452 INTERN int
453 tdm_thread_in_display_thread(pid_t tid)
454 {
455         if (!keep_private_thread)
456                 return 1;
457
458         /* DON'T check TDM_MUTEX_IS_LOCKED here */
459
460         return (keep_private_thread->display_tid == tid) ? 1 : 0;
461 }
462
463 INTERN int
464 tdm_thread_is_running(void)
465 {
466         /* DON'T check TDM_MUTEX_IS_LOCKED here */
467
468         return (keep_private_thread) ? 1 : 0;
469 }
470
471 static void
472 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
473 {
474         if (tdm_debug_module & TDM_DEBUG_THREAD)
475                 TDM_INFO("cb(%p) removed", cb);
476
477         assert(LIST_IS_EMPTY(&cb->call_link));
478
479         LIST_DEL(&cb->link);
480         free(cb);
481 }
482
483 static tdm_private_thread_cb *
484 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
485                                         void *cb_data, tdm_thread_cb func, void *user_data, pid_t caller_tid)
486 {
487         tdm_private_thread_cb *cb = NULL;
488
489         LIST_FOR_EACH_ENTRY(cb, list, link) {
490                 if (cb->object == object &&
491                         cb->cb_type == cb_type &&
492                         cb->cb_data == cb_data &&
493                         cb->func == func &&
494                         cb->user_data == user_data &&
495                         cb->owner_tid == caller_tid)
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, caller_tid);
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         pid_t caller_tid;
571         struct list_head *list;
572
573         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
574         TDM_RETURN_IF_FAIL(object != NULL);
575         TDM_RETURN_IF_FAIL(cb_type > 0);
576         TDM_RETURN_IF_FAIL(func != NULL);
577
578         caller_tid = syscall(SYS_gettid);
579
580         pthread_mutex_lock(&cb_list_lock);
581
582         if (tdm_thread_in_display_thread(caller_tid))
583                 list = &cb_list[0];
584         else
585                 list = &cb_list[1];
586
587         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data, caller_tid);
588         if (!cb) {
589                 pthread_mutex_unlock(&cb_list_lock);
590                 return;
591         }
592
593         _tdm_thread_free_cb(cb);
594         pthread_mutex_unlock(&cb_list_lock);
595 }
596
597 /* when call a callback, we check both cb_base's type and cb_base's data,
598  * because a callback is added with cb_type and cb_data.
599  */
600 INTERN tdm_error
601 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base)
602 {
603         tdm_private_display *private_display = tdm_display_get();
604         tdm_private_thread_cb *cb = NULL, *hh = NULL;
605         int handler_in_other_thread = 0;
606         pid_t caller_tid;
607         struct list_head *list, *other_list;
608         struct list_head call_list;
609         static pid_t waiting_tid = 0;
610         static tdm_thread_cb_type waiting_cb_type = TDM_THREAD_CB_NONE;
611         tdm_error ret;
612
613         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
614         TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
615         TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
616         TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER);
617         TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
618
619         caller_tid = syscall(SYS_gettid);
620
621         assert(find_funcs[cb_base->type] != NULL);
622
623         /* handling only output-status as sync */
624         if (cb_base->type == TDM_THREAD_CB_OUTPUT_STATUS) {
625                 TDM_RETURN_VAL_IF_FAIL(cb_base->sync == 1, TDM_ERROR_INVALID_PARAMETER);
626         } else {
627                 TDM_RETURN_VAL_IF_FAIL(cb_base->sync == 0, TDM_ERROR_INVALID_PARAMETER);
628         }
629
630         if (!object) {
631                 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
632                 if (!object) {
633                         TDM_WRN("%p gone", object);
634                         return TDM_ERROR_NONE;
635                 }
636         }
637
638         pthread_mutex_lock(&cb_list_lock);
639
640         if (tdm_thread_in_display_thread(caller_tid)) {
641                 list = &cb_list[0];
642                 other_list = &cb_list[1];
643         } else {
644                 other_list = &cb_list[0];
645                 list = &cb_list[1];
646         }
647
648         LIST_INITHEAD(&call_list);
649
650         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
651                 if (cb->object != object ||
652                         cb->cb_type != cb_base->type ||
653                         cb->cb_data != cb_base->data)
654                         continue;
655
656                 LIST_ADDTAIL(&cb->call_link, &call_list);
657         }
658
659         if (!LIST_IS_EMPTY(&call_list)) {
660                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
661                         LIST_DELINIT(&cb->call_link);
662                         if (tdm_debug_module & TDM_DEBUG_THREAD)
663                                 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
664                         pthread_mutex_unlock(&cb_list_lock);
665                         cb->func(private_display, cb->object, cb_base, cb->user_data);
666                         pthread_mutex_lock(&cb_list_lock);
667                 }
668         }
669
670         pthread_mutex_unlock(&cb_list_lock);
671
672         assert(LIST_IS_EMPTY(&call_list));
673
674         if (waiting_tid == 0 || waiting_cb_type != cb_base->type) {
675                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
676                         if (cb->object != object ||
677                                 cb->cb_type != cb_base->type ||
678                                 cb->cb_data != cb_base->data)
679                                 continue;
680
681                         handler_in_other_thread = 1;
682                         break;
683                 }
684         }
685
686         if (!handler_in_other_thread) {
687                 if (keep_private_thread) {
688                         if (cb_base->sync) {
689                                 waiting_tid = 0;
690                                 waiting_cb_type = TDM_THREAD_CB_NONE;
691                                 pthread_cond_signal(&keep_private_thread->event_cond);
692                                 if (tdm_debug_module & TDM_DEBUG_THREAD)
693                                         TDM_INFO("pthread broadcase");
694                         }
695                 }
696                 if (tdm_debug_module & TDM_DEBUG_THREAD)
697                         TDM_INFO("'%s' thread_cb done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
698                 return TDM_ERROR_NONE;
699         }
700
701         /* Once we reach here, it means that keep_private_thread is not NULL.
702          * Just make the crash. Avoiding it is not going to help us.
703          */
704         ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
705         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
706
707         /* waiting until all cb are done in another thread */
708         if (cb_base->sync) {
709                 if (tdm_debug_module & TDM_DEBUG_THREAD)
710                         TDM_INFO("pthread wait");
711
712                 /* pthread_cond_wait atomically release mutex, Upon successful return,
713                  * the mutex shall have been locked and shall be owned by the calling thread
714                  */
715                 tdm_mutex_locked = 0;
716                 waiting_tid = caller_tid;
717                 waiting_cb_type = cb_base->type;
718
719                 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
720                 tdm_mutex_locked = 1;
721         }
722
723         if (tdm_debug_module & TDM_DEBUG_THREAD)
724                 TDM_INFO("'%s' thread_cb done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
725
726         return TDM_ERROR_NONE;
727 }