tizen 2.3.1 release
[apps/home/settings.git] / setting-appmgr / src / setting-appmgr-async-worker.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <glib.h>
20 #include <pthread.h>
21
22 #include "setting-appmgr-async-worker.h"
23
24 typedef struct {
25         pthread_t tid;
26         int alive;
27
28         async_fn fn; /*'fn' Must be MT-safe */
29         int fn_ret;
30         callback_fn cb;
31         SettingAppMgrUG *ad;
32
33         Ecore_Idler *worker_idler;
34 } appmgrUg_worker;
35
36 static GHashTable *async_worker_hashT;
37
38 void appmgrUg_thread_testcancel()
39 {
40         int ret;
41
42         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
43         pthread_testcancel();
44         ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
45         if (0 != ret) {
46                 SETTING_TRACE_ERROR("pthread_setcancelstate() Fail(%d)", ret);
47                 pthread_exit(NULL);
48         }
49 }
50
51 static Eina_Bool _async_worker_idler(void *data)
52 {
53         appmgrUg_worker *worker = data;
54
55         retv_if(NULL == data, ECORE_CALLBACK_CANCEL);
56
57         pthread_join(worker->tid, NULL);
58         worker->alive = FALSE;
59
60         if (worker->cb)
61                 worker->cb(worker->fn_ret, worker->ad);
62
63         g_hash_table_remove(async_worker_hashT, worker);
64
65         return ECORE_CALLBACK_CANCEL;
66 }
67
68 static void *_async_worker_thread(void *data)
69 {
70         int ret;
71         appmgrUg_worker *worker = data;
72
73         retv_if(NULL == data, NULL);
74
75         ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
76         if (0 != ret) {
77                 SETTING_TRACE_ERROR("pthread_setcancelstate() Fail(%d)", ret);
78                 pthread_exit(NULL);
79         }
80
81         worker->fn_ret = worker->fn(worker->ad);
82
83         APPMGRUG_STOP_POINT;
84
85         worker->worker_idler = ecore_idler_add(_async_worker_idler, worker);
86
87         pthread_exit(NULL);
88 }
89
90 static void _async_worker_hash_free_key(gpointer data)
91 {
92         appmgrUg_worker *worker = data;
93
94         if (worker->alive) {
95                 pthread_cancel(worker->tid);
96                 pthread_join(worker->tid, NULL);
97         }
98
99         if (worker->worker_idler)
100                 ecore_idler_del(worker->worker_idler);
101
102         free(worker);
103 }
104
105 void *appmgrUg_start_async_worker(async_fn fn, callback_fn cb,
106                                   SettingAppMgrUG *ad)
107 {
108         int ret;
109         appmgrUg_worker *worker;
110
111         retv_if(NULL == fn, NULL);
112
113         if (NULL == async_worker_hashT) {
114                 async_worker_hashT = g_hash_table_new_full(NULL, NULL,
115                                                            _async_worker_hash_free_key, NULL);
116         }
117
118         worker = calloc(1, sizeof(appmgrUg_worker));
119         if (NULL == worker) {
120                 SETTING_TRACE_ERROR("calloc() Fail");
121                 return NULL;
122         }
123         worker->fn = fn;
124         worker->cb = cb;
125         worker->ad = ad;
126
127         g_hash_table_add(async_worker_hashT, worker);
128
129         ret = pthread_create(&worker->tid, NULL, _async_worker_thread, worker);
130         warn_if(ret, "phread_create() Fail(%d)", ret);
131
132         worker->alive = TRUE;
133         return worker;
134 }
135
136 void appmgrUg_stop_async_worker(void *worker_id)
137 {
138         appmgrUg_worker *worker = worker_id;
139
140         ret_if(NULL == worker);
141
142         if (g_hash_table_contains(async_worker_hashT, worker_id))
143                 g_hash_table_remove(async_worker_hashT, worker_id);
144
145         if (0 == g_hash_table_size(async_worker_hashT)) {
146                 g_hash_table_destroy(async_worker_hashT);
147                 async_worker_hashT = NULL;
148         }
149 }
150
151 void appmgrUg_stop_async_worker_all(void)
152 {
153         g_hash_table_destroy(async_worker_hashT);
154         async_worker_hashT = NULL;
155 }