helper: copy filename
[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_STATUS:
436                 case TDM_THREAD_CB_OUTPUT_DPMS:
437                 case TDM_THREAD_CB_PP_DONE:
438                 case TDM_THREAD_CB_CAPTURE_DONE:
439                 case TDM_THREAD_CB_VBLANK_SW:
440                 case TDM_THREAD_CB_VBLANK_CREATE:
441                 case TDM_THREAD_CB_NEED_VALIDATE:
442                         /* this event comes from other thread. so we don't need to propagate this to other thread */
443                         ret = tdm_thread_cb_call(NULL, base, 0);
444                         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
445                         break;
446                 default:
447                         break;
448                 }
449                 i += base->length;
450         }
451
452         return TDM_ERROR_NONE;
453 }
454
455 INTERN int
456 tdm_thread_in_display_thread(pid_t tid)
457 {
458         if (!keep_private_thread)
459                 return 1;
460
461         /* DON'T check TDM_MUTEX_IS_LOCKED here */
462
463         return (keep_private_thread->display_tid == tid) ? 1 : 0;
464 }
465
466 INTERN int
467 tdm_thread_is_running(void)
468 {
469         /* DON'T check TDM_MUTEX_IS_LOCKED here */
470
471         return (keep_private_thread) ? 1 : 0;
472 }
473
474 static void
475 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
476 {
477         if (tdm_debug_module & TDM_DEBUG_THREAD)
478                 TDM_INFO("cb(%p) removed", cb);
479
480         assert(LIST_IS_EMPTY(&cb->call_link));
481
482         LIST_DEL(&cb->link);
483         free(cb);
484 }
485
486 static tdm_private_thread_cb *
487 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
488                                         void *cb_data, tdm_thread_cb func, void *user_data)
489 {
490         tdm_private_thread_cb *cb = NULL;
491
492         LIST_FOR_EACH_ENTRY(cb, list, link) {
493                 if (cb->object == object &&
494                         cb->cb_type == cb_type &&
495                         cb->cb_data == cb_data &&
496                         cb->func == func &&
497                         cb->user_data == user_data)
498                         return cb;
499         }
500
501         return NULL;
502 }
503
504 INTERN void
505 tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func)
506 {
507         TDM_RETURN_IF_FAIL(cb_type > 0);
508
509         if (func && find_funcs[cb_type])
510                 TDM_NEVER_GET_HERE();
511
512         find_funcs[cb_type] = func;
513 }
514
515 INTERN tdm_error
516 tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
517 {
518         tdm_private_thread_cb *cb = NULL;
519         pid_t caller_tid;
520         struct list_head *list;
521
522         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
523         TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER);
524         TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER);
525         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
526
527         caller_tid = syscall(SYS_gettid);
528
529         pthread_mutex_lock(&cb_list_lock);
530
531         if (tdm_thread_in_display_thread(caller_tid))
532                 list = &cb_list[0];
533         else
534                 list = &cb_list[1];
535
536         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
537         if (cb) {
538                 pthread_mutex_unlock(&cb_list_lock);
539                 TDM_ERR("can't be added twice with same data");
540                 return TDM_ERROR_BAD_REQUEST;
541         }
542
543         cb = calloc(1, sizeof *cb);
544         if (!cb) {
545                 pthread_mutex_unlock(&cb_list_lock);
546                 TDM_ERR("calloc failed");
547                 return TDM_ERROR_OUT_OF_MEMORY;
548         }
549
550         LIST_ADDTAIL(&cb->link, list);
551         LIST_INITHEAD(&cb->call_link);
552
553         cb->object = object;
554         cb->cb_type = cb_type;
555         cb->cb_data = cb_data;
556         cb->func = func;
557         cb->user_data = user_data;
558         cb->owner_tid = caller_tid;
559
560         if (tdm_debug_module & TDM_DEBUG_THREAD)
561                 TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb);
562
563         pthread_mutex_unlock(&cb_list_lock);
564
565         return TDM_ERROR_NONE;
566 }
567
568 INTERN void
569 tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
570 {
571         tdm_private_thread_cb *cb;
572         struct list_head *list;
573
574         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
575         TDM_RETURN_IF_FAIL(object != NULL);
576         TDM_RETURN_IF_FAIL(cb_type > 0);
577         TDM_RETURN_IF_FAIL(func != NULL);
578
579         pthread_mutex_lock(&cb_list_lock);
580
581         list = &cb_list[0];
582         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
583         if (cb)
584                 _tdm_thread_free_cb(cb);
585
586         list = &cb_list[1];
587         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
588         if (cb)
589                 _tdm_thread_free_cb(cb);
590
591         pthread_mutex_unlock(&cb_list_lock);
592 }
593
594 /* when call a callback, we check both cb_base's type and cb_base's data,
595  * because a callback is added with cb_type and cb_data.
596  */
597 INTERN tdm_error
598 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base, unsigned int propagation)
599 {
600         tdm_private_display *private_display = tdm_display_get();
601         tdm_private_thread_cb *cb = NULL, *hh = NULL;
602         int handler_in_other_thread = 0;
603         pid_t caller_tid;
604         struct list_head *list, *other_list;
605         struct list_head call_list;
606         static pid_t waiting_tid = 0;
607         tdm_error ret;
608
609         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
610         TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
611         TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
612         TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, 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         if (tdm_debug_module & TDM_DEBUG_THREAD)
620                 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------",
621                                  tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
622
623         /* handling only output-status as sync. below logic can't handle two sync-type events */
624         if (cb_base->type == TDM_THREAD_CB_OUTPUT_STATUS)
625                 assert(cb_base->sync == 1);
626         else
627                 assert(cb_base->sync == 0);
628
629         if (!object) {
630                 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
631                 if (!object) {
632                         TDM_WRN("%p gone", object);
633                         return TDM_ERROR_NONE;
634                 }
635         }
636
637         pthread_mutex_lock(&cb_list_lock);
638
639         if (tdm_thread_in_display_thread(caller_tid)) {
640                 list = &cb_list[0];
641                 other_list = &cb_list[1];
642         } else {
643                 other_list = &cb_list[0];
644                 list = &cb_list[1];
645         }
646
647         LIST_INITHEAD(&call_list);
648
649         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
650                 if (cb->object != object ||
651                         cb->cb_type != cb_base->type ||
652                         cb->cb_data != cb_base->data)
653                         continue;
654
655                 LIST_ADDTAIL(&cb->call_link, &call_list);
656         }
657
658         if (!LIST_IS_EMPTY(&call_list)) {
659                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
660                         LIST_DELINIT(&cb->call_link);
661                         if (tdm_debug_module & TDM_DEBUG_THREAD)
662                                 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
663                         pthread_mutex_unlock(&cb_list_lock);
664                         cb->func(private_display, cb->object, cb_base, cb->user_data);
665                         pthread_mutex_lock(&cb_list_lock);
666                 }
667         }
668
669         pthread_mutex_unlock(&cb_list_lock);
670
671         if (propagation) {
672                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
673                         if (cb->object != object ||
674                                 cb->cb_type != cb_base->type ||
675                                 cb->cb_data != cb_base->data)
676                                 continue;
677
678                         handler_in_other_thread = 1;
679                         break;
680                 }
681         }
682
683         if (!handler_in_other_thread) {
684                 if (keep_private_thread) {
685                         if (cb_base->sync && waiting_tid != 0) {
686                                 waiting_tid = 0;
687                                 pthread_cond_signal(&keep_private_thread->event_cond);
688                                 if (tdm_debug_module & TDM_DEBUG_THREAD)
689                                         TDM_INFO("pthread broadcase");
690                         }
691                 }
692                 if (tdm_debug_module & TDM_DEBUG_THREAD)
693                         TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
694                                          tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
695                 return TDM_ERROR_NONE;
696         }
697
698         /* Once we reach here, it means that keep_private_thread is not NULL.
699          * Just make the crash. Avoiding it is not going to help us.
700          */
701         assert(keep_private_thread != NULL);
702
703         ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
704         TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
705
706         /* waiting until all cb are done in another thread */
707         if (cb_base->sync) {
708                 /* if waiting_tid is not 0, it means there are two sync-type events at the same time.
709                  * and it would make deadlock issue.
710                  */
711                 assert(waiting_tid == 0);
712
713                 if (tdm_debug_module & TDM_DEBUG_THREAD)
714                         TDM_INFO("pthread wait");
715
716                 /* pthread_cond_wait atomically release mutex, Upon successful return,
717                  * the mutex shall have been locked and shall be owned by the calling thread
718                  */
719                 tdm_mutex_locked = 0;
720                 waiting_tid = caller_tid;
721                 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
722                 tdm_mutex_locked = 1;
723         }
724
725         if (tdm_debug_module & TDM_DEBUG_THREAD)
726                 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
727                                  tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
728
729         return TDM_ERROR_NONE;
730 }