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;
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         if (pthread_mutex_init(&cb_list_lock, NULL)) {
205                 TDM_ERR("mutex init failed: %m");
206                 return TDM_ERROR_OUT_OF_MEMORY;
207         }
208
209         LIST_INITHEAD(&cb_list[0]);
210         LIST_INITHEAD(&cb_list[1]);
211
212         /* enable as default */
213         thread = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1);
214         if (!thread) {
215                 TDM_INFO("not using a TDM event thread");
216                 return TDM_ERROR_NONE;
217         }
218
219         private_thread = calloc(1, sizeof * private_thread);
220         if (!private_thread) {
221                 TDM_ERR("alloc failed");
222                 return TDM_ERROR_OUT_OF_MEMORY;
223         }
224
225         if (pthread_cond_init(&private_thread->event_cond, NULL)) {
226                 TDM_ERR("pthread_cond_init failed: %m");
227                 free(private_thread);
228                 return TDM_ERROR_OUT_OF_MEMORY;
229         }
230
231         if (pipe(private_thread->pipe) != 0) {
232                 TDM_ERR("pipe failed: %m");
233                 pthread_cond_destroy(&private_thread->event_cond);
234                 free(private_thread);
235                 return TDM_ERROR_OPERATION_FAILED;
236         }
237
238         if (pipe(private_thread->sub_pipe) != 0) {
239                 TDM_ERR("sub_pipe failed: %m");
240                 close(private_thread->pipe[0]);
241                 close(private_thread->pipe[1]);
242                 pthread_cond_destroy(&private_thread->event_cond);
243                 free(private_thread);
244                 return TDM_ERROR_OPERATION_FAILED;
245         }
246
247         keep_private_thread = private_thread;
248
249         private_thread->private_loop = private_loop;
250         private_loop->private_thread = private_thread;
251
252         private_thread->display_tid = syscall(SYS_gettid);
253
254         /* pthread_cond_wait atomically release mutex, Upon successful return,
255          * the mutex shall have been locked and shall be owned by the calling thread
256          */
257         tdm_mutex_locked = 0;
258         pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
259                                    private_thread);
260
261         /* wait until the tdm thread starts */
262         pthread_cond_wait(&private_thread->event_cond, &private_display->lock);
263         tdm_mutex_locked = 1;
264
265         TDM_INFO("using a TDM event thread. pipe(%d,%d) sub_pipe(%d,%d)",
266                          private_thread->pipe[0], private_thread->pipe[1],
267                          private_thread->sub_pipe[0], private_thread->sub_pipe[1]);
268
269         return TDM_ERROR_NONE;
270 }
271
272 INTERN void
273 tdm_thread_deinit(tdm_private_loop *private_loop)
274 {
275         tdm_private_display *private_display;
276         tdm_private_thread_cb *cb = NULL, *hh = NULL;
277         int i;
278
279         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
280
281         for (i = 0; i < TDM_THREAD_CB_MAX; i++)
282                 find_funcs[i] = NULL;
283
284         if (!private_loop->private_thread)
285                 return;
286
287         if (private_loop->private_thread->sub_event_source)
288                 tdm_event_loop_source_remove(private_loop->private_thread->sub_event_source);
289
290         pthread_cancel(private_loop->private_thread->event_thread);
291
292         private_display = private_loop->dpy;
293
294         /* before falling into the block of pthread_join, we have to unlock the mutex
295          * for subthread to use the mutex.
296          */
297         _pthread_mutex_unlock(&private_display->lock);
298         pthread_join(private_loop->private_thread->event_thread, NULL);
299
300         tdm_log_reset();
301
302         pthread_mutex_destroy(&cb_list_lock);
303
304         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[0], link) {
305                 _tdm_thread_free_cb(cb);
306         }
307         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[1], link) {
308                 _tdm_thread_free_cb(cb);
309         }
310
311         if (private_loop->private_thread->pipe[0] >= 0)
312                 close(private_loop->private_thread->pipe[0]);
313         if (private_loop->private_thread->pipe[1] >= 0)
314                 close(private_loop->private_thread->pipe[1]);
315
316         if (private_loop->private_thread->sub_pipe[0] >= 0)
317                 close(private_loop->private_thread->sub_pipe[0]);
318         if (private_loop->private_thread->sub_pipe[1] >= 0)
319                 close(private_loop->private_thread->sub_pipe[1]);
320
321         pthread_cond_destroy(&private_loop->private_thread->event_cond);
322
323         free(private_loop->private_thread);
324         private_loop->private_thread = NULL;
325         keep_private_thread = NULL;
326
327         TDM_INFO("Finish a TDM event thread");
328 }
329
330 INTERN int
331 tdm_thread_get_fd(tdm_private_loop *private_loop)
332 {
333         tdm_private_thread *private_thread;
334         int in_main;
335
336         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
337         TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
338         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
339
340         private_thread = private_loop->private_thread;
341
342         /* seems like ticky. but easy way to use the same APIs for both threads */
343         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
344
345         if (in_main)
346                 return private_thread->pipe[0];
347         else
348                 return private_thread->sub_pipe[0];
349 }
350
351 INTERN tdm_error
352 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
353 {
354         tdm_private_thread *private_thread;
355         ssize_t len;
356         int pipe, in_main;
357
358         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
359         TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
360         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
361         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
362
363         private_thread = private_loop->private_thread;
364
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));
367
368         if (in_main)
369                 pipe = private_thread->sub_pipe[1];
370         else
371                 pipe = private_thread->pipe[1];
372
373         if (tdm_debug_module & TDM_DEBUG_THREAD)
374                 TDM_INFO("fd(%d) type(%s), length(%d)", pipe, tdm_cb_type_str(base->type), base->length);
375
376         len = write(pipe, base, base->length);
377         if (len != base->length) {
378                 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
379                 return TDM_ERROR_OPERATION_FAILED;
380         }
381
382         if (tdm_debug_module & TDM_DEBUG_THREAD)
383                 TDM_INFO("[%s] write fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
384
385         return TDM_ERROR_NONE;
386 }
387
388 INTERN tdm_error
389 tdm_thread_handle_cb(tdm_private_loop *private_loop)
390 {
391         tdm_private_thread *private_thread;
392         tdm_thread_cb_base *base;
393         char buffer[1024];
394         unsigned int i;
395         int len, pipe, in_main;
396         tdm_error ret = TDM_ERROR_NONE;
397
398         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
399         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
400         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
401
402         private_thread = private_loop->private_thread;
403
404         /* seems like ticky. but easy way to use the same APIs for both threads */
405         in_main = tdm_thread_in_display_thread(syscall(SYS_gettid));
406
407         if (in_main)
408                 pipe = private_thread->pipe[0];
409         else
410                 pipe = private_thread->sub_pipe[0];
411
412         do {
413                 len = read(pipe, buffer, sizeof buffer);
414         } while (len < 0 && errno == EINTR);
415
416         if (tdm_debug_module & TDM_DEBUG_THREAD)
417                 TDM_INFO("[%s] read fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len);
418
419         if (len < 0) {
420                 TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len);
421                 return TDM_ERROR_OPERATION_FAILED;
422         }
423
424         if (len == 0)
425                 return TDM_ERROR_NONE;
426
427         if (len < sizeof * base) {
428                 TDM_ERR("read failed: len(%d)", len);
429                 return TDM_ERROR_OPERATION_FAILED;
430         }
431
432         i = 0;
433         while (i < len) {
434                 base = (tdm_thread_cb_base*)&buffer[i];
435                 if (tdm_debug_module & TDM_DEBUG_THREAD)
436                         TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length);
437                 switch (base->type) {
438                 case TDM_THREAD_CB_OUTPUT_COMMIT:
439                 case TDM_THREAD_CB_OUTPUT_VBLANK:
440                 case TDM_THREAD_CB_OUTPUT_STATUS:
441                 case TDM_THREAD_CB_OUTPUT_DPMS:
442                 case TDM_THREAD_CB_PP_DONE:
443                 case TDM_THREAD_CB_CAPTURE_DONE:
444                 case TDM_THREAD_CB_VBLANK_SW:
445                 case TDM_THREAD_CB_VBLANK_CREATE:
446                 case TDM_THREAD_CB_NEED_VALIDATE:
447                         ret = tdm_thread_cb_call(NULL, base);
448                         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
449                         break;
450                 default:
451                         break;
452                 }
453                 i += base->length;
454         }
455
456         return TDM_ERROR_NONE;
457 }
458
459 INTERN int
460 tdm_thread_in_display_thread(pid_t tid)
461 {
462         if (!keep_private_thread)
463                 return 1;
464
465         /* DON'T check TDM_MUTEX_IS_LOCKED here */
466
467         return (keep_private_thread->display_tid == tid) ? 1 : 0;
468 }
469
470 INTERN int
471 tdm_thread_is_running(void)
472 {
473         /* DON'T check TDM_MUTEX_IS_LOCKED here */
474
475         return (keep_private_thread) ? 1 : 0;
476 }
477
478 static void
479 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
480 {
481         if (tdm_debug_module & TDM_DEBUG_THREAD)
482                 TDM_INFO("cb(%p) removed", cb);
483
484         assert(LIST_IS_EMPTY(&cb->call_link));
485
486         LIST_DEL(&cb->link);
487         free(cb);
488 }
489
490 static tdm_private_thread_cb *
491 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
492                                         void *cb_data, tdm_thread_cb func, void *user_data, pid_t caller_tid)
493 {
494         tdm_private_thread_cb *cb = NULL;
495
496         LIST_FOR_EACH_ENTRY(cb, list, link) {
497                 if (cb->object == object &&
498                         cb->cb_type == cb_type &&
499                         cb->cb_data == cb_data &&
500                         cb->func == func &&
501                         cb->user_data == user_data &&
502                         cb->owner_tid == caller_tid)
503                         return cb;
504         }
505
506         return NULL;
507 }
508
509 INTERN void
510 tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func)
511 {
512         TDM_RETURN_IF_FAIL(cb_type > 0);
513
514         if (func && find_funcs[cb_type])
515                 TDM_NEVER_GET_HERE();
516
517         find_funcs[cb_type] = func;
518 }
519
520 INTERN tdm_error
521 tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
522 {
523         tdm_private_thread_cb *cb = NULL;
524         pid_t caller_tid;
525         struct list_head *list;
526
527         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
528         TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER);
529         TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER);
530         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
531
532         caller_tid = syscall(SYS_gettid);
533
534         pthread_mutex_lock(&cb_list_lock);
535
536         if (tdm_thread_in_display_thread(caller_tid))
537                 list = &cb_list[0];
538         else
539                 list = &cb_list[1];
540
541         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data, caller_tid);
542         if (cb) {
543                 pthread_mutex_unlock(&cb_list_lock);
544                 TDM_ERR("can't be added twice with same data");
545                 return TDM_ERROR_BAD_REQUEST;
546         }
547
548         cb = calloc(1, sizeof *cb);
549         if (!cb) {
550                 pthread_mutex_unlock(&cb_list_lock);
551                 TDM_ERR("calloc failed");
552                 return TDM_ERROR_OUT_OF_MEMORY;
553         }
554
555         LIST_ADDTAIL(&cb->link, list);
556         LIST_INITHEAD(&cb->call_link);
557
558         cb->object = object;
559         cb->cb_type = cb_type;
560         cb->cb_data = cb_data;
561         cb->func = func;
562         cb->user_data = user_data;
563         cb->owner_tid = caller_tid;
564
565         if (tdm_debug_module & TDM_DEBUG_THREAD)
566                 TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb);
567
568         pthread_mutex_unlock(&cb_list_lock);
569
570         return TDM_ERROR_NONE;
571 }
572
573 INTERN void
574 tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
575 {
576         tdm_private_thread_cb *cb;
577         pid_t caller_tid;
578         struct list_head *list;
579
580         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
581         TDM_RETURN_IF_FAIL(object != NULL);
582         TDM_RETURN_IF_FAIL(cb_type > 0);
583         TDM_RETURN_IF_FAIL(func != NULL);
584
585         caller_tid = syscall(SYS_gettid);
586
587         pthread_mutex_lock(&cb_list_lock);
588
589         if (tdm_thread_in_display_thread(caller_tid))
590                 list = &cb_list[0];
591         else
592                 list = &cb_list[1];
593
594         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data, caller_tid);
595         if (!cb) {
596                 pthread_mutex_unlock(&cb_list_lock);
597                 return;
598         }
599
600         _tdm_thread_free_cb(cb);
601         pthread_mutex_unlock(&cb_list_lock);
602 }
603
604 /* when call a callback, we check both cb_base's type and cb_base's data,
605  * because a callback is added with cb_type and cb_data.
606  */
607 INTERN tdm_error
608 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base)
609 {
610         tdm_private_display *private_display = tdm_display_get();
611         tdm_private_thread_cb *cb = NULL, *hh = NULL;
612         int handler_in_other_thread = 0;
613         pid_t caller_tid;
614         struct list_head *list, *other_list;
615         struct list_head call_list;
616         static pid_t waiting_tid = 0;
617         static tdm_thread_cb_type waiting_cb_type = TDM_THREAD_CB_NONE;
618         tdm_error ret;
619
620         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
621         TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
622         TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
623         TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER);
624         TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
625
626         caller_tid = syscall(SYS_gettid);
627
628         assert(find_funcs[cb_base->type] != NULL);
629
630         /* handling only output-status as sync */
631         if (cb_base->type == TDM_THREAD_CB_OUTPUT_STATUS) {
632                 TDM_RETURN_VAL_IF_FAIL(cb_base->sync == 1, TDM_ERROR_INVALID_PARAMETER);
633         } else {
634                 TDM_RETURN_VAL_IF_FAIL(cb_base->sync == 0, TDM_ERROR_INVALID_PARAMETER);
635         }
636
637         if (!object) {
638                 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
639                 if (!object) {
640                         TDM_WRN("%p gone", object);
641                         return TDM_ERROR_NONE;
642                 }
643         }
644
645         pthread_mutex_lock(&cb_list_lock);
646
647         if (tdm_thread_in_display_thread(caller_tid)) {
648                 list = &cb_list[0];
649                 other_list = &cb_list[1];
650         } else {
651                 other_list = &cb_list[0];
652                 list = &cb_list[1];
653         }
654
655         LIST_INITHEAD(&call_list);
656
657         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
658                 if (cb->object != object ||
659                         cb->cb_type != cb_base->type ||
660                         cb->cb_data != cb_base->data)
661                         continue;
662
663                 LIST_ADDTAIL(&cb->call_link, &call_list);
664         }
665
666         if (!LIST_IS_EMPTY(&call_list)) {
667                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
668                         LIST_DELINIT(&cb->call_link);
669                         if (tdm_debug_module & TDM_DEBUG_THREAD)
670                                 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
671                         pthread_mutex_unlock(&cb_list_lock);
672                         cb->func(private_display, cb->object, cb_base, cb->user_data);
673                         pthread_mutex_lock(&cb_list_lock);
674                 }
675         }
676
677         pthread_mutex_unlock(&cb_list_lock);
678
679         assert(LIST_IS_EMPTY(&call_list));
680
681         if (waiting_tid == 0 || waiting_cb_type != cb_base->type) {
682                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
683                         if (cb->object != object ||
684                                 cb->cb_type != cb_base->type ||
685                                 cb->cb_data != cb_base->data)
686                                 continue;
687
688                         handler_in_other_thread = 1;
689                         break;
690                 }
691         }
692
693         if (!handler_in_other_thread) {
694                 if (keep_private_thread) {
695                         if (cb_base->sync) {
696                                 waiting_tid = 0;
697                                 waiting_cb_type = TDM_THREAD_CB_NONE;
698                                 pthread_cond_signal(&keep_private_thread->event_cond);
699                                 if (tdm_debug_module & TDM_DEBUG_THREAD)
700                                         TDM_INFO("pthread broadcase");
701                         }
702                 }
703                 if (tdm_debug_module & TDM_DEBUG_THREAD)
704                         TDM_INFO("'%s' thread_cb done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
705                 return TDM_ERROR_NONE;
706         }
707
708         /* Once we reach here, it means that keep_private_thread is not NULL.
709          * Just make the crash. Avoiding it is not going to help us.
710          */
711         ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
712         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
713
714         /* waiting until all cb are done in another thread */
715         if (cb_base->sync) {
716                 if (tdm_debug_module & TDM_DEBUG_THREAD)
717                         TDM_INFO("pthread wait");
718
719                 /* pthread_cond_wait atomically release mutex, Upon successful return,
720                  * the mutex shall have been locked and shall be owned by the calling thread
721                  */
722                 tdm_mutex_locked = 0;
723                 waiting_tid = caller_tid;
724                 waiting_cb_type = cb_base->type;
725
726                 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
727                 tdm_mutex_locked = 1;
728         }
729
730         if (tdm_debug_module & TDM_DEBUG_THREAD)
731                 TDM_INFO("'%s' thread_cb done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
732
733         return TDM_ERROR_NONE;
734 }