1a9a853edb4f1038490ecbff824f8794fdefc61d
[platform/core/uifw/libtdm.git] / src / tdm_thread.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <sys/socket.h>
41
42 #include "tdm.h"
43 #include "tdm_private.h"
44 #include "tdm_list.h"
45
46 static tdm_private_thread *keep_private_thread;
47
48 struct _tdm_private_thread {
49         tdm_private_loop *private_loop;
50
51         pthread_t event_thread;
52
53         pid_t display_tid;
54         pid_t thread_tid;
55
56         /* 0: read, 1: write */
57         int pipe[2];
58 };
59
60 static void*
61 _tdm_thread_main(void *data)
62 {
63         tdm_private_thread *private_thread = (tdm_private_thread*)data;
64         tdm_private_loop *private_loop = private_thread->private_loop;
65         int fd;
66         struct pollfd fds;
67         int ret;
68
69         /* Not lock/unlock for the private_thread and private_loop structure
70          * because they won't be destroyed as long as tdm thread is running.
71          * When they're destroyed, we have already exit the tdm thread.
72          */
73         private_thread->thread_tid = syscall(SYS_gettid);
74
75         TDM_INFO("display_tid:%d, thread_tid: %d",
76                          private_thread->display_tid, private_thread->thread_tid);
77
78         fd = tdm_event_loop_get_fd(private_loop->dpy);
79         if (fd < 0) {
80                 TDM_ERR("couldn't get fd");
81                 goto exit_thread;
82         }
83
84         fds.events = POLLIN;
85         fds.fd = fd;
86         fds.revents = 0;
87
88         while (1) {
89                 if (tdm_debug_thread)
90                         TDM_INFO("server flush");
91                 tdm_event_loop_flush(private_loop->dpy);
92
93                 if (tdm_debug_thread)
94                         TDM_INFO("fd(%d) polling in", fd);
95
96                 ret = poll(&fds, 1, -1);
97
98                 if (tdm_debug_thread)
99                         TDM_INFO("fd(%d) polling out", fd);
100
101                 if (ret < 0) {
102                         if (errno == EINTR || errno == EAGAIN)  /* normal case */
103                                 continue;
104                         else {
105                                 TDM_ERR("poll failed: %m");
106                                 goto exit_thread;
107                         }
108                 }
109
110                 if (tdm_debug_thread)
111                         TDM_INFO("thread got events");
112
113                 if (tdm_event_loop_dispatch(private_loop->dpy) < 0)
114                         TDM_ERR("dispatch error");
115         }
116
117 exit_thread:
118         pthread_exit(NULL);
119 }
120
121 /* NOTE: tdm thread doesn't care about multi-thread. */
122 INTERN tdm_error
123 tdm_thread_init(tdm_private_loop *private_loop)
124 {
125         tdm_private_display *private_display;
126         tdm_private_thread *private_thread;
127         const char *thread;
128
129         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
130         TDM_RETURN_VAL_IF_FAIL(private_loop->dpy, TDM_ERROR_OPERATION_FAILED);
131
132         private_display = private_loop->dpy;
133         TDM_RETURN_VAL_IF_FAIL(private_display->private_loop, TDM_ERROR_OPERATION_FAILED);
134
135         if (private_loop->private_thread)
136                 return TDM_ERROR_NONE;
137
138         /* enable as default */
139         thread = getenv("TDM_THREAD");
140         if (!thread || strncmp(thread, "1", 1)) {
141                 TDM_INFO("not using a TDM event thread: %s", (thread) ? thread : "none");
142                 return TDM_ERROR_NONE;
143         }
144
145         private_thread = calloc(1, sizeof * private_thread);
146         if (!private_thread) {
147                 TDM_ERR("alloc failed");
148                 return TDM_ERROR_OUT_OF_MEMORY;
149         }
150
151         if (pipe(private_thread->pipe) != 0) {
152                 TDM_ERR("socketpair failed: %m");
153                 free(private_thread);
154                 return TDM_ERROR_OPERATION_FAILED;
155         }
156
157         private_thread->private_loop = private_loop;
158         private_loop->private_thread = private_thread;
159
160         private_thread->display_tid = syscall(SYS_gettid);
161
162         pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
163                                    private_thread);
164
165         keep_private_thread = private_thread;
166
167         TDM_INFO("using a TDM event thread. pipe(%d,%d)",
168                          private_thread->pipe[0], private_thread->pipe[1]);
169
170         return TDM_ERROR_NONE;
171 }
172
173 INTERN void
174 tdm_thread_deinit(tdm_private_loop *private_loop)
175 {
176         tdm_private_display *private_display;
177
178         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
179
180         if (!private_loop->private_thread)
181                 return;
182
183         pthread_cancel(private_loop->private_thread->event_thread);
184
185         private_display = private_loop->dpy;
186
187         /* before falling into the block of pthread_join, we have to unlock the mutex
188          * for subthread to use the mutex.
189          */
190         _pthread_mutex_unlock(&private_display->lock);
191         pthread_join(private_loop->private_thread->event_thread, NULL);
192         _pthread_mutex_lock(&private_display->lock);
193
194         if (private_loop->private_thread->pipe[0] >= 0)
195                 close(private_loop->private_thread->pipe[0]);
196         if (private_loop->private_thread->pipe[1] >= 0)
197                 close(private_loop->private_thread->pipe[1]);
198
199         free(private_loop->private_thread);
200         private_loop->private_thread = NULL;
201         keep_private_thread = NULL;
202
203         TDM_INFO("Finish a TDM event thread");
204 }
205
206 INTERN int
207 tdm_thread_get_fd(tdm_private_loop *private_loop)
208 {
209         tdm_private_thread *private_thread;
210
211         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
212         TDM_RETURN_VAL_IF_FAIL(private_loop, -1);
213         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1);
214
215         private_thread = private_loop->private_thread;
216
217         return private_thread->pipe[0];
218 }
219
220 INTERN tdm_error
221 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
222 {
223         tdm_private_thread *private_thread;
224         ssize_t len;
225
226         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED);
227         TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER);
228         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
229         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
230
231         private_thread = private_loop->private_thread;
232
233         if (tdm_debug_thread)
234                 TDM_INFO("fd(%d) type(%d), length(%d)",
235                                  private_thread->pipe[1], base->type, base->length);
236
237         len = write(private_thread->pipe[1], base, base->length);
238         if (len != base->length) {
239                 TDM_ERR("write failed (%d != %d): %m", (int)len, base->length);
240                 return TDM_ERROR_OPERATION_FAILED;
241         }
242
243         return TDM_ERROR_NONE;
244 }
245
246 INTERN tdm_error
247 tdm_thread_handle_cb(tdm_private_loop *private_loop)
248 {
249         tdm_private_display *private_display;
250         tdm_private_thread *private_thread;
251         tdm_thread_cb_base *base;
252         char buffer[1024];
253         unsigned int len, i;
254
255         /* DON'T check TDM_MUTEX_IS_LOCKED here */
256
257         TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER);
258         TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER);
259
260         private_thread = private_loop->private_thread;
261         private_display = private_loop->dpy;
262
263         len = read(private_thread->pipe[0], buffer, sizeof buffer);
264
265         if (tdm_debug_thread)
266                 TDM_INFO("fd(%d) read length(%d)", private_thread->pipe[0], len);
267
268         if (len == 0)
269                 return TDM_ERROR_NONE;
270
271         if (len < sizeof * base) {
272                 TDM_NEVER_GET_HERE();
273                 return TDM_ERROR_OPERATION_FAILED;
274         }
275
276         _pthread_mutex_lock(&private_display->lock);
277
278         i = 0;
279         while (i < len) {
280                 base = (tdm_thread_cb_base*)&buffer[i];
281                 if (tdm_debug_thread)
282                         TDM_INFO("type(%d), length(%d)", base->type, base->length);
283                 switch (base->type) {
284                 case TDM_THREAD_CB_OUTPUT_COMMIT: {
285                         tdm_thread_cb_output_commit *output_commit = (tdm_thread_cb_output_commit*)base;
286                         tdm_output *output_backend =
287                                 tdm_display_find_output_stamp(private_loop->dpy, output_commit->output_stamp);
288                         if (!output_backend) {
289                                 TDM_WRN("no output(%ld)", output_commit->output_stamp);
290                                 break;
291                         }
292                         tdm_output_cb_commit(output_backend, output_commit->sequence,
293                                                                  output_commit->tv_sec, output_commit->tv_usec,
294                                                                  output_commit->user_data);
295                         break;
296                 }
297                 case TDM_THREAD_CB_OUTPUT_VBLANK: {
298                         tdm_thread_cb_output_vblank *output_vblank = (tdm_thread_cb_output_vblank*)base;
299                         tdm_output *output_backend =
300                                 tdm_display_find_output_stamp(private_loop->dpy, output_vblank->output_stamp);
301                         if (!output_backend) {
302                                 TDM_WRN("no output(%ld)", output_vblank->output_stamp);
303                                 break;
304                         }
305                         tdm_output_cb_vblank(output_backend, output_vblank->sequence,
306                                                                  output_vblank->tv_sec, output_vblank->tv_usec,
307                                                                  output_vblank->user_data);
308                         break;
309                 }
310                 case TDM_THREAD_CB_OUTPUT_STATUS: {
311                         tdm_thread_cb_output_status *output_status = (tdm_thread_cb_output_status*)base;
312                         tdm_output *output_backend =
313                                 tdm_display_find_output_stamp(private_loop->dpy, output_status->output_stamp);
314                         if (!output_backend) {
315                                 TDM_WRN("no output(%ld)", output_status->output_stamp);
316                                 break;
317                         }
318                         tdm_output_cb_status(output_backend, output_status->status,
319                                                                  output_status->user_data);
320                         break;
321                 }
322                 case TDM_THREAD_CB_PP_DONE: {
323                         tdm_thread_cb_pp_done *pp_done = (tdm_thread_cb_pp_done*)base;
324                         tdm_pp *pp_backend =
325                                 tdm_pp_find_stamp(private_loop->dpy, pp_done->pp_stamp);
326                         if (!pp_backend) {
327                                 TDM_WRN("no pp(%ld)", pp_done->pp_stamp);
328                                 break;
329                         }
330                         tdm_pp_cb_done(pp_backend, pp_done->src, pp_done->dst, pp_done->user_data);
331                         break;
332                 }
333                 case TDM_THREAD_CB_CAPTURE_DONE: {
334                         tdm_thread_cb_capture_done *capture_done = (tdm_thread_cb_capture_done*)base;
335                         tdm_capture *capture_backend =
336                                 tdm_capture_find_stamp(private_loop->dpy, capture_done->capture_stamp);
337                         if (!capture_backend) {
338                                 TDM_WRN("no capture(%ld)", capture_done->capture_stamp);
339                                 break;
340                         }
341                         tdm_capture_cb_done(capture_backend, capture_done->buffer, capture_done->user_data);
342                         break;
343                 }
344                 default:
345                         break;
346                 }
347                 i += base->length;
348         }
349
350         _pthread_mutex_unlock(&private_display->lock);
351
352         return TDM_ERROR_NONE;
353 }
354
355 INTERN int
356 tdm_thread_in_display_thread(pid_t tid)
357 {
358         if (!keep_private_thread)
359                 return 1;
360
361         /* DON'T check TDM_MUTEX_IS_LOCKED here */
362
363         return (keep_private_thread->display_tid == tid) ? 1 : 0;
364 }
365
366 INTERN int
367 tdm_thread_is_running(void)
368 {
369         /* DON'T check TDM_MUTEX_IS_LOCKED here */
370
371         return (keep_private_thread) ? 1 : 0;
372 }