4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "setting-storage-async-worker.h"
28 async_fn fn; /*'fn' Must be MT-safe */
33 Ecore_Idler *worker_idler;
36 static GHashTable *storage_worker_hashT;
38 void storageUg_thread_testcancel()
42 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
44 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
46 SETTING_TRACE_ERROR("pthread_setcancelstate() Fail(%d)", ret);
51 static Eina_Bool _async_worker_idler(void *data)
54 SETTING_TRACE("---------------------------------------------thread join BEGIN ");
55 storageUg_worker *worker = data;
57 retv_if(NULL == data, ECORE_CALLBACK_CANCEL);
59 pthread_join(worker->tid, NULL);
60 worker->alive = FALSE;
63 worker->cb(worker->fn_ret, worker->ad);
67 SETTING_TRACE("---------------------------------------------thread join END ");
69 return ECORE_CALLBACK_CANCEL;
72 static void *_async_worker_thread(void *data)
75 storageUg_worker *worker = data;
77 retv_if(NULL == data, NULL);
79 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
81 SETTING_TRACE_ERROR("pthread_setcancelstate() Fail(%d)", ret);
85 worker->fn_ret = worker->fn(worker->ad);
89 worker->worker_idler = ecore_idler_add(_async_worker_idler, worker);
90 if (worker->worker_idler == NULL) {
91 SETTING_TRACE_ERROR("worker->worker_idler == NULL");
92 worker->alive = FALSE;
93 /*----------------------------------- */
95 /*----------------------------------- */
96 _async_worker_idler(worker);
102 static void _async_worker_hash_free_key(gpointer data)
104 storageUg_worker *worker = data;
107 pthread_cancel(worker->tid);
108 pthread_join(worker->tid, NULL);
111 if (worker->worker_idler)
112 ecore_idler_del(worker->worker_idler);
118 bool storageUg_worker_is_running(void *data)
120 storageUg_worker *worker = data;
121 SETTING_TRACE("*** worker->alive ------> %d ", worker->alive);
122 return worker->alive;
125 void *storageUg_start_async_worker(async_fn fn, callback_fn cb,
126 SettingStorageUG *ad)
129 storageUg_worker *worker;
131 retv_if(NULL == fn, NULL);
133 if (NULL == storage_worker_hashT) {
134 storage_worker_hashT = g_hash_table_new_full(NULL, NULL,
135 _async_worker_hash_free_key, NULL);
138 worker = calloc(1, sizeof(storageUg_worker));
139 if (NULL == worker) {
140 SETTING_TRACE_ERROR("calloc() Fail");
147 g_hash_table_add(storage_worker_hashT, worker);
149 ret = pthread_create(&worker->tid, NULL, _async_worker_thread, worker);
150 warn_if(ret, "phread_create() Fail(%d)", ret);
152 worker->alive = TRUE;
156 void storageUg_stop_async_worker(void *worker_id)
158 storageUg_worker *worker = worker_id;
160 ret_if(NULL == worker);
162 if (g_hash_table_contains(storage_worker_hashT, worker_id))
163 g_hash_table_remove(storage_worker_hashT, worker_id);
165 if (0 == g_hash_table_size(storage_worker_hashT)) {
166 g_hash_table_destroy(storage_worker_hashT);
167 storage_worker_hashT = NULL;
171 void storageUg_stop_async_worker_all(void)
173 g_hash_table_destroy(storage_worker_hashT);
174 storage_worker_hashT = NULL;