1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
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>
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:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
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.
34 **************************************************************************/
40 #include <sys/socket.h>
43 #include "tdm_private.h"
46 static tdm_private_thread *keep_private_thread;
48 struct _tdm_private_thread {
49 tdm_private_loop *private_loop;
51 pthread_t event_thread;
56 /* 0: read, 1: write */
61 _tdm_thread_main(void *data)
63 tdm_private_thread *private_thread = (tdm_private_thread*)data;
64 tdm_private_loop *private_loop = private_thread->private_loop;
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.
73 private_thread->thread_tid = syscall(SYS_gettid);
75 TDM_INFO("display_tid:%d, thread_tid: %d",
76 private_thread->display_tid, private_thread->thread_tid);
78 fd = tdm_event_loop_get_fd(private_loop->dpy);
80 TDM_ERR("couldn't get fd");
90 TDM_INFO("server flush");
91 tdm_event_loop_flush(private_loop->dpy);
94 TDM_INFO("fd(%d) polling in", fd);
96 ret = poll(&fds, 1, -1);
99 TDM_INFO("fd(%d) polling out", fd);
102 if (errno == EINTR || errno == EAGAIN) /* normal case */
105 TDM_ERR("poll failed: %m");
110 if (tdm_debug_thread)
111 TDM_INFO("thread got events");
113 if (tdm_event_loop_dispatch(private_loop->dpy) < 0)
114 TDM_ERR("dispatch error");
121 /* NOTE: tdm thread doesn't care about multi-thread. */
123 tdm_thread_init(tdm_private_loop *private_loop)
125 tdm_private_display *private_display;
126 tdm_private_thread *private_thread;
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);
132 private_display = private_loop->dpy;
133 TDM_RETURN_VAL_IF_FAIL(private_display->private_loop, TDM_ERROR_OPERATION_FAILED);
135 if (private_loop->private_thread)
136 return TDM_ERROR_NONE;
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;
145 private_thread = calloc(1, sizeof * private_thread);
146 if (!private_thread) {
147 TDM_ERR("alloc failed");
148 return TDM_ERROR_OUT_OF_MEMORY;
151 if (pipe(private_thread->pipe) != 0) {
152 TDM_ERR("socketpair failed: %m");
153 free(private_thread);
154 return TDM_ERROR_OPERATION_FAILED;
157 private_thread->private_loop = private_loop;
158 private_loop->private_thread = private_thread;
160 private_thread->display_tid = syscall(SYS_gettid);
162 pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main,
165 keep_private_thread = private_thread;
167 TDM_INFO("using a TDM event thread. pipe(%d,%d)",
168 private_thread->pipe[0], private_thread->pipe[1]);
170 return TDM_ERROR_NONE;
174 tdm_thread_deinit(tdm_private_loop *private_loop)
176 tdm_private_display *private_display;
178 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
180 if (!private_loop->private_thread)
183 pthread_cancel(private_loop->private_thread->event_thread);
185 private_display = private_loop->dpy;
187 /* before falling into the block of pthread_join, we have to unlock the mutex
188 * for subthread to use the mutex.
190 _pthread_mutex_unlock(&private_display->lock);
191 pthread_join(private_loop->private_thread->event_thread, NULL);
192 _pthread_mutex_lock(&private_display->lock);
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]);
199 free(private_loop->private_thread);
200 private_loop->private_thread = NULL;
201 keep_private_thread = NULL;
203 TDM_INFO("Finish a TDM event thread");
207 tdm_thread_get_fd(tdm_private_loop *private_loop)
209 tdm_private_thread *private_thread;
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);
215 private_thread = private_loop->private_thread;
217 return private_thread->pipe[0];
221 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base)
223 tdm_private_thread *private_thread;
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);
231 private_thread = private_loop->private_thread;
233 if (tdm_debug_thread)
234 TDM_INFO("fd(%d) type(%d), length(%d)",
235 private_thread->pipe[1], base->type, base->length);
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;
243 return TDM_ERROR_NONE;
247 tdm_thread_handle_cb(tdm_private_loop *private_loop)
249 tdm_private_display *private_display;
250 tdm_private_thread *private_thread;
251 tdm_thread_cb_base *base;
255 /* DON'T check TDM_MUTEX_IS_LOCKED here */
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);
260 private_thread = private_loop->private_thread;
261 private_display = private_loop->dpy;
263 len = read(private_thread->pipe[0], buffer, sizeof buffer);
265 if (tdm_debug_thread)
266 TDM_INFO("fd(%d) read length(%d)", private_thread->pipe[0], len);
269 return TDM_ERROR_NONE;
271 if (len < sizeof * base) {
272 TDM_NEVER_GET_HERE();
273 return TDM_ERROR_OPERATION_FAILED;
276 _pthread_mutex_lock(&private_display->lock);
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);
292 tdm_output_cb_commit(output_backend, output_commit->sequence,
293 output_commit->tv_sec, output_commit->tv_usec,
294 output_commit->user_data);
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);
305 tdm_output_cb_vblank(output_backend, output_vblank->sequence,
306 output_vblank->tv_sec, output_vblank->tv_usec,
307 output_vblank->user_data);
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);
318 tdm_output_cb_status(output_backend, output_status->status,
319 output_status->user_data);
322 case TDM_THREAD_CB_PP_DONE: {
323 tdm_thread_cb_pp_done *pp_done = (tdm_thread_cb_pp_done*)base;
325 tdm_pp_find_stamp(private_loop->dpy, pp_done->pp_stamp);
327 TDM_WRN("no pp(%ld)", pp_done->pp_stamp);
330 tdm_pp_cb_done(pp_backend, pp_done->src, pp_done->dst, pp_done->user_data);
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);
341 tdm_capture_cb_done(capture_backend, capture_done->buffer, capture_done->user_data);
350 _pthread_mutex_unlock(&private_display->lock);
352 return TDM_ERROR_NONE;
356 tdm_thread_in_display_thread(pid_t tid)
358 if (!keep_private_thread)
361 /* DON'T check TDM_MUTEX_IS_LOCKED here */
363 return (keep_private_thread->display_tid == tid) ? 1 : 0;
367 tdm_thread_is_running(void)
369 /* DON'T check TDM_MUTEX_IS_LOCKED here */
371 return (keep_private_thread) ? 1 : 0;