devices: replace priority enum to integer 06/262306/1
authorYoungjae Cho <y0.cho@samsung.com>
Fri, 6 Aug 2021 07:22:22 +0000 (16:22 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Fri, 6 Aug 2021 07:28:41 +0000 (16:28 +0900)
Previously, there were only two priorities of enum device_priority,
so it wasn't possible to fine-tune the initialization order.
Replace this enum to integer and sorts devices with this integer.
Higher integer will be initialized first.

Change-Id: I90183d86159cca6ad00afa3a92dbadd3bab6751a
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/core/devices.c
src/core/devices.h

index 30ddbe6..3470781 100644 (file)
@@ -36,10 +36,7 @@ GList *get_device_list_head(void)
 
 void add_device(const struct device_ops *dev)
 {
-       if (dev->priority == DEVICE_PRIORITY_HIGH)
-               SYS_G_LIST_PREPEND(dev_head, dev);
-       else
-               SYS_G_LIST_APPEND(dev_head, dev);
+       SYS_G_LIST_APPEND(dev_head, dev);
 }
 
 void remove_device(const struct device_ops *dev)
@@ -105,12 +102,21 @@ static const dbus_interface_u dbus_interface = {
        .nr_methods = ARRAY_SIZE(dbus_methods),
 };
 
+static gint compare_priority(gconstpointer a, gconstpointer b)
+{
+       /* decreasing order
+        * high priority number will be initialized first */
+       return ((const struct device_ops *)b)->priority - ((const struct device_ops *)a)->priority;
+}
+
 void devices_init(void *data)
 {
        GList *elem, *elem_n;
        const struct device_ops *dev;
        int ret;
 
+       dev_head = g_list_sort(dev_head, compare_priority);
+
        SYS_G_LIST_FOREACH_SAFE(dev_head, elem, elem_n, dev) {
                if (dev->probe && dev->probe(data) != 0) {
                        _E("[%s] Failed to probe.", dev->name);
index e1a149d..4375edb 100644 (file)
 
 #include "shared/common.h"
 
-enum device_priority {
-       DEVICE_PRIORITY_NORMAL = 0,
-       DEVICE_PRIORITY_HIGH,
-};
+#define DEVICE_PRIORITY_NORMAL    0
+#define DEVICE_PRIORITY_HIGH      1
 
 enum device_flags {
        NORMAL_MODE                   = 0x00000001,
@@ -55,7 +53,7 @@ enum device_flags {
 };
 
 struct device_ops {
-       enum device_priority priority;
+       int priority; /* high number will be initialized first */
        char *name;
        int len;
        int (*probe) (void *data);