ad86c03dd915534e5ced99a0af5223e63433cb52
[platform/core/pim/calendar-service.git] / common / cal_mutex.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
20 #include <pthread.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_mutex.h"
25
26 static pthread_mutex_t _cal_property_hash_mutex = PTHREAD_MUTEX_INITIALIZER;
27 static pthread_mutex_t _cal_connection_mutex = PTHREAD_MUTEX_INITIALIZER;
28 static pthread_mutex_t _cal_pims_ipc_call_mutex = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_mutex_t _cal_inotify_mutex = PTHREAD_MUTEX_INITIALIZER;
30 static pthread_mutex_t _cal_pims_ipc_pubsub_mutex = PTHREAD_MUTEX_INITIALIZER;
31 static pthread_mutex_t _cal_access_control_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static pthread_mutex_t _cal_timeout_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_mutex_t _cal_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
34
35 static inline pthread_mutex_t* _cal_mutex_get_mutex(int type)
36 {
37         pthread_mutex_t *ret_val;
38
39         switch (type) {
40         case CAL_MUTEX_PROPERTY_HASH:
41                 ret_val = &_cal_property_hash_mutex;
42                 break;
43         case CAL_MUTEX_CONNECTION:
44                 ret_val = &_cal_connection_mutex;
45                 break;
46         case CAL_MUTEX_PIMS_IPC_CALL:
47                 ret_val = &_cal_pims_ipc_call_mutex;
48                 break;
49         case CAL_MUTEX_INOTIFY:
50                 ret_val = &_cal_inotify_mutex;
51                 break;
52         case CAL_MUTEX_PIMS_IPC_PUBSUB:
53                 ret_val = &_cal_pims_ipc_pubsub_mutex;
54                 break;
55         case CAL_MUTEX_ACCESS_CONTROL:
56                 ret_val = &_cal_access_control_mutex;
57                 break;
58         case CAL_MUTEX_TIMEOUT:
59                 ret_val = &_cal_timeout_mutex;
60                 break;
61         case CAL_MUTEX_HANDLE:
62                 ret_val = &_cal_handle_mutex;
63                 break;
64         default:
65                 ERR("unknown type(%d)", type);
66                 ret_val = NULL;
67                 break;
68         }
69         return ret_val;
70 }
71
72 void cal_mutex_lock(int type)
73 {
74         int ret;
75         pthread_mutex_t *mutex;
76
77         mutex = _cal_mutex_get_mutex(type);
78
79         if (mutex) {
80                 ret = pthread_mutex_lock(mutex);
81                 RETM_IF(ret, "pthread_mutex_lock() Fail(%d)", ret);
82         }
83 }
84
85 void cal_mutex_unlock(int type)
86 {
87         int ret;
88         pthread_mutex_t *mutex;
89
90         mutex = _cal_mutex_get_mutex(type);
91
92         if (mutex) {
93                 ret = pthread_mutex_unlock(mutex);
94                 RETM_IF(ret, "pthread_mutex_unlock() Fail(%d)", ret);
95         }
96 }