virtual: share buffer with client
[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_DISPLAY_OUTPUT_CREATE:
455                 case TDM_THREAD_CB_OUTPUT_DESTROY:
456                 case TDM_THREAD_CB_OUTPUT_COMMIT:
457                 case TDM_THREAD_CB_OUTPUT_VBLANK:
458                 case TDM_THREAD_CB_OUTPUT_STATUS:
459                 case TDM_THREAD_CB_OUTPUT_DPMS:
460                 case TDM_THREAD_CB_PP_DONE:
461                 case TDM_THREAD_CB_CAPTURE_DONE:
462                 case TDM_THREAD_CB_VBLANK_SW:
463                 case TDM_THREAD_CB_VBLANK_CREATE:
464                 case TDM_THREAD_CB_HWC_COMMIT:
465                 case TDM_THREAD_CB_VOUTPUT_COMMIT:
466                         /* this event comes from other thread. so we don't need to propagate this to other thread */
467                         ret = tdm_thread_cb_call(NULL, base, 0);
468                         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
469                         break;
470                 case TDM_THREAD_CB_EXIT:
471                         private_thread->event_thread_exit = 1;
472                         break;
473                 default:
474                         break;
475                 }
476                 i += base->length;
477         }
478
479         return TDM_ERROR_NONE;
480 }
481
482 INTERN int
483 tdm_thread_in_display_thread(pid_t tid)
484 {
485         if (!keep_private_thread)
486                 return 1;
487
488         /* DON'T check TDM_MUTEX_IS_LOCKED here */
489
490         return (keep_private_thread->display_tid == tid) ? 1 : 0;
491 }
492
493 INTERN int
494 tdm_thread_is_running(void)
495 {
496         /* DON'T check TDM_MUTEX_IS_LOCKED here */
497
498         return (keep_private_thread && !keep_private_thread->event_thread_joined) ? 1 : 0;
499 }
500
501 static void
502 _tdm_thread_free_cb(tdm_private_thread_cb *cb)
503 {
504         if (tdm_debug_module & TDM_DEBUG_THREAD)
505                 TDM_INFO("cb(%p) removed", cb);
506
507         assert(LIST_IS_EMPTY(&cb->call_link));
508
509         LIST_DEL(&cb->link);
510         free(cb);
511 }
512
513 static tdm_private_thread_cb *
514 _tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type,
515                                         void *cb_data, tdm_thread_cb func, void *user_data)
516 {
517         tdm_private_thread_cb *cb = NULL;
518
519         LIST_FOR_EACH_ENTRY(cb, list, link) {
520                 if (cb->object == object &&
521                         cb->cb_type == cb_type &&
522                         cb->cb_data == cb_data &&
523                         cb->func == func &&
524                         cb->user_data == user_data)
525                         return cb;
526         }
527
528         return NULL;
529 }
530
531 INTERN void
532 tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func)
533 {
534         TDM_RETURN_IF_FAIL(cb_type > 0);
535
536         if (func && find_funcs[cb_type])
537                 TDM_NEVER_GET_HERE();
538
539         find_funcs[cb_type] = func;
540 }
541
542 INTERN tdm_error
543 tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
544 {
545         tdm_private_thread_cb *cb = NULL;
546         pid_t caller_tid;
547         struct list_head *list;
548
549         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
550         TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER);
551         TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER);
552         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
553
554         caller_tid = syscall(SYS_gettid);
555
556         pthread_mutex_lock(&cb_list_lock);
557
558         if (tdm_thread_in_display_thread(caller_tid))
559                 list = &cb_list[0];
560         else
561                 list = &cb_list[1];
562
563         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
564         if (cb) {
565                 pthread_mutex_unlock(&cb_list_lock);
566                 TDM_ERR("can't be added twice with same data");
567                 return TDM_ERROR_BAD_REQUEST;
568         }
569
570         cb = calloc(1, sizeof *cb);
571         if (!cb) {
572                 pthread_mutex_unlock(&cb_list_lock);
573                 TDM_ERR("calloc failed");
574                 return TDM_ERROR_OUT_OF_MEMORY;
575         }
576
577         LIST_ADDTAIL(&cb->link, list);
578         LIST_INITHEAD(&cb->call_link);
579
580         cb->object = object;
581         cb->cb_type = cb_type;
582         cb->cb_data = cb_data;
583         cb->func = func;
584         cb->user_data = user_data;
585         cb->owner_tid = caller_tid;
586
587         if (tdm_debug_module & TDM_DEBUG_THREAD)
588                 TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb);
589
590         pthread_mutex_unlock(&cb_list_lock);
591
592         return TDM_ERROR_NONE;
593 }
594
595 INTERN void
596 tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data)
597 {
598         tdm_private_thread_cb *cb;
599         struct list_head *list;
600
601         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
602         TDM_RETURN_IF_FAIL(object != NULL);
603         TDM_RETURN_IF_FAIL(cb_type > 0);
604         TDM_RETURN_IF_FAIL(func != NULL);
605
606         pthread_mutex_lock(&cb_list_lock);
607
608         list = &cb_list[0];
609         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
610         if (cb)
611                 _tdm_thread_free_cb(cb);
612
613         list = &cb_list[1];
614         cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data);
615         if (cb)
616                 _tdm_thread_free_cb(cb);
617
618         pthread_mutex_unlock(&cb_list_lock);
619 }
620
621 /* when call a callback, we check both cb_base's type and cb_base's data,
622  * because a callback is added with cb_type and cb_data.
623  */
624 INTERN tdm_error
625 tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base, unsigned int propagation)
626 {
627         tdm_private_display *private_display = tdm_display_get();
628         tdm_private_thread_cb *cb = NULL, *hh = NULL;
629         int handler_in_other_thread = 0;
630         pid_t caller_tid;
631         struct list_head *list, *other_list;
632         struct list_head call_list;
633         static pid_t waiting_tid = 0;
634         tdm_error ret;
635
636         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
637         TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER);
638         TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER);
639         TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER);
640         TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER);
641
642         caller_tid = syscall(SYS_gettid);
643
644         assert(find_funcs[cb_base->type] != NULL);
645
646         if (keep_private_thread && keep_private_thread->thread_tid != caller_tid) {
647                 /* A sync-type event from display-thread to tdm-thread can't be handled.
648                  * If sync-type events happen in both threads at the same time,
649                  * it would make a deadlock issue.
650                  */
651                 assert(cb_base->sync != 1);
652         }
653
654         if (tdm_debug_module & TDM_DEBUG_THREAD)
655                 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------",
656                                  tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
657
658         if (!object) {
659                 object = find_funcs[cb_base->type](private_display, cb_base->object_stamp);
660                 if (!object) {
661                         TDM_WRN("%p gone", object);
662                         return TDM_ERROR_NONE;
663                 }
664         }
665
666         pthread_mutex_lock(&cb_list_lock);
667
668         if (tdm_thread_in_display_thread(caller_tid)) {
669                 list = &cb_list[0];
670                 other_list = &cb_list[1];
671         } else {
672                 other_list = &cb_list[0];
673                 list = &cb_list[1];
674         }
675
676         LIST_INITHEAD(&call_list);
677
678         LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) {
679                 if (cb->object != object ||
680                         cb->cb_type != cb_base->type ||
681                         cb->cb_data != cb_base->data)
682                         continue;
683
684                 LIST_ADDTAIL(&cb->call_link, &call_list);
685         }
686
687         if (!LIST_IS_EMPTY(&call_list)) {
688                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) {
689                         LIST_DELINIT(&cb->call_link);
690                         if (tdm_debug_module & TDM_DEBUG_THREAD)
691                                 TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb);
692                         pthread_mutex_unlock(&cb_list_lock);
693                         cb->func(private_display, cb->object, cb_base, cb->user_data);
694                         pthread_mutex_lock(&cb_list_lock);
695                 }
696         }
697
698         pthread_mutex_unlock(&cb_list_lock);
699
700         if (propagation) {
701                 LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) {
702                         if (cb->object != object ||
703                                 cb->cb_type != cb_base->type ||
704                                 cb->cb_data != cb_base->data)
705                                 continue;
706
707                         handler_in_other_thread = 1;
708                         break;
709                 }
710         }
711
712         if (!handler_in_other_thread) {
713                 if (keep_private_thread) {
714                         /* NOTE quick fix
715                          * In case of 'cb_base->sync = 0' and 'waiting_tid != 0',
716                          * probably it means one thread is waiting for signal of
717                          * pthread condition.
718                          */
719                         if (!cb_base->sync && waiting_tid != 0) {
720                                 waiting_tid = 0;
721                                 pthread_cond_signal(&keep_private_thread->event_cond);
722                                 if (tdm_debug_module & TDM_DEBUG_THREAD)
723                                         TDM_INFO("pthread broadcase");
724                         }
725                 }
726                 if (tdm_debug_module & TDM_DEBUG_THREAD)
727                         TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
728                                          tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
729                 return TDM_ERROR_NONE;
730         }
731
732         /* Once we reach here, it means that keep_private_thread is not NULL.
733          * Just make the crash. Avoiding it is not going to help us.
734          */
735         assert(keep_private_thread != NULL);
736
737         if (!cb_base->sync) {
738                 ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
739                 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
740         }
741         /* waiting until all cb are done in another thread */
742         else {
743                 /* NOTE quick fix
744                  * Sync type event from display-thread(main thread) can't be
745                  * handled. In this way, if we call tdm_thread_send_cb() with
746                  * 'cb_base->sync = 1', then libtdm will eveventually raise
747                  * abort(). Please see commit '4abfab36' for more detail.
748                  */
749                 cb_base->sync = 0;
750                 ret = tdm_thread_send_cb(private_display->private_loop, cb_base);
751                 TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED);
752                 cb_base->sync = 1;
753
754                 /* if waiting_tid is not 0, it means there are two sync-type events at the same time.
755                  * and it would make deadlock issue.
756                  */
757                 assert(waiting_tid == 0);
758
759                 if (tdm_debug_module & TDM_DEBUG_THREAD)
760                         TDM_INFO("pthread wait");
761
762                 /* pthread_cond_wait atomically release mutex, Upon successful return,
763                  * the mutex shall have been locked and shall be owned by the calling thread
764                  */
765                 tdm_mutex_locked = 0;
766                 waiting_tid = caller_tid;
767                 pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock);
768                 tdm_mutex_locked = 1;
769         }
770
771         if (tdm_debug_module & TDM_DEBUG_THREAD)
772                 TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...",
773                                  tdm_cb_type_str(cb_base->type), cb_base->sync, propagation);
774
775         return TDM_ERROR_NONE;
776 }