revise packaging
[platform/core/pim/contacts-service.git] / common / ctsvc_mutex.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 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 #include <pthread.h>
20
21 #include "contacts.h"
22 #include "ctsvc_internal.h"
23 #include "ctsvc_mutex.h"
24
25 typedef struct {
26         int (*lock) (pthread_mutex_t *mutex);
27         int (*unlock) (pthread_mutex_t *mutex);
28 } cts_mutex_fns;
29
30 static cts_mutex_fns __ctsvc_mutex_funtions = {
31         pthread_mutex_lock,
32         pthread_mutex_unlock
33 };
34
35 static pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;
36 static pthread_mutex_t sockfd_mutex = PTHREAD_MUTEX_INITIALIZER;
37 static pthread_mutex_t ipc_mutex = PTHREAD_MUTEX_INITIALIZER;
38 static pthread_mutex_t ipc_pubsub_mutex = PTHREAD_MUTEX_INITIALIZER;
39 static pthread_mutex_t access_control_mutex = PTHREAD_MUTEX_INITIALIZER;
40 static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
41 static pthread_mutex_t timeout_mutex = PTHREAD_MUTEX_INITIALIZER;
42 static pthread_mutex_t socket_client_info_mutex = PTHREAD_MUTEX_INITIALIZER;
43 static pthread_mutex_t cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45 static int __old_type = 0;
46 static int __defered_ref = 0;
47
48 static inline pthread_mutex_t* __ctsvc_mutex_get_mutex(int type)
49 {
50         pthread_mutex_t *ret_val;
51
52         switch (type) {
53         case CTS_MUTEX_CONNECTION:
54                 ret_val = &conn_mutex;
55                 break;
56         case CTS_MUTEX_SOCKET_FD:
57                 ret_val = &sockfd_mutex;
58                 break;
59         case CTS_MUTEX_PIMS_IPC_CALL:
60                 ret_val = &ipc_mutex;
61                 break;
62         case CTS_MUTEX_PIMS_IPC_PUBSUB:
63                 ret_val = &ipc_pubsub_mutex;
64                 break;
65         case CTS_MUTEX_ACCESS_CONTROL:
66                 ret_val = &access_control_mutex;
67                 break;
68         case CTS_MUTEX_HANDLE:
69                 ret_val = &handle_mutex;
70                 break;
71         case CTS_MUTEX_TIMEOUT:
72                 ret_val = &timeout_mutex;
73                 break;
74         case CTS_MUTEX_SOCKET_CLIENT_INFO:
75                 ret_val = &socket_client_info_mutex;
76                 break;
77         case CTS_MUTEX_CYNARA:
78                 ret_val = &cynara_mutex;
79                 break;
80         default:
81                 /* LCOV_EXCL_START */
82                 ERR("unknown type(%d)", type);
83                 ret_val = NULL;
84                 break;
85                 /* LCOV_EXCL_STOP */
86         }
87         return ret_val;
88 }
89
90 void ctsvc_mutex_lock(int type)
91 {
92         int ret;
93         pthread_mutex_t *mutex;
94
95         /*
96          * If user use pthread_cancel, lock can be occured
97          * protect it, call pthread_set_canceltype as PTHREAD_CANCEL_DEFERRED
98          * But, if another module call PTHREAD_CANCEL_ASYNCHRONOUS,
99          * it can be occured again
100          * So, Do not use the pthread_cancel with contacts_service API
101          */
102         if (__defered_ref == 0)
103                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &__old_type);
104         else
105                 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
106
107         __defered_ref++;
108
109         mutex = __ctsvc_mutex_get_mutex(type);
110
111         if (__ctsvc_mutex_funtions.lock) {
112                 ret = __ctsvc_mutex_funtions.lock(mutex);
113                 WARN_IF(ret, "mutex_lock Fail(%d)", ret);
114         }
115 }
116
117 void ctsvc_mutex_unlock(int type)
118 {
119         int ret;
120         pthread_mutex_t *mutex;
121
122         mutex = __ctsvc_mutex_get_mutex(type);
123
124         if (__ctsvc_mutex_funtions.unlock) {
125                 ret = __ctsvc_mutex_funtions.unlock(mutex);
126                 WARN_IF(ret, "mutex_unlock Fail(%d)", ret);
127         }
128
129         __defered_ref--;
130         if (__defered_ref == 0)
131                 pthread_setcanceltype(__old_type, NULL);
132 }
133