1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 * This file provides APIs related to mutex and semaphores.
33 * @brief Logging tag for module name
35 #define TAG PCF("UMUTEX")
37 u_mutex u_mutex_new(void)
39 GMutex *mutexLock = g_new(GMutex, 1);
40 g_mutex_init(mutexLock);
41 return (u_mutex) mutexLock;
44 void u_mutex_lock(u_mutex mutex)
48 OIC_LOG(ERROR, TAG , "u_mutex_lock, Invalid mutex !");
52 GMutex *mutexLock = (GMutex *) mutex;
53 g_mutex_lock(mutexLock);
56 bool u_mutex_trylock(u_mutex mutex)
60 OIC_LOG(ERROR, TAG, "u_mutex_trylock, Invalid mutex !");
64 GMutex *mutexLock = (GMutex *) mutex;
66 return(g_mutex_trylock(mutexLock));
69 void u_mutex_unlock(u_mutex mutex)
73 OIC_LOG(ERROR, TAG, "u_mutex_unlock, Invalid mutex !");
77 GMutex *mutexLock = (GMutex *) mutex;
78 g_mutex_unlock(mutexLock);
81 void u_mutex_free(u_mutex mutex)
85 OIC_LOG(ERROR, TAG, "u_mutex_free, Invalid mutex !");
89 GMutex *mutexLock = (GMutex *) mutex;
90 g_mutex_clear(mutexLock);
94 u_cond u_cond_new(void)
96 GCond *condition = g_new(GCond, 1);
97 g_cond_init(condition);
98 return (u_cond) condition;
101 void u_cond_signal(u_cond cond)
105 OIC_LOG(ERROR, TAG, "u_cond_signal, Invalid condition !");
109 GCond *condition = (GCond *) cond;
110 g_cond_signal(condition);
113 void u_cond_broadcast(u_cond cond)
117 OIC_LOG(ERROR, TAG, "u_cond_broadcast, Invalid condition !");
121 GCond *condition = (GCond *) cond;
122 g_cond_broadcast(condition);
125 void u_cond_wait(u_cond cond, u_mutex mutex)
129 OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid mutex !");
135 OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid condition !");
139 GMutex *mutexLock = (GMutex *) mutex;
140 GCond *condition = (GCond *) cond;
141 g_cond_wait(condition, mutexLock);
144 void u_cond_wait_until(u_cond cond, u_mutex mutex, int32_t microseconds)
148 OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid mutex !");
154 OIC_LOG(ERROR, TAG, "u_cond_wait, Invalid condition !");
158 GMutex *mutexLock = (GMutex *) mutex;
159 GCond *condition = (GCond *) cond;
161 if (microseconds <= 0)
163 g_cond_wait(condition, mutexLock);
168 end_time = g_get_monotonic_time() + microseconds;
170 g_cond_wait_until(condition, mutexLock, end_time);
173 void u_cond_free(u_cond cond)
177 OIC_LOG(ERROR, TAG, "u_cond_free, Invalid condition !");
181 GCond *condition = (GCond *) cond;
182 g_cond_clear(condition);