Added connectivity files for full code review
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / umutex.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
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  * @file    umutex.c
21  * @brief   This file provides APIs related to mutex and semaphores
22  */
23
24 #include "umutex.h"
25 #include <glib.h>
26 #include <string.h>
27 #include "logger.h"
28
29 /**
30  * @def TAG
31  * @brief Logging tag for module name
32  */
33 #define TAG PCF("UMUTEX")
34
35 u_mutex u_mutex_new(void)
36 {
37     GMutex *mutexLock = g_new(GMutex, 1);
38     g_mutex_init(mutexLock);
39     return (u_mutex) mutexLock;
40 }
41
42 void u_mutex_lock(u_mutex mutex)
43 {
44     if (NULL == mutex)
45     {
46         OIC_LOG_V(ERROR, TAG , "u_mutex_lock, Invalid mutex !");
47         return;
48     }
49
50     GMutex *mutexLock = (GMutex *) mutex;
51     g_mutex_lock(mutexLock);
52 }
53
54 bool u_mutex_trylock(u_mutex mutex)
55 {
56     if (NULL == mutex)
57     {
58         OIC_LOG_V(ERROR, TAG, "u_mutex_trylock, Invalid mutex !");
59         return false;
60     }
61
62     GMutex *mutexLock = (GMutex *) mutex;
63
64     return(g_mutex_trylock(mutexLock));
65 }
66
67 void u_mutex_unlock(u_mutex mutex)
68 {
69     if (NULL == mutex)
70     {
71         OIC_LOG_V(ERROR, TAG, "u_mutex_unlock, Invalid mutex !");
72         return;
73     }
74
75     GMutex *mutexLock = (GMutex *) mutex;
76     g_mutex_unlock(mutexLock);
77 }
78
79 void u_mutex_free(u_mutex mutex)
80 {
81     if (NULL == mutex)
82     {
83         OIC_LOG_V(ERROR, TAG, "u_mutex_free, Invalid mutex !");
84         return;
85     }
86
87     GMutex *mutexLock = (GMutex *) mutex;
88     g_mutex_clear(mutexLock);
89     g_free(mutexLock);
90 }
91
92 u_cond u_cond_new(void)
93 {
94     GCond *condition = g_new(GCond, 1);
95     g_cond_init(condition);
96     return (u_cond) condition;
97 }
98
99 void u_cond_signal(u_cond cond)
100 {
101     if (NULL == cond)
102     {
103         OIC_LOG_V(ERROR, TAG, "u_cond_signal, Invalid condition !");
104         return;
105     }
106
107     GCond *condition = (GCond *) cond;
108     g_cond_signal(condition);
109 }
110
111 void u_cond_broadcast(u_cond cond)
112 {
113     if (NULL == cond)
114     {
115         OIC_LOG_V(ERROR, TAG, "u_cond_broadcast, Invalid condition !");
116         return;
117     }
118
119     GCond *condition = (GCond *) cond;
120     g_cond_broadcast(condition);
121 }
122
123 void u_cond_wait(u_cond cond, u_mutex mutex)
124 {
125     if (NULL == mutex)
126     {
127         OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid mutex !");
128         return;
129     }
130
131     if (NULL == cond)
132     {
133         OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid condition !");
134         return;
135     }
136
137     GMutex *mutexLock = (GMutex *) mutex;
138     GCond *condition = (GCond *) cond;
139     g_cond_wait(condition, mutexLock);
140 }
141
142 void u_cond_wait_until(u_cond cond, u_mutex mutex, int32_t microseconds)
143 {
144     if (NULL == mutex)
145     {
146         OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid mutex !");
147         return;
148     }
149
150     if (NULL == cond)
151     {
152         OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid condition !");
153         return;
154     }
155
156     GMutex *mutexLock = (GMutex *) mutex;
157     GCond *condition = (GCond *) cond;
158
159     if (microseconds <= 0)
160     {
161         g_cond_wait(condition, mutexLock);
162         return;
163     }
164
165     gint64 end_time;
166     end_time = g_get_monotonic_time() + microseconds;
167
168     g_cond_wait_until(condition, mutexLock, end_time);
169 }
170
171 void u_cond_free(u_cond cond)
172 {
173     if (NULL == cond)
174     {
175         OIC_LOG_V(ERROR, TAG, "u_cond_free, Invalid condition !");
176         return;
177     }
178
179     GCond *condition = (GCond *) cond;
180     g_cond_clear(condition);
181     g_free(condition);
182 }
183
184