1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 ******************************************************************/
21 * @brief This file provides APIs related to mutex and semaphores
31 * @brief Logging tag for module name
33 #define TAG PCF("UMUTEX")
35 u_mutex u_mutex_new(void)
37 GMutex *mutexLock = g_new(GMutex, 1);
38 g_mutex_init(mutexLock);
39 return (u_mutex) mutexLock;
42 void u_mutex_lock(u_mutex mutex)
46 OIC_LOG_V(ERROR, TAG , "u_mutex_lock, Invalid mutex !");
50 GMutex *mutexLock = (GMutex *) mutex;
51 g_mutex_lock(mutexLock);
54 bool u_mutex_trylock(u_mutex mutex)
58 OIC_LOG_V(ERROR, TAG, "u_mutex_trylock, Invalid mutex !");
62 GMutex *mutexLock = (GMutex *) mutex;
64 return(g_mutex_trylock(mutexLock));
67 void u_mutex_unlock(u_mutex mutex)
71 OIC_LOG_V(ERROR, TAG, "u_mutex_unlock, Invalid mutex !");
75 GMutex *mutexLock = (GMutex *) mutex;
76 g_mutex_unlock(mutexLock);
79 void u_mutex_free(u_mutex mutex)
83 OIC_LOG_V(ERROR, TAG, "u_mutex_free, Invalid mutex !");
87 GMutex *mutexLock = (GMutex *) mutex;
88 g_mutex_clear(mutexLock);
92 u_cond u_cond_new(void)
94 GCond *condition = g_new(GCond, 1);
95 g_cond_init(condition);
96 return (u_cond) condition;
99 void u_cond_signal(u_cond cond)
103 OIC_LOG_V(ERROR, TAG, "u_cond_signal, Invalid condition !");
107 GCond *condition = (GCond *) cond;
108 g_cond_signal(condition);
111 void u_cond_broadcast(u_cond cond)
115 OIC_LOG_V(ERROR, TAG, "u_cond_broadcast, Invalid condition !");
119 GCond *condition = (GCond *) cond;
120 g_cond_broadcast(condition);
123 void u_cond_wait(u_cond cond, u_mutex mutex)
127 OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid mutex !");
133 OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid condition !");
137 GMutex *mutexLock = (GMutex *) mutex;
138 GCond *condition = (GCond *) cond;
139 g_cond_wait(condition, mutexLock);
142 void u_cond_wait_until(u_cond cond, u_mutex mutex, int32_t microseconds)
146 OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid mutex !");
152 OIC_LOG_V(ERROR, TAG, "u_cond_wait, Invalid condition !");
156 GMutex *mutexLock = (GMutex *) mutex;
157 GCond *condition = (GCond *) cond;
159 if (microseconds <= 0)
161 g_cond_wait(condition, mutexLock);
166 end_time = g_get_monotonic_time() + microseconds;
168 g_cond_wait_until(condition, mutexLock, end_time);
171 void u_cond_free(u_cond cond)
175 OIC_LOG_V(ERROR, TAG, "u_cond_free, Invalid condition !");
179 GCond *condition = (GCond *) cond;
180 g_cond_clear(condition);