Rename SendResponse to SendDirectStackResponse in ocstack.c
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / camutex_glib.c
1 /* ****************************************************************
2  *
3  * Copyright 2014 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 /**
22  * @file
23  * This file provides APIs related to mutex and semaphores.
24  */
25
26 #include "camutex.h"
27 #include <glib.h>
28 #include <string.h>
29 #include "logger.h"
30
31 /**
32  * @def TAG
33  * @brief Logging tag for module name
34  */
35 #define TAG PCF("UMUTEX")
36
37 ca_mutex ca_mutex_new(void)
38 {
39     GMutex *mutexLock = g_new(GMutex, 1);
40     g_mutex_init(mutexLock);
41     return (ca_mutex) mutexLock;
42 }
43
44 void ca_mutex_lock(ca_mutex mutex)
45 {
46     if (NULL == mutex)
47     {
48         OIC_LOG(ERROR, TAG , "ca_mutex_lock, Invalid mutex !");
49         return;
50     }
51
52     GMutex *mutexLock = (GMutex *) mutex;
53     g_mutex_lock(mutexLock);
54 }
55
56 bool ca_mutex_trylock(ca_mutex mutex)
57 {
58     if (NULL == mutex)
59     {
60         OIC_LOG(ERROR, TAG, "ca_mutex_trylock, Invalid mutex !");
61         return false;
62     }
63
64     GMutex *mutexLock = (GMutex *) mutex;
65
66     return(g_mutex_trylock(mutexLock));
67 }
68
69 void ca_mutex_unlock(ca_mutex mutex)
70 {
71     if (NULL == mutex)
72     {
73         OIC_LOG(ERROR, TAG, "ca_mutex_unlock, Invalid mutex !");
74         return;
75     }
76
77     GMutex *mutexLock = (GMutex *) mutex;
78     g_mutex_unlock(mutexLock);
79 }
80
81 bool ca_mutex_free(ca_mutex mutex)
82 {
83     if (NULL == mutex)
84     {
85         OIC_LOG(ERROR, TAG, "ca_mutex_free, Invalid mutex !");
86         return false;
87     }
88
89     GMutex *mutexLock = (GMutex *) mutex;
90     g_mutex_clear(mutexLock);
91     g_free(mutexLock);
92     return true;
93 }
94
95 ca_cond ca_cond_new(void)
96 {
97     GCond *condition = g_new(GCond, 1);
98     g_cond_init(condition);
99     return (ca_cond) condition;
100 }
101
102 void ca_cond_signal(ca_cond cond)
103 {
104     if (NULL == cond)
105     {
106         OIC_LOG(ERROR, TAG, "ca_cond_signal, Invalid condition !");
107         return;
108     }
109
110     GCond *condition = (GCond *) cond;
111     g_cond_signal(condition);
112 }
113
114 void ca_cond_broadcast(ca_cond cond)
115 {
116     if (NULL == cond)
117     {
118         OIC_LOG(ERROR, TAG, "ca_cond_broadcast, Invalid condition !");
119         return;
120     }
121
122     GCond *condition = (GCond *) cond;
123     g_cond_broadcast(condition);
124 }
125
126 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
127 {
128     if (NULL == mutex)
129     {
130         OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid mutex !");
131         return;
132     }
133
134     if (NULL == cond)
135     {
136         OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid condition !");
137         return;
138     }
139
140     GMutex *mutexLock = (GMutex *) mutex;
141     GCond *condition = (GCond *) cond;
142     g_cond_wait(condition, mutexLock);
143 }
144
145 CAWaitResult_t ca_cond_wait_until(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
146 {
147     if (NULL == mutex)
148     {
149         OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid mutex !");
150         return CA_WAIT_INVAL;
151     }
152
153     if (NULL == cond)
154     {
155         OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid condition !");
156         return CA_WAIT_INVAL;
157     }
158
159     GMutex *mutexLock = (GMutex *) mutex;
160     GCond *condition = (GCond *) cond;
161
162     if (microseconds == 0)
163     {
164         g_cond_wait(condition, mutexLock);
165         return CA_WAIT_SUCCESS;
166     }
167
168     gboolean bRet = g_cond_wait_until(condition, mutexLock, microseconds);
169     return bRet ? CA_WAIT_SUCCESS : CA_WAIT_TIMEDOUT;
170 }
171
172 void ca_cond_free(ca_cond cond)
173 {
174     if (NULL == cond)
175     {
176         OIC_LOG(ERROR, TAG, "ca_cond_free, Invalid condition !");
177         return;
178     }
179
180     GCond *condition = (GCond *) cond;
181     g_cond_clear(condition);
182     g_free(condition);
183 }
184
185