e_input: generalize boost mutex function and move it to e_input 82/303382/1
authorJoonbum Ko <joonbum.ko@samsung.com>
Tue, 19 Dec 2023 04:20:09 +0000 (13:20 +0900)
committerTizen Window System <tizen.windowsystem@gmail.com>
Wed, 27 Dec 2023 02:01:31 +0000 (11:01 +0900)
 - Moved the implementation of e_comp_wl_connection_lock/unlcok to e_input
 - It was modified to receive GMutex* as a parameter so that boosting
  can be called for other mutexes.
 - If the input thread ID set, save the thread_id using pthread_setspecific.
 - If the return value of pthread_getspecific is NULL,
  it is determined that it is not input thread, and it will not call
  boosting for main thread when trylock failed.

Change-Id: I19151d9248ec31723d45ebd422eb8dc9dc67e9fa
Signed-off-by: Joonbum Ko <joonbum.ko@samsung.com>
src/bin/e_comp_wl.c
src/bin/e_comp_wl.h
src/bin/e_input.c
src/bin/e_input.h

index 89b387c..6b86ff3 100644 (file)
@@ -13,8 +13,6 @@
 #include <libds/log.h>
 #include <libds/single_pixel_buffer_v1.h>
 
-#include <cpu-boosting.h>
-
 /* handle include for printing uint64_t */
 #define __STDC_FORMAT_MACROS
 #include <inttypes.h>
@@ -53,7 +51,7 @@ static E_Client *cursor_timer_ec = NULL;
 static Eina_Bool need_send_released = EINA_FALSE;
 static Eina_Bool need_send_motion = EINA_TRUE;
 
-static GMutex flush_mutex;
+static GMutex connection_mutex;
 
 static int _e_comp_wl_hooks_delete = 0;
 static int _e_comp_wl_hooks_walking = 0;
@@ -269,9 +267,9 @@ e_comp_wl_display_flush()
              ELOGF("INPUT", "wl_display_flush_clients|B|", NULL);
           }
 
-        g_mutex_lock(&flush_mutex);
+        g_mutex_lock(&connection_mutex);
         wl_display_flush_clients(e_comp_wl->wl.disp);
-        g_mutex_unlock(&flush_mutex);
+        g_mutex_unlock(&connection_mutex);
 
         if (e_config->key_input_ttrace_enable)
           {
@@ -281,51 +279,16 @@ e_comp_wl_display_flush()
      }
 }
 
-void
-_e_comp_wl_main_thread_set_boosting()
+EINTERN void
+e_comp_wl_connection_lock()
 {
-   pid_t main_thread_id = e_input_main_thread_id_get();
-   if (main_thread_id <= 0)
-     {
-        ERR("Failed to get main thread id");
-        return;
-     }
-
-   TRACE_INPUT_BEGIN("_e_comp_wl_main_thread_set_boosting");
-   resource_pid_t resource;
-   resource.pid = 0;
-   resource.tid = &main_thread_id;
-   resource.tid_count = 1;
-
-   int ret = resource_set_cpu_boosting(resource, CPU_BOOSTING_LEVEL_STRONG, CPU_BOOSTING_RESET_ON_FORK, -1);
-   TRACE_INPUT_END();
-
-   if (ret != 0)
-     ERR("set_cpu_boosting failed. error %d", ret);
+   e_input_boost_lock(&connection_mutex);
 }
 
-void
-_e_comp_wl_main_thread_clear_boosting()
+EINTERN void
+e_comp_wl_connection_unlock()
 {
-   pid_t main_thread_id = e_input_main_thread_id_get();
-   if (main_thread_id <= 0)
-     {
-        ERR("Failed to get main thread id");
-        return;
-     }
-
-   TRACE_INPUT_BEGIN("_e_comp_wl_main_thread_clear_boosting");
-   resource_pid_t resource;
-   resource.pid = 0;
-   resource.tid = &main_thread_id;
-   resource.tid_count = 1;
-
-   int ret = resource_clear_cpu_boosting(resource);
-   TRACE_INPUT_END();
-
-   if (ret != 0)
-     ERR("clear_cpu_boosting failed. error %d", ret);
-
+   e_input_boost_unlock(&connection_mutex);
 }
 
 void
@@ -334,14 +297,7 @@ e_comp_wl_focused_client_flush()
    E_Client *focused_ec;
    if (!(e_comp_wl && e_comp_wl->wl.disp)) return;
 
-   if (!g_mutex_trylock(&flush_mutex))
-     {
-        TRACE_INPUT_BEGIN("flush_mutex lock");
-        _e_comp_wl_main_thread_set_boosting();
-        g_mutex_lock(&flush_mutex);
-        _e_comp_wl_main_thread_clear_boosting();
-        TRACE_INPUT_END();
-     }
+   e_input_boost_lock(&connection_mutex);
    focused_ec = e_client_focused_get();
 
    if (focused_ec)
@@ -350,7 +306,7 @@ e_comp_wl_focused_client_flush()
         struct wl_resource *surface = e_comp_wl_client_surface_get(focused_ec);
         if (!surface)
           {
-            g_mutex_unlock(&flush_mutex);
+            e_input_boost_unlock(&connection_mutex);
             return;
           }
 
@@ -358,7 +314,7 @@ e_comp_wl_focused_client_flush()
         wl_client_flush(wc);
      }
 
-   g_mutex_unlock(&flush_mutex);
+   e_input_boost_unlock(&connection_mutex);
 }
 
 static void
@@ -3823,7 +3779,7 @@ _e_comp_wl_display_create(void)
    /* set compositor wayland data */
    e_comp_wl = e_comp->wl_comp_data = wl_cdata;
 
