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 ca_mutex ca_mutex_new(void)
39 GMutex *mutexLock = g_new(GMutex, 1);
40 g_mutex_init(mutexLock);
41 return (ca_mutex) mutexLock;
44 void ca_mutex_lock(ca_mutex mutex)
48 OIC_LOG(ERROR, TAG , "ca_mutex_lock, Invalid mutex !");
52 GMutex *mutexLock = (GMutex *) mutex;
53 g_mutex_lock(mutexLock);
56 bool ca_mutex_trylock(ca_mutex mutex)
60 OIC_LOG(ERROR, TAG, "ca_mutex_trylock, Invalid mutex !");
64 GMutex *mutexLock = (GMutex *) mutex;
66 return(g_mutex_trylock(mutexLock));
69 void ca_mutex_unlock(ca_mutex mutex)
73 OIC_LOG(ERROR, TAG, "ca_mutex_unlock, Invalid mutex !");
77 GMutex *mutexLock = (GMutex *) mutex;
78 g_mutex_unlock(mutexLock);
81 bool ca_mutex_free(ca_mutex mutex)
85 OIC_LOG(ERROR, TAG, "ca_mutex_free, Invalid mutex !");
89 GMutex *mutexLock = (GMutex *) mutex;
90 g_mutex_clear(mutexLock);
95 ca_cond ca_cond_new(void)
97 GCond *condition = g_new(GCond, 1);
98 g_cond_init(condition);
99 return (ca_cond) condition;
102 void ca_cond_signal(ca_cond cond)
106 OIC_LOG(ERROR, TAG, "ca_cond_signal, Invalid condition !");
110 GCond *condition = (GCond *) cond;
111 g_cond_signal(condition);
114 void ca_cond_broadcast(ca_cond cond)
118 OIC_LOG(ERROR, TAG, "ca_cond_broadcast, Invalid condition !");
122 GCond *condition = (GCond *) cond;
123 g_cond_broadcast(condition);
126 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
130 OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid mutex !");
136 OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid condition !");
140 GMutex *mutexLock = (GMutex *) mutex;
141 GCond *condition = (GCond *) cond;
142 g_cond_wait(condition, mutexLock);
145 CAWaitResult_t ca_cond_wait_until(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
149 OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid mutex !");
150 return CA_WAIT_INVAL;
155 OIC_LOG(ERROR, TAG, "ca_cond_wait, Invalid condition !");
156 return CA_WAIT_INVAL;
159 GMutex *mutexLock = (GMutex *) mutex;
160 GCond *condition = (GCond *) cond;
162 if (microseconds == 0)
164 g_cond_wait(condition, mutexLock);
165 return CA_WAIT_SUCCESS;
168 gboolean bRet = g_cond_wait_until(condition, mutexLock, microseconds);
169 return bRet ? CA_WAIT_SUCCESS : CA_WAIT_TIMEDOUT;
172 void ca_cond_free(ca_cond cond)
176 OIC_LOG(ERROR, TAG, "ca_cond_free, Invalid condition !");
180 GCond *condition = (GCond *) cond;
181 g_cond_clear(condition);