Split TCP Server Mutex into different file
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpserver_mutex.c
1 /* ****************************************************************
2  *
3  * Copyright 2019 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "catcpserver_mutex.h"
22 /**
23  * Logging tag for module name.
24  */
25 //#define TAG "OIC_CA_TCP_SERVER"
26 #define TAG TCP_SERVER_TAG
27
28 /**
29  * Mutex to synchronize device object list.
30  */
31 oc_mutex g_mutexObjectList = NULL;
32
33 /**
34  * Conditional mutex to synchronize.
35  */
36 oc_cond g_condObjectList= NULL;
37
38 /**
39  * Mutex to synchronize send.
40  */
41 oc_mutex g_mutexSend= NULL;
42
43 /**
44  * Conditional mutex to synchronize send.
45  */
46 oc_cond g_condSend= NULL;
47
48 void CATCPDestroyMutex()
49 {
50     if (g_mutexObjectList)
51     {
52         oc_mutex_free(g_mutexObjectList);
53         g_mutexObjectList = NULL;
54     }
55 }
56
57 CAResult_t CATCPCreateMutex()
58 {
59     if (!g_mutexObjectList)
60     {
61         g_mutexObjectList = oc_mutex_new_recursive();
62         if (!g_mutexObjectList)
63         {
64             OIC_LOG(ERROR, TAG, "Failed to create mutex!");
65             return CA_STATUS_FAILED;
66         }
67     }
68
69     return CA_STATUS_OK;
70 }
71
72 void CATCPDestroyCond()
73 {
74     if (g_condObjectList)
75     {
76         oc_cond_free(g_condObjectList);
77         g_condObjectList = NULL;
78     }
79 }
80
81 CAResult_t CATCPCreateCond()
82 {
83     if (!g_condObjectList)
84     {
85         g_condObjectList = oc_cond_new();
86         if (!g_condObjectList)
87         {
88             OIC_LOG(ERROR, TAG, "Failed to create cond!");
89             return CA_STATUS_FAILED;
90         }
91     }
92     return CA_STATUS_OK;
93 }
94
95 void CATCPDestroySendMutex()
96 {
97     if (g_mutexSend)
98     {
99         oc_mutex_free(g_mutexSend);
100         g_mutexSend = NULL;
101     }
102 }
103
104 CAResult_t CATCPCreateSendMutex()
105 {
106     if (!g_mutexSend)
107     {
108         g_mutexSend = oc_mutex_new();
109         if (!g_mutexSend)
110         {
111             OIC_LOG(ERROR, TAG, "Failed to create send mutex!");
112             return CA_STATUS_FAILED;
113         }
114     }
115
116     return CA_STATUS_OK;
117 }
118
119 void CATCPDestroySendCond()
120 {
121     if (g_condSend)
122     {
123         oc_cond_free(g_condSend);
124         g_condSend = NULL;
125     }
126 }
127
128 CAResult_t CATCPCreateSendCond()
129 {
130     if (!g_condSend)
131     {
132         g_condSend = oc_cond_new();
133         if (!g_condSend)
134         {
135             OIC_LOG(ERROR, TAG, "Failed to create send cond!");
136             return CA_STATUS_FAILED;
137         }
138     }
139     return CA_STATUS_OK;
140 }