-   g_mutex_init(&flush_mutex);
+   g_mutex_init(&connection_mutex);
 
    /* try to create a wayland display */
    if (!(wl_cdata->wl.disp = wl_display_create()))
@@ -3915,7 +3871,7 @@ comp_err:
 sock_err:
    wl_display_destroy(wl_cdata->wl.disp);
 disp_err:
-   g_mutex_clear(&flush_mutex);
+   g_mutex_clear(&connection_mutex);
    free(comp);
    return EINA_FALSE;
 }
index f4fd95a..18b7e5c 100644 (file)
@@ -675,6 +675,9 @@ EINTERN void e_comp_wl_client_surface_set(E_Client *ec, struct wl_resource *surf
 EINTERN void    e_comp_wl_display_flush();
 EINTERN void    e_comp_wl_focused_client_flush();
 
+EINTERN void    e_comp_wl_connection_lock();
+EINTERN void    e_comp_wl_connection_unlock();
+
 EINTERN E_Client *e_comp_wl_util_client_from_surface_resource(struct wl_resource *surface_resource);
 
 # endif
index 5669905..93f908f 100644 (file)
@@ -2,11 +2,17 @@
 #include "e_input_private.h"
 #include <Ecore_Input_Evas.h>
 
+#include <cpu-boosting.h>
+#include <pthread.h>
+
 static int _e_input_hooks_delete = 0;
 static int _e_input_hooks_walking = 0;
 
 static pid_t _e_input_thread_id = 0;
 static pid_t _e_input_main_thread_id = 0;
+static pthread_key_t thread_id_key = 0;
+static pthread_once_t once_init = PTHREAD_ONCE_INIT;
+static int _e_input_thread_error = 0;
 
 static Eina_Inlist *_e_input_hooks[] =
 {
@@ -397,9 +403,30 @@ E_API pid_t e_input_thread_id_get()
    return _e_input_thread_id;
 }
 
+void
+e_input_init_thread()
+{
+   int ret = pthread_key_create(&thread_id_key, NULL);
+   if (ret < 0)
+     {
+        ERR("failed to create pthread_key");
+     }
+
+   _e_input_thread_error = ret;
+}
+
 EINTERN void e_input_thread_id_set(pid_t tid)
 {
    _e_input_thread_id = tid;
+
+   pthread_once(&once_init, e_input_init_thread);
+   int ret = pthread_setspecific(thread_id_key, &_e_input_thread_id);
+   if (ret < 0)
+     {
+        ERR("failed to set thread specific");
+     }
+
+   _e_input_thread_error = ret;
 }
 
 E_API pid_t e_input_main_thread_id_get()
@@ -411,3 +438,71 @@ EINTERN void e_input_main_thread_id_set(pid_t tid)
 {
    _e_input_main_thread_id = tid;
 }
+
+void
+_e_input_main_thread_set_boosting()
+{
+   if (!pthread_getspecific(thread_id_key))
+     return;
+
+   pid_t main_thread_id = e_input_main_thread_id_get();
+   if (main_thread_id <= 0)
+     {
+        ERR("Failed to get main thread id");
+        return;
+     }
+
+   TRACE_INPUT_BEGIN(_e_input_main_thread_set_boosting);
+   resource_pid_t resource;
+   resource.pid = 0;
+   resource.tid = &main_thread_id;
+   resource.tid_count = 1;
+
+   int ret = resource_set_cpu_boosting(resource, CPU_BOOSTING_LEVEL_STRONG, CPU_BOOSTING_RESET_ON_FORK, -1);
+   TRACE_INPUT_END();
+
+   if (ret != 0)
+     ERR("set_cpu_boosting failed. error %d", ret);
+}
+
+void
+_e_input_main_thread_clear_boosting()
+{
+   if (!pthread_getspecific(thread_id_key))
+     return;
+
+   pid_t main_thread_id = e_input_main_thread_id_get();
+   if (main_thread_id <= 0)
+     {
+        ERR("Failed to get main thread id");
+        return;
+     }
+
+   TRACE_INPUT_BEGIN(_e_input_main_thread_clear_boosting);
+   resource_pid_t resource;
+   resource.pid = 0;
+   resource.tid = &main_thread_id;
+   resource.tid_count = 1;
+
+   int ret = resource_clear_cpu_boosting(resource);
+   TRACE_INPUT_END();
+
+   if (ret != 0)
+     ERR("clear_cpu_boosting failed. error %d", ret);
+
+}
+
+EINTERN void e_input_boost_lock(GMutex *mutex)
+{
+   if (!g_mutex_trylock(mutex))
+     {
+        _e_input_main_thread_set_boosting();
+        g_mutex_lock(mutex);
+        _e_input_main_thread_clear_boosting();
+     }
+}
+
+EINTERN void e_input_boost_unlock(GMutex *mutex)
+{
+   g_mutex_unlock(mutex);
+}
index c9194f2..b430f21 100644 (file)
@@ -12,6 +12,8 @@ typedef struct _E_Input E_Input;
 #define E_INPUT_REQUEST_SERVER ((void *)0x1)
 
 #include <xkbcommon/xkbcommon.h>
+#include <glib.h>
+
 #include "e_device.h"
 #include "e_input_event.h"
 
@@ -176,6 +178,9 @@ EINTERN void e_input_thread_id_set(pid_t tid);
 E_API pid_t e_input_main_thread_id_get();
 EINTERN void e_input_main_thread_id_set(pid_t tid);
 
+EINTERN void e_input_boost_lock(GMutex *mutex);
+EINTERN void e_input_boost_unlock(GMutex *mutex);
+
 EINTERN void e_input_thread_run(E_Input_Backend *input);
 EINTERN Eina_Bool e_input_libinput_context_create(E_Input_Backend *input);