pass: resmon: Replace timer-related code with timer utility 85/269985/6
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 20 Jan 2022 09:11:26 +0000 (18:11 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 24 Jan 2022 09:23:58 +0000 (18:23 +0900)
Change-Id: I30d9f3a8cc37036bb37c350565c6ef2c58f2bdba
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pass/pass-resmon-internal.h
src/pass/pass-resmon.c

index 82f5bbf..011bb81 100644 (file)
@@ -125,11 +125,6 @@ struct resmon {
         * uevent-based resource monitor.
         */
        struct udev_monitor *udev_monitor;
-       /**
-        * File descriptor for udev device. It will be only used for
-        * uevent-based resource monitor.
-        */
-       guint udev_monitor_fd;
 };
 
 #endif /* __PASS_RESMON_INTERNAL__ */
index a445a7e..2c69bd0 100644 (file)
  * @ingroup    COM_POWER_MGNT
  */
 
-#include <glib-unix.h>
-#include <libudev.h>
-
 #include <util/log.h>
+#include <util/timer.h>
 
 #include "pass.h"
 #include "pass-hal.h"
@@ -105,42 +103,53 @@ static int resmon_timer_add(struct resmon *monitor, unsigned int interval)
        struct pass_resmon *resmon = monitor->resmon;
        int ret;
 
-       /* Add new timer-based resmon to timer_list */
-       if (!resmon_find_monitor(resmon, RESMON_TIMER, monitor->id)) {
-               monitor->id = g_timeout_add((guint)interval,
-                                       (GSourceFunc)resmon_timer_func,
-                                       (gpointer)monitor);
-               if (!monitor->id)
-                       return -EPERM;
-               monitor->timer_interval = interval;
-
-               if (monitor->ops && monitor->ops->init) {
-                       ret = monitor->ops->init(monitor);
-                       if (ret < 0) {
-                               g_source_remove(monitor->id);
-                               monitor->timer_interval = 0;
-                               return ret;
-                       }
+       if (resmon_find_monitor(resmon, RESMON_TIMER, monitor->id))
+               return -EINVAL;
+
+       monitor->id = add_timer_handler(interval, resmon_timer_func, monitor);
+       if (!monitor->id)
+               return -EPERM;
+       monitor->timer_interval = interval;
+
+       if (monitor->ops && monitor->ops->init) {
+               ret = monitor->ops->init(monitor);
+               if (ret < 0) {
+                       delete_timer_handler(monitor->id);
+                       monitor->timer_interval = 0;
+                       return ret;
                }
+       }
 
-               resmon->timer_list = g_list_append(resmon->timer_list,
-                                               (gpointer)monitor);
+       resmon->timer_list = g_list_append(resmon->timer_list, (gpointer)monitor);
 
-       /* Update timer interval of the registered timer-based resmon */
-       } else {
-               g_source_remove(monitor->id);
-               monitor->timer_interval = 0;
+       return monitor->id;
+}
 
-               /* Update timer interval of timer-based resmon */
-               monitor->id = g_timeout_add((guint)interval,
-                                       (GSourceFunc)resmon_timer_func,
-                                       (gpointer)monitor);
-               if (!monitor->id)
-                       return -EPERM;
-               monitor->timer_interval = interval;
+/**
+ * @brief      Update the interval of a timer-based resource monitor.
+ * @param      [in] monitor Instance of a resource monitor
+ * @param      [in] interval New interval of timer-based resource monitor
+ * @return     @c 0 on success, otherwise error value
+ */
+static int resmon_timer_update_interval(struct resmon *monitor,
+                                       unsigned int interval)
+{
+       struct pass_resmon *resmon = monitor->resmon;
+
+       if (!resmon_find_monitor(resmon, RESMON_TIMER, monitor->id))
+               return -EINVAL;
+
+       /* Update timer interval of timer-based resmon */
+       delete_timer_handler(monitor->id);
+
+       monitor->id = add_timer_handler(interval, resmon_timer_func, monitor);
+       if (!monitor->id) {
+               _E("failed to update interval of timer (id:%d)\n\n", monitor->id);
+               return -EPERM;
        }
+       monitor->timer_interval = interval;
 
-       return monitor->id;
+       return 0;
 }
 
 /**
@@ -153,7 +162,7 @@ static int resmon_timer_delete(struct resmon *monitor)
        struct pass_resmon *resmon = monitor->resmon;
        int ret;
 
-       g_source_remove(monitor->id);
+       delete_timer_handler(monitor->id);
 
        if (monitor->ops && monitor->ops->exit) {
                ret = monitor->ops->exit(monitor);
@@ -361,7 +370,7 @@ int pass_resmon_update_timer_interval(struct pass_resource *res,
                return 0;
 
        /* Update new interval of timer-based resmon */
-       ret = resmon_timer_add(monitor, timer_interval);
+       ret = resmon_timer_update_interval(monitor, timer_interval);
        if (ret < 0)
                return -EINVAL;
 
@@ -429,84 +438,24 @@ static int resmon_uevent_add(struct resmon *monitor)
        struct pass_resmon *resmon = monitor->resmon;
        struct pass_resource *res
                = container_of(resmon, struct pass_resource, resmon);;
-       guint gfd;
        int ret;
-       int fd;
-       int i;
 
        if (!monitor->ops) {
                _E("failed to add the udev-monitor\n");
                return -EINVAL;
        }
 
-       /* Initialize the udev-monitor instance and set buffer size */
-       udev_monitor = udev_monitor_new_from_netlink(g_udev, "kernel");
+       udev_monitor = add_uevent_handler(monitor->ops->uevent_subsystem,
+                                       monitor->ops->uevent_devtype,
+                                       monitor->ops->number_of_uevent,
+                                       resmon_uevent_func,
+                                       (void *)monitor);
        if (!udev_monitor) {
-               _E("failed to create the udev monitor " \
-                               "(res_name:%s, src_type: 0x%x)\n",
-                               res->config_data.res_name, monitor->src_type);
+               _E("failed to add uevent handler\n");
                return -EINVAL;
        }
 
        /*
-        * Number of uevent subsystem must be more than 0 at least.
-        */
-       if (monitor->ops->number_of_uevent <= 0) {
-               _E("failed to add filter due to subsystem/devtype are NULL " \
-                               "(res_name:%s, src_type: 0x%x)\n",
-                               res->config_data.res_name, monitor->src_type);
-               ret = -EINVAL;
-               goto err_udev_monitor;
-       }
-
-       /* Update the kernel's subsystem and devtype for filtering */
-       for (i = 0; i < monitor->ops->number_of_uevent; i++) {
-               ret = udev_monitor_filter_add_match_subsystem_devtype(
-                                               udev_monitor,
-                                               monitor->ops->uevent_subsystem[i],
-                                               monitor->ops->uevent_devtype[i]);
-               if (ret < 0) {
-                       _E("failed to add filter with subsystem(%s)/devtype(%s) " \
-                                       "(res_name:%s, src_type: 0x%x)\n",
-                                       monitor->ops->uevent_subsystem[i],
-                                       monitor->ops->uevent_devtype[i],
-                                       res->config_data.res_name, monitor->src_type);
-                       goto err_udev_monitor;
-               }
-       }
-
-       ret = udev_monitor_filter_update(udev_monitor);
-       if (ret < 0) {
-               for (i = 0; i < monitor->ops->number_of_uevent; i++) {
-                       _E("failed to update filter with subsystem(%s)/devtype(%s) " \
-                               "(res_name:%s, src_type: 0x%x)\n",
-                               monitor->ops->uevent_subsystem[i],
-                               monitor->ops->uevent_devtype[i],
-                               res->config_data.res_name, monitor->src_type);
-               }
-               goto err_udev_monitor;
-       }
-
-       /* Register callback to subscribe the event */
-       fd = udev_monitor_get_fd(udev_monitor);
-       if (fd <= 0) {
-               _E("failed to get file descriptor of udev monitor " \
-                               "(res_name:%s, src_type: 0x%x)\n",
-                               res->config_data.res_name, monitor->src_type);
-               ret = -EINVAL;
-               goto err_udev_monitor;
-       }
-
-       gfd = g_unix_fd_add(fd, G_IO_IN, resmon_uevent_func, monitor);
-       if (gfd <= 0) {
-               _E("failed to add uevent-based callback function " \
-                               "(res_name:%s, src_type: 0x%x)\n",
-                               res->config_data.res_name, monitor->src_type);
-               ret = -EINVAL;
-               goto err_udev_monitor;
-       }
-
-       /*
         * Invoke the .init of each RESMON_SRC_* source
         * before enabling the udev monitoring.
         */
@@ -520,30 +469,16 @@ static int resmon_uevent_add(struct resmon *monitor)
                }
        }
 
