Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / utility / fw_thread.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 "utility/sync_util.h"
19 #include "utility/fw_thread.h"
20 #include <stdlib.h>
21
22 #ifndef EXPORT_API
23 #define EXPORT_API __attribute__ ((visibility("default")))
24 #endif
25
26 #ifndef SYNC_AGENT_LOG
27 #undef LOG_TAG
28 #define LOG_TAG "AF_THREAD"
29 #endif
30
31 pthread_mutex_t *util_alloc_init_mutex(void)
32 {
33         _EXTERN_FUNC_ENTER;
34
35         pthread_mutex_t *mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
36         if (mutex == NULL) {
37                 goto return_part;
38         }
39         if (0 != pthread_mutex_init(mutex, NULL)) {
40                 free(mutex);
41                 mutex = NULL;
42                 goto return_part;
43         }
44
45  return_part:
46         _EXTERN_FUNC_EXIT;
47         return mutex;
48 }
49
50 void util_free_mutex(pthread_mutex_t * mutex)
51 {
52         _EXTERN_FUNC_ENTER;
53
54         if (mutex != NULL) {
55                 pthread_mutex_destroy(mutex);
56                 free(mutex);
57         }
58
59         _EXTERN_FUNC_EXIT;
60 }
61
62 pthread_cond_t *util_alloc_init_thread_cond(void)
63 {
64         _EXTERN_FUNC_ENTER;
65
66         pthread_cond_t *thread_cond = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
67         if (thread_cond == NULL) {
68                 goto return_part;
69         }
70
71         if (0 != pthread_cond_init(thread_cond, 0)) {
72                 free(thread_cond);
73                 thread_cond = NULL;
74                 goto return_part;
75         }
76
77  return_part:
78         _EXTERN_FUNC_EXIT;
79         return thread_cond;
80 }
81
82 void util_free_thread_cond(pthread_cond_t * thread_cond)
83 {
84         _EXTERN_FUNC_ENTER;
85
86         if (thread_cond != NULL) {
87                 pthread_cond_destroy(thread_cond);
88                 free(thread_cond);
89         }
90
91         _EXTERN_FUNC_EXIT;
92 }