diff options
-rw-r--r-- | packaging/task-worker.spec | 2 | ||||
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/err-check.h | 15 | ||||
-rw-r--r-- | src/task.h | 12 | ||||
-rw-r--r-- | src/worker.c | 82 | ||||
-rw-r--r-- | src/worker.h | 37 |
6 files changed, 109 insertions, 41 deletions
diff --git a/packaging/task-worker.spec b/packaging/task-worker.spec index 74d0131..f694ee0 100644 --- a/packaging/task-worker.spec +++ b/packaging/task-worker.spec @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c733536..2acb38c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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}) diff --git a/src/err-check.h b/src/err-check.h index 2ee8168..ddf2fd7 100644 --- a/src/err-check.h +++ b/src/err-check.h @@ -33,5 +33,18 @@ }\ } 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 @@ -15,6 +15,11 @@ */ /** + * @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. diff --git a/src/worker.c b/src/worker.c index e69de29..8a5804d 100644 --- a/src/worker.c +++ b/src/worker.c @@ -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 diff --git a/src/worker.h b/src/worker.h index bdd53a1..05021d4 100644 --- a/src/worker.h +++ b/src/worker.h @@ -25,27 +25,6 @@ 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 |