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