Merge branch 'connectivity-abstraction' to master
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / umutex.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 "umutex.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 u_mutex u_mutex_new(void)
38 {
39     GMutex *mutexLock = g_new(GMutex, 1);
40     g_mutex_init(mutexLock);
41     return (u_mutex) mutexLock;
42 }
43
44 void u_mutex_lock(u_mutex mutex)
45 {
46     if (NULL == mutex)
47     {
48         OIC_LOG(ERROR, TAG , "u_mutex_lock, Invalid mutex !");
49         return;
50     }
51
52     GMutex *mutexLock = (GMutex *) mutex;
53     g_mutex_lock(mutexLock);
54 }
55
56 bool u_mutex_trylock(u_mutex mutex)
57 {
58     if (NULL == mutex)
59     {
60         OIC_LOG(ERROR, TAG, "u_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 u_mutex_unlock(u_mutex mutex)
70 {
71     if (NULL == mutex)
72     {
73         OIC_LOG(ERROR, TAG, "u_mutex_unlock, Invalid mutex !");
74         return;
75     }
76
77     GMutex *mutexLock = (GMutex *) mutex;
78     g_mutex_unlock(mutexLock);
79 }
80
81 void u_mutex_free(u_mutex mutex)
82 {
83     if (NULL == mutex)
84     {
85         OIC_LOG(ERROR, TAG, "u_mutex_free, Invalid mutex !");
86         return;
87     }
88
89     GMutex *mutexLock = (GMutex *) mutex;
90     g_mutex_clear(mutexLock);
91     g_free(mutexLock);
92 }
93
94 u_cond u_cond_new(void)
95 {
96     GCond *condition = g_new(GCond, 1);
97     g_cond_init(condition);
98     return (u_cond) condition;
99 }
100
101 void u_cond_signal(u_cond cond)
102 {
103     if (NULL == cond)
104     {
105         OIC_LOG(ERROR, TAG, "u_cond_signal, Invalid condition !");
106         return;
107     }
108
109     GCond *condition = (GCond *) cond;
110     g_cond_signal(condition);
111 }
112
113 void u_cond_broadcast(u_cond cond)
114 {
115     if (NULL == cond)
116     {
117         OIC_LOG(ERROR, TAG, "u_cond_broadcast, Invalid condition !");
118         return;
119     }
120
121     GCond *condition = (GCond *) cond;
122     g_cond_broadcast(condition);
123 }
124
125 void u_cond_wait(u_cond cond, u_mutex mutex)
126 {
127     if (NULL == mutex)
128     {
129         OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid mutex !");
130         return;
131     }
132
133     if (NULL == cond)
134     {
135         OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid condition !");
136         return;
137     }
138
139     GMutex *mutexLock = (GMutex *) mutex;
140     GCond *condition = (GCond *) cond;
141     g_cond_wait(condition, mutexLock);
142 }
143
144 void u_cond_wait_until(u_cond cond, u_mutex mutex, int32_t microseconds)
145 {
146     if (NULL == mutex)
147     {
148         OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid mutex !");
149         return;
150     }
151
152     if (NULL == cond)
153     {
154         OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid condition !");
155         return;
156     }
157
158     GMutex *mutexLock = (GMutex *) mutex;
159     GCond *condition = (GCond *) cond;
160
161     if (microseconds <= 0)
162     {
163         g_cond_wait(condition, mutexLock);
164         return;
165     }
166
167     gint64 end_time;
168     end_time = g_get_monotonic_time() + microseconds;
169
170     g_cond_wait_until(condition, mutexLock, end_time);
171 }
172
173 void u_cond_free(u_cond cond)
174 {
175     if (NULL == cond)
176     {
177         OIC_LOG(ERROR, TAG, "u_cond_free, Invalid condition !");
178         return;
179     }
180
181     GCond *condition = (GCond *) cond;
182     g_cond_clear(condition);
183     g_free(condition);
184 }
185
186