X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftdm_thread.c;h=40d7859ed752a367e8ca2c0abeedbaea3ef1ca51;hb=70fed1b2a9e9a77f139e63fa1844cf90120322cb;hp=25d76e96a6d7c2eee86772b849aa290869f0cc00;hpb=b8fba9abbd8774defa90185e02dce7bb4778be7a;p=platform%2Fcore%2Fuifw%2Flibtdm.git diff --git a/src/tdm_thread.c b/src/tdm_thread.c index 25d76e9..40d7859 100644 --- a/src/tdm_thread.c +++ b/src/tdm_thread.c @@ -1,60 +1,103 @@ /************************************************************************** - -libtdm - -Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved. - -Contact: Eunchul Kim , - JinYoung Jeon , - Taeheon Kim , - YoungJun Cho , - SooChan Lim , - Boram Park - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sub license, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice (including the -next paragraph) shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. -IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR -ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - + * + * libtdm + * + * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved. + * + * Contact: Eunchul Kim , + * JinYoung Jeon , + * Taeheon Kim , + * YoungJun Cho , + * SooChan Lim , + * Boram Park + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * **************************************************************************/ #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include - -#include "tdm.h" #include "tdm_private.h" -#include "tdm_list.h" + +static tdm_private_thread *keep_private_thread; struct _tdm_private_thread { tdm_private_loop *private_loop; + pthread_cond_t event_cond; pthread_t event_thread; + unsigned int event_thread_exit; + unsigned int event_thread_joined; pid_t display_tid; pid_t thread_tid; - /* 0: read, 1: write */ + /* 0: read, 1: write (tdm-thread -> display-thread) */ int pipe[2]; + + /* 0: read, 1: write (tdm-thread <- display-thread) */ + int sub_pipe[2]; + tdm_event_loop_source *sub_event_source; }; +typedef struct _tdm_private_thread_cb { + struct list_head link; + struct list_head call_link; + + void *object; + tdm_thread_cb_type cb_type; + void *cb_data; + tdm_thread_cb func; + void *user_data; + + pid_t owner_tid; +} tdm_private_thread_cb; + +static tdm_thread_find_object find_funcs[TDM_THREAD_CB_MAX] = {0, }; + +/* 0: for display thread, 1: for tdm thread */ +static struct list_head cb_list[2]; +static pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER; + +static void _tdm_thread_free_cb(tdm_private_thread_cb *cb); + +static tdm_error +_tdm_thread_handle_events(int fd, tdm_event_loop_mask mask, void *user_data) +{ + tdm_private_loop *private_loop = user_data; + tdm_error ret; + + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); + TDM_RETURN_VAL_IF_FAIL(private_loop != NULL, TDM_ERROR_OPERATION_FAILED); + TDM_RETURN_VAL_IF_FAIL(private_loop->dpy != NULL, TDM_ERROR_OPERATION_FAILED); + + ret = tdm_thread_handle_cb(private_loop); + + TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret); + + return TDM_ERROR_NONE; +} + static void* _tdm_thread_main(void *data) { @@ -63,6 +106,7 @@ _tdm_thread_main(void *data) int fd; struct pollfd fds; int ret; + tdm_error error; /* Not lock/unlock for the private_thread and private_loop structure * because they won't be destroyed as long as tdm thread is running. @@ -71,7 +115,29 @@ _tdm_thread_main(void *data) private_thread->thread_tid = syscall(SYS_gettid); TDM_INFO("display_tid:%d, thread_tid: %d", - private_thread->display_tid, private_thread->thread_tid); + private_thread->display_tid, private_thread->thread_tid); + + /* fd SHOULD be the same with sub_pipe[0] to make sure that using + * tdm_thread_get_fd, tdm_thread_send_cb, tdm_thread_handle_cb in + * both threads is fine. Otherwise, assert. + */ + tdm_display_lock(private_loop->dpy); + assert(tdm_thread_get_fd(private_loop) == private_thread->sub_pipe[0]); + + private_thread->sub_event_source = + tdm_event_loop_add_fd_handler(private_loop->dpy, + private_thread->sub_pipe[0], + TDM_EVENT_LOOP_READABLE, + _tdm_thread_handle_events, + private_loop, + &error); + TDM_GOTO_IF_FAIL(error == TDM_ERROR_NONE, exit_thread); + TDM_GOTO_IF_FAIL(private_thread->sub_event_source != NULL, exit_thread); + + pthread_cond_signal(&private_thread->event_cond); + + /* mutex shall be locked by the thread calling pthread_cond_signal() */ + tdm_display_unlock(private_loop->dpy); fd = tdm_event_loop_get_fd(private_loop->dpy); if (fd < 0) { @@ -84,20 +150,23 @@ _tdm_thread_main(void *data) fds.revents = 0; while (1) { - if (tdm_debug_thread) + if (tdm_debug_module & TDM_DEBUG_EVENT) TDM_INFO("server flush"); tdm_event_loop_flush(private_loop->dpy); - if (tdm_debug_thread) + if (private_thread->event_thread_exit) + break; + + if (tdm_debug_module & TDM_DEBUG_EVENT) TDM_INFO("fd(%d) polling in", fd); ret = poll(&fds, 1, -1); - if (tdm_debug_thread) + if (tdm_debug_module & TDM_DEBUG_EVENT) TDM_INFO("fd(%d) polling out", fd); if (ret < 0) { - if (errno == EBUSY) /* normal case */ + if (errno == EINTR || errno == EAGAIN) /* normal case */ continue; else { TDM_ERR("poll failed: %m"); @@ -105,7 +174,7 @@ _tdm_thread_main(void *data) } } - if (tdm_debug_thread) + if (tdm_debug_module & TDM_DEBUG_EVENT) TDM_INFO("thread got events"); if (tdm_event_loop_dispatch(private_loop->dpy) < 0) @@ -113,17 +182,35 @@ _tdm_thread_main(void *data) } exit_thread: + tdm_display_unlock(private_loop->dpy); pthread_exit(NULL); } +static tdm_error +_tdm_thread_exit(tdm_private_loop *private_loop) +{ + tdm_thread_cb_base cb_base; + tdm_error ret; + + memset(&cb_base, 0, sizeof cb_base); + cb_base.type = TDM_THREAD_CB_EXIT; + cb_base.length = sizeof cb_base; + + ret = tdm_thread_send_cb(private_loop, &cb_base); + TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, ret); + + return ret; +} + /* NOTE: tdm thread doesn't care about multi-thread. */ INTERN tdm_error tdm_thread_init(tdm_private_loop *private_loop) { tdm_private_display *private_display; tdm_private_thread *private_thread; - const char *thread; + int thread, i; + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); TDM_RETURN_VAL_IF_FAIL(private_loop->dpy, TDM_ERROR_OPERATION_FAILED); private_display = private_loop->dpy; @@ -132,35 +219,68 @@ tdm_thread_init(tdm_private_loop *private_loop) if (private_loop->private_thread) return TDM_ERROR_NONE; + for (i = 0; i < TDM_THREAD_CB_MAX; i++) + find_funcs[i] = NULL; + + LIST_INITHEAD(&cb_list[0]); + LIST_INITHEAD(&cb_list[1]); + /* enable as default */ - thread = getenv("TDM_THREAD"); - if (!thread || !strncmp(thread, "1", 1)) { + thread = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_THREAD, 1); + if (!thread) { TDM_INFO("not using a TDM event thread"); return TDM_ERROR_NONE; } - private_thread = calloc(1, sizeof *private_thread); + private_thread = calloc(1, sizeof * private_thread); if (!private_thread) { TDM_ERR("alloc failed"); return TDM_ERROR_OUT_OF_MEMORY; } + if (pthread_cond_init(&private_thread->event_cond, NULL)) { + TDM_ERR("pthread_cond_init failed: %m"); + free(private_thread); + return TDM_ERROR_OUT_OF_MEMORY; + } + if (pipe(private_thread->pipe) != 0) { - TDM_ERR("socketpair failed: %m"); + TDM_ERR("pipe failed: %m"); + pthread_cond_destroy(&private_thread->event_cond); free(private_thread); return TDM_ERROR_OPERATION_FAILED; } + if (pipe(private_thread->sub_pipe) != 0) { + TDM_ERR("sub_pipe failed: %m"); + close(private_thread->pipe[0]); + close(private_thread->pipe[1]); + pthread_cond_destroy(&private_thread->event_cond); + free(private_thread); + return TDM_ERROR_OPERATION_FAILED; + } + + keep_private_thread = private_thread; + private_thread->private_loop = private_loop; private_loop->private_thread = private_thread; private_thread->display_tid = syscall(SYS_gettid); + /* pthread_cond_wait atomically release mutex, Upon successful return, + * the mutex shall have been locked and shall be owned by the calling thread + */ + tdm_mutex_locked = 0; pthread_create(&private_thread->event_thread, NULL, _tdm_thread_main, - private_thread); + private_thread); + + /* wait until the tdm thread starts */ + pthread_cond_wait(&private_thread->event_cond, &private_display->lock); + tdm_mutex_locked = 1; - TDM_INFO("using a TDM event thread. pipe(%d,%d)", - private_thread->pipe[0], private_thread->pipe[1]); + TDM_INFO("using a TDM event thread. pipe(%d,%d) sub_pipe(%d,%d)", + private_thread->pipe[0], private_thread->pipe[1], + private_thread->sub_pipe[0], private_thread->sub_pipe[1]); return TDM_ERROR_NONE; } @@ -168,19 +288,57 @@ tdm_thread_init(tdm_private_loop *private_loop) INTERN void tdm_thread_deinit(tdm_private_loop *private_loop) { + tdm_private_display *private_display; + tdm_private_thread_cb *cb = NULL, *hh = NULL; + tdm_error ret; + + TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED()); + if (!private_loop->private_thread) return; - pthread_cancel(private_loop->private_thread->event_thread); + ret = _tdm_thread_exit(private_loop); + if (ret != TDM_ERROR_NONE) + pthread_cancel(private_loop->private_thread->event_thread); + + private_display = private_loop->dpy; + + /* before falling into the block of pthread_join, we have to unlock the mutex + * for subthread to use the mutex. + */ + _pthread_mutex_unlock(&private_display->lock); pthread_join(private_loop->private_thread->event_thread, NULL); + private_loop->private_thread->event_thread_joined = 1; + TDM_INFO("Joined a TDM event thread"); + + pthread_mutex_unlock(&cb_list_lock); + tdm_log_reset(); + + if (private_loop->private_thread->sub_event_source) + tdm_event_loop_source_remove(private_loop->private_thread->sub_event_source); + + LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[0], link) { + _tdm_thread_free_cb(cb); + } + LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &cb_list[1], link) { + _tdm_thread_free_cb(cb); + } if (private_loop->private_thread->pipe[0] >= 0) close(private_loop->private_thread->pipe[0]); if (private_loop->private_thread->pipe[1] >= 0) close(private_loop->private_thread->pipe[1]); + if (private_loop->private_thread->sub_pipe[0] >= 0) + close(private_loop->private_thread->sub_pipe[0]); + if (private_loop->private_thread->sub_pipe[1] >= 0) + close(private_loop->private_thread->sub_pipe[1]); + + pthread_cond_destroy(&private_loop->private_thread->event_cond); + free(private_loop->private_thread); private_loop->private_thread = NULL; + keep_private_thread = NULL; TDM_INFO("Finish a TDM event thread"); } @@ -189,13 +347,21 @@ INTERN int tdm_thread_get_fd(tdm_private_loop *private_loop) { tdm_private_thread *private_thread; + int in_main; + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); TDM_RETURN_VAL_IF_FAIL(private_loop, -1); TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, -1); private_thread = private_loop->private_thread; - return private_thread->pipe[0]; + /* seems like ticky. but easy way to use the same APIs for both threads */ + in_main = tdm_thread_in_display_thread(syscall(SYS_gettid)); + + if (in_main) + return private_thread->pipe[0]; + else + return private_thread->sub_pipe[0]; } INTERN tdm_error @@ -203,23 +369,35 @@ tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base) { tdm_private_thread *private_thread; ssize_t len; + int pipe, in_main; + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); TDM_RETURN_VAL_IF_FAIL(base, TDM_ERROR_INVALID_PARAMETER); TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER); TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER); private_thread = private_loop->private_thread; - if (tdm_debug_thread) - TDM_INFO("fd(%d) type(%d), length(%d)", - private_thread->pipe[1], base->type, base->length); + /* seems like ticky. but easy way to use the same APIs for both threads */ + in_main = tdm_thread_in_display_thread(syscall(SYS_gettid)); + + if (in_main) + pipe = private_thread->sub_pipe[1]; + else + pipe = private_thread->pipe[1]; + + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("fd(%d) type(%s), length(%d)", pipe, tdm_cb_type_str(base->type), base->length); - len = write(private_thread->pipe[1], base, base->length); + len = write(pipe, base, base->length); if (len != base->length) { TDM_ERR("write failed (%d != %d): %m", (int)len, base->length); return TDM_ERROR_OPERATION_FAILED; } + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("[%s] write fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len); + return TDM_ERROR_NONE; } @@ -229,119 +407,370 @@ tdm_thread_handle_cb(tdm_private_loop *private_loop) tdm_private_thread *private_thread; tdm_thread_cb_base *base; char buffer[1024]; - int len, i; + unsigned int i; + int len, pipe, in_main; + tdm_error ret = TDM_ERROR_NONE; + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); TDM_RETURN_VAL_IF_FAIL(private_loop, TDM_ERROR_INVALID_PARAMETER); TDM_RETURN_VAL_IF_FAIL(private_loop->private_thread, TDM_ERROR_INVALID_PARAMETER); private_thread = private_loop->private_thread; - len = read(private_thread->pipe[0], buffer, sizeof buffer); + /* seems like ticky. but easy way to use the same APIs for both threads */ + in_main = tdm_thread_in_display_thread(syscall(SYS_gettid)); + + if (in_main) + pipe = private_thread->pipe[0]; + else + pipe = private_thread->sub_pipe[0]; + + do { + len = read(pipe, buffer, sizeof buffer); + } while (len < 0 && errno == EINTR); - if (tdm_debug_thread) - TDM_INFO("fd(%d) read length(%d)", private_thread->pipe[0], len); + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("[%s] read fd(%d) length(%d)", (in_main) ? "main" : "sub", pipe, len); + + if (len < 0) { + TDM_ERR("read failed: errno(%d), len(%d) %m", errno, len); + return TDM_ERROR_OPERATION_FAILED; + } if (len == 0) return TDM_ERROR_NONE; - if (len < sizeof *base) { - TDM_NEVER_GET_HERE(); + if (len < sizeof * base) { + TDM_ERR("read failed: len(%d)", len); return TDM_ERROR_OPERATION_FAILED; } i = 0; while (i < len) { base = (tdm_thread_cb_base*)&buffer[i]; - if (tdm_debug_thread) - TDM_INFO("type(%d), length(%d)", base->type, base->length); + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("type(%s), length(%d)", tdm_cb_type_str(base->type), base->length); switch (base->type) { + case TDM_THREAD_CB_DISPLAY_OUTPUT_CREATE: + case TDM_THREAD_CB_OUTPUT_DESTROY: case TDM_THREAD_CB_OUTPUT_COMMIT: - { - tdm_thread_cb_output_commit *output_commit = (tdm_thread_cb_output_commit*)base; - tdm_output *output_backend = - tdm_display_find_output_stamp(private_loop->dpy, output_commit->output_stamp); - if (!output_backend) { - TDM_WRN("no output(%ld)", output_commit->output_stamp); - break; - } - tdm_output_cb_commit(output_backend, output_commit->sequence, - output_commit->tv_sec, output_commit->tv_usec, - output_commit->user_data); - break; - } case TDM_THREAD_CB_OUTPUT_VBLANK: - { - tdm_thread_cb_output_vblank *output_vblank = (tdm_thread_cb_output_vblank*)base; - tdm_output *output_backend = - tdm_display_find_output_stamp(private_loop->dpy, output_vblank->output_stamp); - if (!output_backend) { - TDM_WRN("no output(%ld)", output_vblank->output_stamp); - break; - } - tdm_output_cb_vblank(output_backend, output_vblank->sequence, - output_vblank->tv_sec, output_vblank->tv_usec, - output_vblank->user_data); - break; - } case TDM_THREAD_CB_OUTPUT_STATUS: - { - tdm_thread_cb_output_status *output_status = (tdm_thread_cb_output_status*)base; - tdm_output *output_backend = - tdm_display_find_output_stamp(private_loop->dpy, output_status->output_stamp); - if (!output_backend) { - TDM_WRN("no output(%ld)", output_status->output_stamp); - break; - } - tdm_output_cb_status(output_backend, output_status->status, - output_status->user_data); - break; - } + case TDM_THREAD_CB_OUTPUT_DPMS: case TDM_THREAD_CB_PP_DONE: - { - tdm_thread_cb_pp_done *pp_done = (tdm_thread_cb_pp_done*)base; - tdm_pp *pp_backend = - tdm_pp_find_stamp(private_loop->dpy, pp_done->pp_stamp); - if (!pp_backend) { - TDM_WRN("no pp(%ld)", pp_done->pp_stamp); - break; - } - tdm_pp_cb_done(pp_backend, pp_done->src, pp_done->dst, pp_done->user_data); - break; - } case TDM_THREAD_CB_CAPTURE_DONE: - { - tdm_thread_cb_capture_done *capture_done = (tdm_thread_cb_capture_done*)base; - tdm_capture *capture_backend = - tdm_capture_find_stamp(private_loop->dpy, capture_done->capture_stamp); - if (!capture_backend) { - TDM_WRN("no capture(%ld)", capture_done->capture_stamp); - break; - } - tdm_capture_cb_done(capture_backend, capture_done->buffer, capture_done->user_data); + case TDM_THREAD_CB_VBLANK_SW: + case TDM_THREAD_CB_VBLANK_CREATE: + case TDM_THREAD_CB_HWC_COMMIT: + case TDM_THREAD_CB_VOUTPUT_COMMIT: + /* this event comes from other thread. so we don't need to propagate this to other thread */ + ret = tdm_thread_cb_call(NULL, base, 0); + TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE); + break; + case TDM_THREAD_CB_EXIT: + private_thread->event_thread_exit = 1; break; - } default: break; } i += base->length; } - tdm_event_loop_flush(private_loop->dpy); - return TDM_ERROR_NONE; } INTERN int -tdm_thread_in_display_thread(tdm_private_loop *private_loop, pid_t tid) +tdm_thread_in_display_thread(pid_t tid) { - tdm_private_thread *private_thread; + if (!keep_private_thread) + return 1; - TDM_RETURN_VAL_IF_FAIL(private_loop, 1); + /* DON'T check TDM_MUTEX_IS_LOCKED here */ - if (!private_loop->private_thread) - return 1; + return (keep_private_thread->display_tid == tid) ? 1 : 0; +} - private_thread = private_loop->private_thread; +INTERN int +tdm_thread_is_running(void) +{ + /* DON'T check TDM_MUTEX_IS_LOCKED here */ + + return (keep_private_thread && !keep_private_thread->event_thread_joined) ? 1 : 0; +} + +static void +_tdm_thread_free_cb(tdm_private_thread_cb *cb) +{ + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("cb(%p) removed", cb); + + assert(LIST_IS_EMPTY(&cb->call_link)); + + LIST_DEL(&cb->link); + free(cb); +} + +static tdm_private_thread_cb * +_tdm_thread_find_cb(struct list_head *list, void *object, tdm_thread_cb_type cb_type, + void *cb_data, tdm_thread_cb func, void *user_data) +{ + tdm_private_thread_cb *cb = NULL; + + LIST_FOR_EACH_ENTRY(cb, list, link) { + if (cb->object == object && + cb->cb_type == cb_type && + cb->cb_data == cb_data && + cb->func == func && + cb->user_data == user_data) + return cb; + } + + return NULL; +} + +INTERN void +tdm_thread_cb_set_find_func(tdm_thread_cb_type cb_type, tdm_thread_find_object func) +{ + TDM_RETURN_IF_FAIL(cb_type > 0); + + if (func && find_funcs[cb_type]) + TDM_NEVER_GET_HERE(); + + find_funcs[cb_type] = func; +} + +INTERN tdm_error +tdm_thread_cb_add(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data) +{ + tdm_private_thread_cb *cb = NULL; + pid_t caller_tid; + struct list_head *list; + + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); + TDM_RETURN_VAL_IF_FAIL(object != NULL, TDM_ERROR_INVALID_PARAMETER); + TDM_RETURN_VAL_IF_FAIL(cb_type > 0, TDM_ERROR_INVALID_PARAMETER); + TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER); + + caller_tid = syscall(SYS_gettid); + + pthread_mutex_lock(&cb_list_lock); + + if (tdm_thread_in_display_thread(caller_tid)) + list = &cb_list[0]; + else + list = &cb_list[1]; + + cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data); + if (cb) { + pthread_mutex_unlock(&cb_list_lock); + TDM_ERR("can't be added twice with same data"); + return TDM_ERROR_BAD_REQUEST; + } + + cb = calloc(1, sizeof *cb); + if (!cb) { + pthread_mutex_unlock(&cb_list_lock); + TDM_ERR("calloc failed"); + return TDM_ERROR_OUT_OF_MEMORY; + } + + LIST_ADDTAIL(&cb->link, list); + LIST_INITHEAD(&cb->call_link); + + cb->object = object; + cb->cb_type = cb_type; + cb->cb_data = cb_data; + cb->func = func; + cb->user_data = user_data; + cb->owner_tid = caller_tid; + + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("cb_type(%s) cb(%p) added", tdm_cb_type_str(cb_type), cb); + + pthread_mutex_unlock(&cb_list_lock); + + return TDM_ERROR_NONE; +} + +INTERN void +tdm_thread_cb_remove(void *object, tdm_thread_cb_type cb_type, void *cb_data, tdm_thread_cb func, void *user_data) +{ + tdm_private_thread_cb *cb; + struct list_head *list; + + TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED()); + TDM_RETURN_IF_FAIL(object != NULL); + TDM_RETURN_IF_FAIL(cb_type > 0); + TDM_RETURN_IF_FAIL(func != NULL); + + pthread_mutex_lock(&cb_list_lock); + + list = &cb_list[0]; + cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data); + if (cb) + _tdm_thread_free_cb(cb); + + list = &cb_list[1]; + cb = _tdm_thread_find_cb(list, object, cb_type, cb_data, func, user_data); + if (cb) + _tdm_thread_free_cb(cb); + + pthread_mutex_unlock(&cb_list_lock); +} + +/* when call a callback, we check both cb_base's type and cb_base's data, + * because a callback is added with cb_type and cb_data. + */ +INTERN tdm_error +tdm_thread_cb_call(void *object, tdm_thread_cb_base *cb_base, unsigned int propagation) +{ + tdm_private_display *private_display = tdm_display_get(); + tdm_private_thread_cb *cb = NULL, *hh = NULL; + int handler_in_other_thread = 0; + pid_t caller_tid; + struct list_head *list, *other_list; + struct list_head call_list; + static pid_t waiting_tid = 0; + tdm_error ret; + + TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), TDM_ERROR_OPERATION_FAILED); + TDM_RETURN_VAL_IF_FAIL(cb_base != NULL, TDM_ERROR_INVALID_PARAMETER); + TDM_RETURN_VAL_IF_FAIL(cb_base->type > 0, TDM_ERROR_INVALID_PARAMETER); + TDM_RETURN_VAL_IF_FAIL(cb_base->length > 0, TDM_ERROR_INVALID_PARAMETER); + TDM_RETURN_VAL_IF_FAIL(cb_base->object_stamp > 0, TDM_ERROR_INVALID_PARAMETER); + + caller_tid = syscall(SYS_gettid); + + assert(find_funcs[cb_base->type] != NULL); + + if (keep_private_thread && keep_private_thread->thread_tid != caller_tid) { + /* A sync-type event from display-thread to tdm-thread can't be handled. + * If sync-type events happen in both threads at the same time, + * it would make a deadlock issue. + */ + assert(cb_base->sync != 1); + } + + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------", + tdm_cb_type_str(cb_base->type), cb_base->sync, propagation); + + if (!object) { + object = find_funcs[cb_base->type](private_display, cb_base->object_stamp); + if (!object) { + TDM_WRN("%p gone", object); + return TDM_ERROR_NONE; + } + } + + pthread_mutex_lock(&cb_list_lock); + + if (tdm_thread_in_display_thread(caller_tid)) { + list = &cb_list[0]; + other_list = &cb_list[1]; + } else { + other_list = &cb_list[0]; + list = &cb_list[1]; + } + + LIST_INITHEAD(&call_list); + + LIST_FOR_EACH_ENTRY_SAFE(cb, hh, list, link) { + if (cb->object != object || + cb->cb_type != cb_base->type || + cb->cb_data != cb_base->data) + continue; + + LIST_ADDTAIL(&cb->call_link, &call_list); + } - return (private_thread->display_tid == tid) ? 1 : 0; + if (!LIST_IS_EMPTY(&call_list)) { + LIST_FOR_EACH_ENTRY_SAFE(cb, hh, &call_list, call_link) { + LIST_DELINIT(&cb->call_link); + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("cb_type(%s) cb(%p) calling", tdm_cb_type_str(cb->cb_type), cb); + pthread_mutex_unlock(&cb_list_lock); + cb->func(private_display, cb->object, cb_base, cb->user_data); + pthread_mutex_lock(&cb_list_lock); + } + } + + pthread_mutex_unlock(&cb_list_lock); + + if (propagation) { + LIST_FOR_EACH_ENTRY_SAFE(cb, hh, other_list, link) { + if (cb->object != object || + cb->cb_type != cb_base->type || + cb->cb_data != cb_base->data) + continue; + + handler_in_other_thread = 1; + break; + } + } + + if (!handler_in_other_thread) { + if (keep_private_thread) { + /* NOTE quick fix + * In case of 'cb_base->sync = 0' and 'waiting_tid != 0', + * probably it means one thread is waiting for signal of + * pthread condition. + */ + if (!cb_base->sync && waiting_tid != 0) { + waiting_tid = 0; + pthread_cond_signal(&keep_private_thread->event_cond); + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("pthread broadcase"); + } + } + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...", + tdm_cb_type_str(cb_base->type), cb_base->sync, propagation); + return TDM_ERROR_NONE; + } + + /* Once we reach here, it means that keep_private_thread is not NULL. + * Just make the crash. Avoiding it is not going to help us. + */ + assert(keep_private_thread != NULL); + + if (!cb_base->sync) { + ret = tdm_thread_send_cb(private_display->private_loop, cb_base); + TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED); + } + /* waiting until all cb are done in another thread */ + else { + /* NOTE quick fix + * Sync type event from display-thread(main thread) can't be + * handled. In this way, if we call tdm_thread_send_cb() with + * 'cb_base->sync = 1', then libtdm will eveventually raise + * abort(). Please see commit '4abfab36' for more detail. + */ + cb_base->sync = 0; + ret = tdm_thread_send_cb(private_display->private_loop, cb_base); + TDM_RETURN_VAL_IF_FAIL(ret == TDM_ERROR_NONE, TDM_ERROR_OPERATION_FAILED); + cb_base->sync = 1; + + /* if waiting_tid is not 0, it means there are two sync-type events at the same time. + * and it would make deadlock issue. + */ + assert(waiting_tid == 0); + + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("pthread wait"); + + /* pthread_cond_wait atomically release mutex, Upon successful return, + * the mutex shall have been locked and shall be owned by the calling thread + */ + tdm_mutex_locked = 0; + waiting_tid = caller_tid; + pthread_cond_wait(&keep_private_thread->event_cond, &private_display->lock); + tdm_mutex_locked = 1; + } + + if (tdm_debug_module & TDM_DEBUG_THREAD) + TDM_INFO("'%s' thread_cb (sync:%d, propagation:%d) ------...", + tdm_cb_type_str(cb_base->type), cb_base->sync, propagation); + + return TDM_ERROR_NONE; }