-       /* Bind udev-monitor to enable the udev monitoring */
-       ret = udev_monitor_enable_receiving(udev_monitor);
-       if (ret < 0) {
-               _E("failed to bind udev monitor for receiving the event " \
-                               "(res_name:%s, src_type: 0x%x)\n",
-                               res->config_data.res_name, monitor->src_type);
-               ret = -EAGAIN;
-               goto err_udev_monitor_fd;
-       }
-
        /* Add new uevent-based resmon to uevent_list */
        resmon->uevent_list = g_list_append(resmon->uevent_list,
                                                (gpointer)monitor);
 
        monitor->udev_monitor = udev_monitor;;
-       monitor->udev_monitor_fd = gfd;
        monitor->id = ++uevent_id;
 
        return 0;
 
 err_udev_monitor_fd:
-       g_source_remove(gfd);
-       gfd = -1;
-err_udev_monitor:
        udev_monitor_unref(udev_monitor);
        udev_monitor = NULL;
 
@@ -573,10 +508,8 @@ static int resmon_uevent_delete(struct resmon *monitor)
                }
        }
 
-       udev_monitor_unref(monitor->udev_monitor);
-       g_source_remove(monitor->udev_monitor_fd);
+       delete_uevent_handler(monitor->udev_monitor);
        monitor->udev_monitor = NULL;
-       monitor->udev_monitor_fd = -1;
 
        /* Delete the uevent-based resmon from uevent_list */
        resmon->uevent_list = g_list_remove(resmon->uevent_list,