+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;
+ pid_t caller_tid;
+ 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);
+
+ 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, caller_tid);
+ if (!cb) {
+ pthread_mutex_unlock(&cb_list_lock);
+ return;
+ }
+
+ _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)
+{
+ 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;
+ 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->sync == 0 || cb_base->sync == 1, 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 (!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);
+ }
+
+ 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);
+
+ assert(LIST_IS_EMPTY(&call_list));
+
+ 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) {
+ if (cb_base->sync) {
+ 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 done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
+ 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.
+ */
+ 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 */
+ if (cb_base->sync) {
+ 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;
+ 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 done(sync:%d)", tdm_cb_type_str(cb_base->type), cb_base->sync);
+
+ return TDM_ERROR_NONE;