add comment LCOV_EXCL
[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                 /* LCOV_EXCL_START */
66                 ERR("unknown type(%d)", type);
67                 ret_val = NULL;
68                 break;
69                 /* LCOV_EXCL_STOP */
70         }
71         return ret_val;
72 }
73
74 void cal_mutex_lock(int type)
75 {
76         int ret;
77         pthread_mutex_t *mutex;
78
79         mutex = _cal_mutex_get_mutex(type);
80
81         if (mutex) {
82                 ret = pthread_mutex_lock(mutex);
83                 RETM_IF(ret, "pthread_mutex_lock() Fail(%d)", ret);
84         }
85 }
86
87 void cal_mutex_unlock(int type)
88 {
89         int ret;
90         pthread_mutex_t *mutex;
91
92         mutex = _cal_mutex_get_mutex(type);
93
94         if (mutex) {
95                 ret = pthread_mutex_unlock(mutex);
96                 RETM_IF(ret, "pthread_mutex_unlock() Fail(%d)", ret);
97         }
98 }