2.0_alpha release commit
[framework/messaging/email-service.git] / email-ipc / email-stub / email-stub-task-manager.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 #include <string.h>
25 #include <pthread.h>
26 #include <errno.h>
27 #include <glib.h>
28
29 #include "email-stub-task-manager.h"
30 #include "email-stub-task.h"
31 #include "email-ipc-build.h"
32
33 #include "email-debug-log.h"
34 #include "email-api.h"
35 #include "email-internal-types.h"
36
37 static pthread_t task_thread = 0;
38 static bool stop_flag = false;
39 static GQueue *task_queue = NULL;
40
41 pthread_mutex_t ipc_task_mutex = PTHREAD_MUTEX_INITIALIZER;
42 pthread_cond_t ipc_task_cond = PTHREAD_COND_INITIALIZER;
43
44 EXPORT_API bool emipc_start_task_thread()
45 {
46         EM_DEBUG_FUNC_BEGIN();
47         if (task_thread)
48                 return true;
49
50         task_queue = g_queue_new();
51
52         if (pthread_create(&task_thread, NULL, &emipc_do_task_thread, NULL) != 0) {
53                 EM_DEBUG_LOG("Worker thread creation failed: %s", strerror(errno));
54                 return false;   
55         }
56
57         return true;
58 }
59
60 EXPORT_API void emipc_terminate_task_thread()
61 {
62         emipc_stop_task_thread();
63         pthread_cancel(task_thread);
64
65         emipc_email_task *task = (emipc_email_task *)g_queue_pop_head(task_queue);
66         while (task) {
67                 EM_SAFE_FREE(task);
68                 task = (emipc_email_task *)g_queue_pop_head(task_queue);
69         }
70         g_queue_free(task_queue);
71 }
72
73 EXPORT_API bool emipc_stop_task_thread()
74 {
75         stop_flag = true;
76         return true;
77 }
78
79 EXPORT_API void *emipc_do_task_thread()
80 {
81         EM_DEBUG_FUNC_BEGIN();
82         
83         emipc_email_task *task = NULL;
84
85         while (!stop_flag) {
86                 ENTER_CRITICAL_SECTION(ipc_task_mutex);
87                 while (g_queue_is_empty(task_queue)) {
88                         EM_DEBUG_LOG("Blocked until new task arrivers %p.", &ipc_task_cond);
89                         SLEEP_CONDITION_VARIABLE(ipc_task_cond, ipc_task_mutex);
90                 }
91                 
92                 task = (emipc_email_task *)g_queue_pop_head(task_queue);
93                 LEAVE_CRITICAL_SECTION(ipc_task_mutex);
94
95                 if (task) {
96                         emipc_run_task(task);
97                         emipc_free_email_task(task);
98                 }
99         }
100         
101         return NULL;                    
102 }
103
104 /* code for ipc handler */
105 EXPORT_API bool emipc_create_task(unsigned char *task_stream, int response_channel)
106 {
107         emipc_email_task *task = NULL;
108         bool ret = true;
109
110         task = (emipc_email_task *)malloc(sizeof(emipc_email_task));
111         if (task == NULL) {
112                 EM_DEBUG_EXCEPTION("Malloc failed.");
113                 ret = false;
114         } else {
115                 emipc_parse_stream_email_task(task, task_stream, response_channel);
116                 
117                 EM_DEBUG_LOG("[IPCLib] ======================================================");
118                 EM_DEBUG_LOG("[IPCLib] Register new task : %p", task);
119                 EM_DEBUG_LOG("[IPCLib] Task API ID : %s (%d)", EM_APIID_TO_STR(task->api_info->api_id), task->api_info->api_id);
120                 EM_DEBUG_LOG("[IPCLib] Task Response ID : %d", EM_APIID_TO_STR(task->api_info->response_id));
121                 EM_DEBUG_LOG("[IPCLib] Task APP ID : %d", task->api_info->app_id);
122                 EM_DEBUG_LOG("[IPCLib] ======================================================");
123
124                 ENTER_CRITICAL_SECTION(ipc_task_mutex);
125                 g_queue_push_head(task_queue, (void *)task);
126                 
127                 WAKE_CONDITION_VARIABLE(ipc_task_cond);
128                 LEAVE_CRITICAL_SECTION(ipc_task_mutex);
129         }
130         return ret;
131 }