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