common : event : separate event to global and local 34/141934/2 accepted/tizen/4.0/unified/20170816.013428 accepted/tizen/4.0/unified/20170829.020114 accepted/tizen/unified/20170803.155436 submit/tizen/20170803.023017 submit/tizen_4.0/20170811.094300 submit/tizen_4.0/20170828.100004 submit/tizen_4.0/20170828.110004 tizen_4.0.IoT.p1_release
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 2 Aug 2017 04:53:58 +0000 (13:53 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Wed, 2 Aug 2017 06:04:33 +0000 (15:04 +0900)
- Global event is for all modules
  - Define at the common module
  - Have unique value
  - Can be broadcasted or sent

- Local event is for specific module
  - Define at the target module
  - Can have overlapped value between modules
  - Only can be sent

Change-Id: I54c6424ecaad7a3306ed2705e015b8e982da1281
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/common/event.c
src/common/event.h
src/proc-usage/proc-usage-dbus.c
src/proc-usage/proc-usage-dbus.h
src/proc-usage/proc-usage.c
src/proc-usage/proc-usage.h [new file with mode: 0644]
src/test/test.c

index 813fb2e2ff11815222be7448f8e0a84cb811183a..c8327ef41887132f05fde4835b9b35a731be80f6 100644 (file)
@@ -32,7 +32,7 @@ static GCond event_worker_thread_cond;
 static GMutex event_worker_thread_mutex;
 static int ewt_var_initialized;
 
-API int event_send(char *dest, enum event_type type, void *data)
+API int event_send(char *dest, event_type type, void *data)
 {
        struct module *module;
        struct event *event;
@@ -77,9 +77,16 @@ static void event_push(struct module *module, void *user_data)
        g_main_context_wakeup(module->event_worker->context);
 }
 
-API int event_broadcast(enum event_type type, void *data)
+API int event_broadcast(event_type type, void *data)
 {
-       struct event *event = g_slice_new(struct event);
+       struct event *event;
+
+       if (type >= GLOBAL_EVENT_MAX) {
+               _E("Only global event can be broadcasted");
+               return -EINVAL;
+       }
+
+       event = g_slice_new(struct event);
        if (!event) {
                _E("Not enough memory");
                return -ENOMEM;
index 2e6708fd61b227283f8f4d539dbf9be22bb17f0c..1a9c1f1b82f449154800c4140690f4a07f263653 100644 (file)
 
 /**
  * @brief  Event list handled by ResourceD-HeadLess
+ * @details  Global event is for all modules. It can be broadcasted or sent
+ *           to the target module.
+ *           On the other hand, local event is only available in
+ *           the defining module, so it should not be broadcasted.
+ *           Global event must be unique, but local event can be overlapped
+ *           between modules.
+ * @notice  Define local event in the target module.
  */
 enum event_type {
-       RDHL_EVENT_TEST = 0,    /* For testing. It will be removed */
-
-       /* Resource usage */
-       RDHL_EVENT_PROCESS_MEMORY_USAGE,
-       RDHL_EVENT_PROCESS_CPU_USAGE,
+       GLOBAL_EVENT_NONE = 0,
+       GLOBAL_EVENT_MAX  = 0xffff,
+       LOCAL_EVENT_MIN,
+       LOCAL_EVENT_MAX   = 0xffffffff,
 };
 
+typedef unsigned int event_type;
 typedef GSourceFunc event_handler_func;
 
 /**
@@ -46,7 +53,7 @@ typedef GSourceFunc event_handler_func;
  */
 struct event {
        char *dest;
-       enum event_type type;
+       event_type type;
        void *data;
        gint ref_count;
 };
@@ -70,15 +77,15 @@ struct event_worker {
  * @param[in] data  Delivered data
  * @return  0 on success, otherwise a negative error value
  */
-int event_send(char *dest, enum event_type type, void *data);
+int event_send(char *dest, event_type type, void *data);
 
 /**
- * @brief  Broadcast event to all modules
+ * @brief  Broadcast global event to all modules
  * @param[in] type  Event type
  * @param[in] data  Delivered data
  * @return  0 on success, otherwise a negative error value
  */
-int event_broadcast(enum event_type type, void *data);
+int event_broadcast(event_type type, void *data);
 
 /**
  * @brief  Register event worker
index 4e113ecc0b32c8f2f941d1ef941d7089b1608791..ae678a8eaf27765d651dc70d174c84ad02d27d00 100644 (file)
 #include <log.h>
 #include <macro.h>
 
+#include "proc-usage.h"
 #include "proc-usage-dbus.h"
 
-static void proc_usage_dbus_push_event(enum event_type event_type,
+static void proc_usage_dbus_push_event(event_type event_type,
                GDBusMethodInvocation *invocation, GVariant *params)
 {
        int ret;
@@ -58,12 +59,12 @@ error:
 
 static void proc_usage_dbus_memory_usage(GDBusMethodInvocation *invocation, GVariant *params)
 {
-       proc_usage_dbus_push_event(RDHL_EVENT_PROCESS_MEMORY_USAGE, invocation, params);
+       proc_usage_dbus_push_event(PROC_USAGE_EVENT_PROCESS_MEMORY_USAGE, invocation, params);
 }
 
 static void proc_usage_dbus_cpu_usage(GDBusMethodInvocation *invocation, GVariant *params)
 {
-       proc_usage_dbus_push_event(RDHL_EVENT_PROCESS_CPU_USAGE, invocation, params);
+       proc_usage_dbus_push_event(PROC_USAGE_EVENT_PROCESS_CPU_USAGE, invocation, params);
 }
 
 static struct dbus_method proc_usage_dbus_methods[] = {
index baab6638f3b282a0c009834f2a635e79da29c2f4..fcc81d8f13c93378a4f3f3eb3cca15a149ec7368 100644 (file)
 #define RDHL_DBUS_PATH_PROC                    RDHL_DBUS_PATH(Process)
 #define RDHL_DBUS_INTERFACE_PROC       RDHL_DBUS_INTERFACE(process)
 
-/**
- * @brief  Argument pushed in the proc-usage worker queue
- */
-struct proc_usage_event {
-       GDBusMethodInvocation *invocation;
-       GVariant *params;
-};
-
 int proc_usage_dbus_init(void);
 
 #endif /* __RESOURCED_HEADLESS_PROC_USAGE_DBUS_H__ */
index e0dfbfa6a213957c8e4ca235360e7d71bb2b0071..a0542e8ba3c0049f10ab6832c799546c0e5b2213 100644 (file)
@@ -24,6 +24,7 @@
 #include <macro.h>
 #include <module.h>
 
+#include "proc-usage.h"
 #include "proc-usage-dbus.h"
 #include "proc-usage-process.h"
 
@@ -39,10 +40,10 @@ static gboolean proc_usage_event_func(gpointer user_data)
        }
 
        switch (event->type) {
-       case RDHL_EVENT_PROCESS_MEMORY_USAGE:
+       case PROC_USAGE_EVENT_PROCESS_MEMORY_USAGE:
                usage = proc_usage_process_get_memory_usages(pue->params);
                break;
-       case RDHL_EVENT_PROCESS_CPU_USAGE:
+       case PROC_USAGE_EVENT_PROCESS_CPU_USAGE:
                usage = proc_usage_process_get_cpu_usages(pue->params);
                break;
        default:
diff --git a/src/proc-usage/proc-usage.h b/src/proc-usage/proc-usage.h
new file mode 100644 (file)
index 0000000..c43faad
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * resourced-headless
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file  proc-usage.h
+ * @brief  Proc-usage module and its event handler
+ */
+
+#ifndef __RESOURCED_HEADLESS_PROC_USAGE_H__
+#define __RESOURCED_HEADLESS_PROC_USAGE_H__
+
+#include <gio/gio.h>
+#include <glib.h>
+
+/**
+ * @brief  Local event for proc-usage module
+ */
+enum proc_usage_event_type {
+       PROC_USAGE_EVENT_PROCESS_MEMORY_USAGE = LOCAL_EVENT_MIN,
+       PROC_USAGE_EVENT_PROCESS_CPU_USAGE,
+};
+
+/**
+ * @brief  Argument pushed in the proc-usage worker queue
+ */
+struct proc_usage_event {
+       GDBusMethodInvocation *invocation;
+       GVariant *params;
+};
+
+#endif /* __RESOURCED_HEADLESS_PROC_USAGE_H__ */
index 9334b3357939c9c2caf1f3606cb191b348191544..cbcab2c508da39f35fed505904a89d61c3cc2130 100644 (file)
 /* Wait 1 second from now */
 #define TIME_TO_WAIT_EVENT                     (g_get_monotonic_time() + 1000000)
 
+/**
+ * @brief  Local event for test module
+ */
+enum test_event_type {
+       TEST_EVENT_GET_VALUE = 170802,
+};
+
 static GCond test_worker_thread_cond;
 static GMutex test_worker_thread_mutex;
 
@@ -40,7 +47,7 @@ gboolean test_event_handler(gpointer user_data)
        struct event *event = (struct event *)user_data;
        int *arg = (int *)event->data;
 
-       if (event->type == RDHL_EVENT_TEST) {
+       if (event->type == TEST_EVENT_GET_VALUE) {
                _I("Test module's event (type = %d) callback is called. Receive %d",
                                event->type, *arg);
                g_cond_signal(&test_worker_thread_cond);
@@ -80,25 +87,18 @@ void test_event(GDBusMethodInvocation *invocation, GVariant *params)
        g_assert(arg_broadcast && arg_send);
 
        /* Broadcast event */
-       g_mutex_lock(&test_worker_thread_mutex);
        *arg_broadcast = 623;
-       _I("Broadcast event (arg = %d)", *arg_broadcast);
-       if (event_broadcast(RDHL_EVENT_TEST, arg_broadcast) < 0) {
+       _I("Broadcast local event (arg = %d). It should be failed", *arg_broadcast);
+       if (event_broadcast(TEST_EVENT_GET_VALUE, arg_broadcast) == 0) {
                err_code = G_DBUS_ERROR_FAILED;
                goto err;
        }
-       if (!g_cond_wait_until(&test_worker_thread_cond, &test_worker_thread_mutex,
-                                               TIME_TO_WAIT_EVENT)) {
-               err_code = G_DBUS_ERROR_TIMEOUT;
-               goto err;
-       }
-       g_mutex_unlock(&test_worker_thread_mutex);
 
        /* Send event */
        g_mutex_lock(&test_worker_thread_mutex);
        *arg_send = 731;
-       _I("Send event to the test module (arg = %d)", *arg_send);
-       if (event_send("test", RDHL_EVENT_TEST, arg_send) < 0) {
+       _I("Send local event to the test module (arg = %d)", *arg_send);
+       if (event_send("test", TEST_EVENT_GET_VALUE, arg_send) < 0) {
                err_code = G_DBUS_ERROR_FAILED;
                goto err;
        }