worker: implementation 25/181025/10 per-core
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Thu, 7 Jun 2018 10:07:08 +0000 (12:07 +0200)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Fri, 15 Jun 2018 13:09:03 +0000 (15:09 +0200)
Change-Id: I5e3a5ac2897643fa332b69676681d00e0a968932
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
packaging/task-worker.spec
src/CMakeLists.txt
src/err-check.h
src/task.h
src/worker.c
src/worker.h

index 74d0131..f694ee0 100644 (file)
@@ -10,7 +10,9 @@ BuildRequires:  cmake
 BuildRequires:  hash-signer
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(json-glib-1.0)
 BuildRequires:  pkgconfig(capi-appfw-service-application)
+BuildRequires:  pkgconfig(glib-2.0)
 
 %description
 Process and Memory Tizen Things Daemon Worker
index c733536..2acb38c 100644 (file)
@@ -1,5 +1,6 @@
 INCLUDE(FindPkgConfig)
 pkg_check_modules(APP_PKGS REQUIRED
+       glib-2.0
        dlog
 )
 
@@ -16,6 +17,7 @@ SET(SRCS
        procfs.c
        report-generator.c
        report-json-serializer.c
+       worker.c
 )
 ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
 
index 2ee8168..ddf2fd7 100644 (file)
        }\
 } while (0)
 
-#endif
+#define ON_NULL_RETURN(x) do { \
+       if ((x) == NULL) {\
+               ERR(#x" is NULL");\
+               return;\
+       }\
+} while (0)
 
+#define ON_TRUE_RETURN(cond) do { \
+       if ((cond)) {\
+               ERR("condition ("#cond") is true");\
+               return;\
+       }\
+} while (0)
+
+#endif
index 5b6d5b0..3132cb8 100644 (file)
 */
 
 /**
+ * @brief The task template.
+ */
+typedef struct task task_t;
+
+/**
  * @brief Called on the task execution.
  */
 typedef void(*task_execute_cb)(task_t *task);
@@ -29,17 +34,14 @@ typedef int(*task_init_cb)(task_t *task);
  */
 typedef void(*task_shutdown_cb)(task_t *task);
 
-/**
- * @brief The task template.
- */
-typedef struct task
+struct task
 {
     task_init_cb init;
     task_shutdown_cb shutdown;
     task_execute_cb execute;
     int frequency;
     void *user_data;
-} task_t;
+};
 
 /**
  * @brief Initializes the task.
index e69de29..8a5804d 100644 (file)
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * 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.
+ */
+
+#include <glib.h>
+
+#include "worker.h"
+#include "log.h"
+#include "err-check.h"
+
+#define MAX_THREAD_DEFAULT 1
+
+static void worker_function(gpointer data, gpointer user_data);
+
+struct worker
+{
+    GThreadPool *thread_pool;
+};
+
+worker_t *worker_create()
+{
+    GError *err = NULL;
+    struct worker *_worker = g_malloc(sizeof(struct worker));
+
+    _worker->thread_pool = g_thread_pool_new((GFunc)worker_function, NULL, MAX_THREAD_DEFAULT, FALSE, &err);
+    if (err != NULL)
+    {
+        ERR("Failed to create thread pool ", err->message);
+        g_error_free (err);
+        g_free(_worker);
+        return NULL;
+    }
+    return _worker;
+}
+
+void worker_destroy(worker_t *worker)
+{
+    ON_NULL_RETURN(worker);
+
+    g_thread_pool_free (worker->thread_pool, FALSE, TRUE);
+    g_free(worker);
+}
+
+void worker_set_max_num_of_threads(worker_t *worker, int max_threads)
+{
+    ON_NULL_RETURN(worker);
+
+    GError *err = NULL;
+    g_thread_pool_set_max_threads(worker->thread_pool, max_threads, &err);
+    if (err != NULL)
+    {
+        ERR("Failed to set maximum threads for given pool ", err->message);
+        g_error_free (err);
+    }
+}
+
+int worker_enqueue_task(worker_t *worker, task_t task)
+{
+    ON_NULL_RETURN_VAL(worker, -1);
+
+    return g_thread_pool_push(worker->thread_pool, &task, NULL);
+}
+
+static void worker_function(gpointer data, gpointer user_data)
+{
+    task_t *task = (task_t *)data;
+    task->init(task);
+    task->execute(task);
+    task->shutdown(task);
+}
\ No newline at end of file
index bdd53a1..05021d4 100644 (file)
 typedef struct worker worker_t;
 
 /**
- * @brief Worker mode enumeration.
- */
-typedef enum worker_mode
-{
-    /**
-     * @brief In this mode worker busy to idle proportion is low.
-     */
-    MODE_LOW,
-
-    /**
-     * @brief In this mode worker busy to idle proportion is balanced.
-     */
-    MODE_MEDIUM,
-
-    /**
-     * @brief In this mode worker busy to idle proportion is high.
-     */
-    MODE_HIGH
-} worker_mode_e;
-
-/**
  * @brief Creates worker.
  */
 worker_t *worker_create();
@@ -53,7 +32,7 @@ worker_t *worker_create();
 /**
  * @brief Destroys worker.
  */
-void worker_destroy();
+void worker_destroy(worker_t *worker);
 
 /**
  * @brief Sets maximum number of threads that worker can use.
@@ -69,16 +48,4 @@ void worker_set_max_num_of_threads(worker_t *worker, int max_threads);
  */
 int worker_enqueue_task(worker_t *worker, task_t task);
 
-/**
- * @brief Sets worker mode.
- * @param[in] worker Worker reference.
- */
-void worker_set_mode(worker_t *worker, worker_mode_e mode);
-
-/**
- * @brief Gets worker mode.
- * @param[in] worker Worker reference.
- */
-worker_mode_e worker_get_mode(worker_t *worker);
-
-#endif
\ No newline at end of file
+#endif