1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 //*********************************************************************
26 * This file provides APIs related to mutex and semaphores.
29 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
30 // causes header files to expose definitions
31 // corresponding to the POSIX.1b, Real-time extensions
32 // (IEEE Std 1003.1b-1993) specification
34 // For this specific file, see use of clock_gettime and PTHREAD_MUTEX_DEFAULT
35 #ifndef _POSIX_C_SOURCE
36 #define _POSIX_C_SOURCE 200809L
46 #include <oic_malloc.h>
53 * Logging tag for module name
55 #define TAG PCF("UMUTEX")
57 static const uint64_t USECS_PER_SEC = 1000000;
58 static const uint64_t NANOSECS_PER_USECS = 1000;
59 static const uint64_t NANOSECS_PER_SEC = 1000000000L;
61 typedef struct _tagMutexInfo_t
63 pthread_mutex_t mutex;
66 typedef struct _tagEventInfo_t
69 pthread_condattr_t condattr;
72 ca_mutex ca_mutex_new(void)
74 ca_mutex retVal = NULL;
75 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) OICMalloc(sizeof(ca_mutex_internal));
76 if (NULL != mutexInfo)
78 // create the mutex with the attributes set
79 int ret=pthread_mutex_init(&(mutexInfo->mutex), PTHREAD_MUTEX_DEFAULT);
82 retVal = (ca_mutex) mutexInfo;
86 OIC_LOG_V(ERROR, TAG, "%s Failed to initialize mutex !", __func__);
94 bool ca_mutex_free(ca_mutex mutex)
98 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
101 int ret = pthread_mutex_destroy(&mutexInfo->mutex);
109 OIC_LOG_V(ERROR, TAG, "%s Failed to free mutex !", __func__);
114 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
120 void ca_mutex_lock(ca_mutex mutex)
122 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
125 int ret = pthread_mutex_lock(&mutexInfo->mutex);
131 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
136 bool ca_mutex_trylock(ca_mutex mutex)
140 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
146 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
148 int result = pthread_mutex_trylock(&mutexInfo->mutex);
157 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
167 void ca_mutex_unlock(ca_mutex mutex)
169 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
172 int ret = pthread_mutex_unlock(&mutexInfo->mutex);
178 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
183 ca_cond ca_cond_new(void)
185 ca_cond retVal = NULL;
186 ca_cond_internal *eventInfo = (ca_cond_internal*) OICMalloc(sizeof(ca_cond_internal));
187 if (NULL != eventInfo)
189 int ret = pthread_condattr_init(&(eventInfo->condattr));
192 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
197 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
198 ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
202 OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
207 ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
210 retVal = (ca_cond) eventInfo;
214 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
222 void ca_cond_free(ca_cond cond)
224 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
225 if (eventInfo != NULL)
227 int ret = pthread_cond_destroy(&(eventInfo->cond));
228 int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
229 if (0 == ret && 0 == ret2)
235 OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
236 __func__, ret, ret2);
241 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
245 void ca_cond_signal(ca_cond cond)
247 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
248 if (eventInfo != NULL)
250 int ret = pthread_cond_signal(&(eventInfo->cond));
253 OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
258 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
262 void ca_cond_broadcast(ca_cond cond)
264 ca_cond_internal* eventInfo = (ca_cond_internal*) cond;
265 if (eventInfo != NULL)
267 int ret = pthread_cond_broadcast(&(eventInfo->cond));
270 OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
275 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
279 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
281 ca_cond_wait_for(cond, mutex, 0L);
284 struct timespec ca_get_current_time()
286 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
288 clock_gettime(CLOCK_MONOTONIC, &ts);
292 gettimeofday(&tv, NULL);
294 TIMEVAL_TO_TIMESPEC(&tv, &ts);
299 void ca_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
301 time_t secPart = microseconds/USECS_PER_SEC;
302 uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
303 uint64_t totalNs = ts->tv_nsec + nsecPart;
304 time_t secOfNs = totalNs/NANOSECS_PER_SEC;
306 ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
307 ts->tv_sec += secPart + secOfNs;
310 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
312 CAWaitResult_t retVal = CA_WAIT_INVAL;
314 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
315 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
317 if (NULL == mutexInfo)
319 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
320 return CA_WAIT_INVAL;
323 if (NULL == eventInfo)
325 OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
326 return CA_WAIT_INVAL;
329 if (microseconds > 0)
331 struct timespec abstime = ca_get_current_time();
332 ca_add_microseconds_to_timespec(&abstime, microseconds);
334 //Wait for the given time
335 int ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
340 retVal = CA_WAIT_SUCCESS;
343 retVal = CA_WAIT_TIMEDOUT;
346 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
347 retVal = CA_WAIT_INVAL;
350 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
351 retVal = CA_WAIT_INVAL;
358 int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
359 retVal = ret == 0 ? CA_WAIT_SUCCESS : CA_WAIT_INVAL;