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);
129 OIC_LOG_V(ERROR, TAG, "Pthread Mutex lock failed: %d", ret);
135 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
140 bool ca_mutex_trylock(ca_mutex mutex)
144 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
150 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
152 int result = pthread_mutex_trylock(&mutexInfo->mutex);
161 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
171 void ca_mutex_unlock(ca_mutex mutex)
173 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
176 int ret = pthread_mutex_unlock(&mutexInfo->mutex);
181 OIC_LOG_V(ERROR, TAG, "Pthread Mutex unlock failed: %d", ret);
188 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
193 ca_cond ca_cond_new(void)
195 ca_cond retVal = NULL;
196 ca_cond_internal *eventInfo = (ca_cond_internal*) OICMalloc(sizeof(ca_cond_internal));
197 if (NULL != eventInfo)
199 int ret = pthread_condattr_init(&(eventInfo->condattr));
202 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
208 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
209 ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
213 OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
215 pthread_condattr_destroy(&(eventInfo->condattr));
220 ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
223 retVal = (ca_cond) eventInfo;
227 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
228 pthread_condattr_destroy(&(eventInfo->condattr));
236 void ca_cond_free(ca_cond cond)
238 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
239 if (eventInfo != NULL)
241 int ret = pthread_cond_destroy(&(eventInfo->cond));
242 int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
243 if (0 == ret && 0 == ret2)
249 OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
250 __func__, ret, ret2);
255 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
259 void ca_cond_signal(ca_cond cond)
261 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
262 if (eventInfo != NULL)
264 int ret = pthread_cond_signal(&(eventInfo->cond));
267 OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
272 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
276 void ca_cond_broadcast(ca_cond cond)
278 ca_cond_internal* eventInfo = (ca_cond_internal*) cond;
279 if (eventInfo != NULL)
281 int ret = pthread_cond_broadcast(&(eventInfo->cond));
284 OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
289 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
293 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
295 ca_cond_wait_for(cond, mutex, 0L);
298 struct timespec ca_get_current_time()
300 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
302 clock_gettime(CLOCK_MONOTONIC, &ts);
306 gettimeofday(&tv, NULL);
308 TIMEVAL_TO_TIMESPEC(&tv, &ts);
313 void ca_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
315 time_t secPart = microseconds/USECS_PER_SEC;
316 uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
317 uint64_t totalNs = ts->tv_nsec + nsecPart;
318 time_t secOfNs = totalNs/NANOSECS_PER_SEC;
320 ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
321 ts->tv_sec += secPart + secOfNs;
324 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
326 CAWaitResult_t retVal = CA_WAIT_INVAL;
328 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
329 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
331 if (NULL == mutexInfo)
333 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
334 return CA_WAIT_INVAL;
337 if (NULL == eventInfo)
339 OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
340 return CA_WAIT_INVAL;
343 if (microseconds > 0)
345 struct timespec abstime = ca_get_current_time();
346 ca_add_microseconds_to_timespec(&abstime, microseconds);
348 //Wait for the given time
349 int ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
354 retVal = CA_WAIT_SUCCESS;
357 retVal = CA_WAIT_TIMEDOUT;
360 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
361 retVal = CA_WAIT_INVAL;
364 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
365 retVal = CA_WAIT_INVAL;
372 int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
373 retVal = ret == 0 ? CA_WAIT_SUCCESS : CA_WAIT_INVAL;