Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / utility / fw_async_queue.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdlib.h>
19 #include "utility/fw_async_queue.h"
20 #include "utility/fw_async_queue_internal.h"
21 #include "utility/sync_util.h"
22
23 #ifndef EXPORT_API
24 #define EXPORT_API __attribute__ ((visibility("default")))
25 #endif
26
27 #ifndef SYNC_AGENT_LOG
28 #undef LOG_TAG
29 #define LOG_TAG "AF_UTIL_FW_AYNC_QUEUE"
30 #endif
31
32 EXPORT_API sync_agent_util_async_queue_s *sync_agent_alloc_async_queue()
33 {
34         _EXTERN_FUNC_ENTER;
35
36 #if !GLIB_CHECK_VERSION (2, 32, 0)
37         if (!g_thread_supported()) {
38                 g_thread_init(NULL);
39         }
40 #endif
41
42         sync_agent_util_async_queue_s *queue = (sync_agent_util_async_queue_s *) calloc(1, sizeof(sync_agent_util_async_queue_s));
43         if (queue == NULL) {
44                 goto return_part;
45         }
46
47         queue->pQueue = g_async_queue_new();
48         if (queue->pQueue == NULL) {
49                 goto error_part;
50         }
51
52  return_part:
53         _EXTERN_FUNC_EXIT;
54         return queue;
55
56  error_part:
57         util_destroy_async_queue(queue);
58         return NULL;
59 }
60
61 void util_destroy_async_queue(sync_agent_util_async_queue_s * queue)
62 {
63         _EXTERN_FUNC_ENTER;
64
65         retm_if(queue == NULL, "queue parameter is NULL !!");
66
67         if (queue != NULL) {
68                 if (queue->pQueue != NULL) {
69                         g_async_queue_unref(queue->pQueue);
70                         queue->pQueue = NULL;
71                 }
72                 free(queue);
73         }
74
75         _EXTERN_FUNC_EXIT;
76 }
77
78 void util_lock_async_queue(sync_agent_util_async_queue_s * queue)
79 {
80         _EXTERN_FUNC_ENTER;
81
82         retm_if(queue == NULL, "queue parameter is NULL !!");
83
84         if (queue != NULL && queue->pQueue != NULL) {
85                 g_async_queue_lock(queue->pQueue);
86         } else {
87                 _DEBUG_INFO("Invalid fw_async_queue\n");
88         }
89
90         _EXTERN_FUNC_EXIT;
91 }
92
93 void util_unlock_async_queue(sync_agent_util_async_queue_s * queue)
94 {
95         _EXTERN_FUNC_ENTER;
96
97         retm_if(queue == NULL, "queue parameter is NULL !!");
98
99         if (queue != NULL && queue->pQueue != NULL) {
100                 g_async_queue_unlock(queue->pQueue);
101         } else {
102                 _DEBUG_INFO("Invalid fw_async_queue\n");
103         }
104
105         _EXTERN_FUNC_EXIT;
106 }
107
108 EXPORT_API void *sync_agent_receive_msg_async_queue(sync_agent_util_async_queue_s * queue)
109 {
110         _EXTERN_FUNC_ENTER;
111
112         retvm_if(queue == NULL, NULL, "queue parameter is NULL !!");
113
114         _EXTERN_FUNC_EXIT;
115
116         return g_async_queue_pop(queue->pQueue);
117 }
118
119 EXPORT_API void sync_agent_send_msg_async_queue(sync_agent_util_async_queue_s * queue, void *msg)
120 {
121         _EXTERN_FUNC_ENTER;
122
123         retm_if(queue == NULL, "queue parameter is NULL !!");
124
125         g_async_queue_push(queue->pQueue, msg);
126
127         _EXTERN_FUNC_EXIT;
128 }
129
130 void util_send_msg_async_queue_with_compare_priority(sync_agent_util_async_queue_s * queue, void *msg, sync_agent_compare_priority_cb cpf, void *user_data)
131 {
132         _EXTERN_FUNC_ENTER;
133
134         retm_if(queue == NULL, "queue parameter is NULL !!");
135
136         if (cpf == NULL) {
137                 g_async_queue_push(queue->pQueue, msg);
138         } else {
139                 g_async_queue_push_sorted(queue->pQueue, msg, cpf, user_data);
140         }
141
142         _EXTERN_FUNC_EXIT;
143 }
144
145 EXPORT_API int sync_agent_async_queue_length(sync_agent_util_async_queue_s * queue)
146 {
147         _EXTERN_FUNC_ENTER;
148
149         retvm_if(queue == NULL, -1, "queue parameter is NULL !!");
150
151         _EXTERN_FUNC_EXIT;
152
153         return g_async_queue_length(queue->pQueue);
154 }