2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include "err-check.h"
24 #define THREADS_NO_LIMIT -1
26 static void worker_function(gpointer data, gpointer user_data);
30 GThreadPool *thread_pool;
33 worker_t *worker_create()
36 struct worker *_worker = g_malloc(sizeof(struct worker));
38 _worker->thread_pool = g_thread_pool_new((GFunc)worker_function, NULL, THREADS_NO_LIMIT, FALSE, &err);
41 ERR("Failed to create thread pool ", err->message);
49 void worker_destroy(worker_t *worker)
51 ON_NULL_RETURN(worker);
53 g_thread_pool_free (worker->thread_pool, FALSE, TRUE);
57 void worker_sync(worker_t *worker)
61 g_thread_pool_free(worker->thread_pool, FALSE, TRUE);
62 worker->thread_pool = g_thread_pool_new((GFunc)worker_function, NULL, THREADS_NO_LIMIT, FALSE, &err);
65 ERR("Failed to create thread pool ", err->message);
71 void worker_set_max_num_of_threads(worker_t *worker, int max_threads)
73 ON_NULL_RETURN(worker);
76 g_thread_pool_set_max_threads(worker->thread_pool, max_threads, &err);
79 ERR("Failed to set maximum threads for given pool ", err->message);
84 int worker_enqueue_task(worker_t *worker, task_t *task)
86 ON_NULL_RETURN_VAL(worker, -1);
88 return g_thread_pool_push(worker->thread_pool, task, NULL);
91 static void worker_function(gpointer data, gpointer user_data)
93 task_t *task = (task_t *)data;