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
39 #include "iotivity_config.h"
53 #ifdef HAVE_SYS_TIME_H
59 #include <oic_malloc.h>
64 * Logging tag for module name
66 #define TAG PCF("UMUTEX")
70 * Android has pthread_condattr_setclock() only in version >= 5.0, older
71 * version do have a function called __pthread_cond_timedwait_relative()
72 * which waits *for* the given timespec, this function is not visible in
73 * android version >= 5.0 anymore. This is the same way as it is handled in
75 * http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/thread/qwaitcondition_unix.cpp?h=v5.5.0#n54
77 static int camutex_condattr_setclock(pthread_condattr_t *, clockid_t)
78 __attribute__ ((weakref("pthread_condattr_setclock")));
80 static int camutex_cond_timedwait_relative(pthread_cond_t*, pthread_mutex_t*, const struct timespec*)
81 __attribute__ ((weakref("__pthread_cond_timedwait_relative")));
82 #endif /* __ANDROID__ */
84 static const uint64_t USECS_PER_SEC = 1000000;
85 static const uint64_t NANOSECS_PER_USECS = 1000;
86 static const uint64_t NANOSECS_PER_SEC = 1000000000L;
88 typedef struct _tagMutexInfo_t
90 pthread_mutex_t mutex;
93 typedef struct _tagEventInfo_t
96 pthread_condattr_t condattr;
99 typedef struct _tagThreadInfo_t
102 pthread_attr_t threadattr;
103 } oc_thread_internal;
106 OCThreadResult_t oc_thread_new(oc_thread *t, void *(*start_routine)(void *), void *arg)
108 OCThreadResult_t oc_thread_new(oc_thread *t, void *(*start_routine)(void *), void *arg,
109 const char *task_name, int stack_size)
112 OCThreadResult_t res = OC_THREAD_SUCCESS;
113 oc_thread_internal *threadInfo = (oc_thread_internal*)OICMalloc(sizeof(oc_thread_internal));
114 if (NULL != threadInfo)
118 pthread_attr_init(&attr);
119 struct sched_param prio;
120 prio.sched_priority = 90;
121 (void)pthread_attr_setschedparam(&attr, &prio);
122 (void)pthread_attr_setstacksize(&attr, stack_size);
123 int result = pthread_create(&threadInfo->thread, &attr, start_routine, arg);
124 pthread_setname_np(threadInfo->thread, task_name);
126 int result = pthread_create(&threadInfo->thread, NULL, start_routine, arg);
130 res = OC_THREAD_CREATE_FAILURE;
133 OIC_LOG_V(ERROR, TAG, "%s: pthread_create failed", __func__);
137 *t = (oc_thread)threadInfo;
142 OIC_LOG_V(ERROR, TAG, "%s Failed to allocate thread!", __func__);
144 res = OC_THREAD_ALLOCATION_FAILURE;
149 OCThreadResult_t oc_thread_free(oc_thread t)
151 OCThreadResult_t res = OC_THREAD_SUCCESS;
152 oc_thread_internal *threadInfo = (oc_thread_internal*) t;
159 OIC_LOG_V(ERROR, TAG, "%s Invalid thread !", __func__);
160 res = OC_THREAD_INVALID;
165 OCThreadResult_t oc_thread_wait(oc_thread t)
167 OCThreadResult_t res = OC_THREAD_SUCCESS;
168 oc_thread_internal *threadInfo = (oc_thread_internal*) t;
169 int joinres = pthread_join(threadInfo->thread, NULL);
172 OIC_LOG_V(ERROR, TAG, "Failed to join thread with error %d", joinres);
173 res = OC_THREAD_WAIT_FAILURE;
180 OCThreadResult_t oc_thread_cancel(oc_thread t)
182 OCThreadResult_t res = OC_THREAD_SUCCESS;
183 oc_thread_internal *threadInfo = (oc_thread_internal*) t;
184 int ret = pthread_cancel(threadInfo->thread);
187 OIC_LOG_V(ERROR, TAG, "Failed to cancel thread with error %d", ret);
188 res = OC_THREAD_CANCEL_FAILURE;
195 oc_mutex oc_mutex_new(void)
197 oc_mutex retVal = NULL;
198 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) OICMalloc(sizeof(oc_mutex_internal));
199 if (NULL != mutexInfo)
201 // create the mutex with the attributes set
202 int ret=pthread_mutex_init(&(mutexInfo->mutex), PTHREAD_MUTEX_DEFAULT);
205 retVal = (oc_mutex) mutexInfo;
209 OIC_LOG_V(ERROR, TAG, "%s Failed to initialize mutex !", __func__);
215 OIC_LOG_V(ERROR, TAG, "%s Failed to allocate mutex!", __func__);
221 bool oc_mutex_free(oc_mutex mutex)
224 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
227 int ret = pthread_mutex_destroy(&mutexInfo->mutex);
235 OIC_LOG_V(ERROR, TAG, "%s Failed to free mutex !", __func__);
240 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
246 void oc_mutex_lock(oc_mutex mutex)
248 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
251 int ret = pthread_mutex_lock(&mutexInfo->mutex);
254 OIC_LOG_V(ERROR, TAG, "Pthread Mutex lock failed: %d", ret);
260 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
264 void oc_mutex_unlock(oc_mutex mutex)
266 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
269 #if defined(__TIZENRT__)
270 if (mutexInfo->mutex.pid == 0)
275 int ret = pthread_mutex_unlock(&mutexInfo->mutex);
278 OIC_LOG_V(ERROR, TAG, "Pthread Mutex unlock failed: %d", ret);
284 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
288 oc_cond oc_cond_new(void)
290 oc_cond retVal = NULL;
291 oc_cond_internal *eventInfo = (oc_cond_internal*) OICMalloc(sizeof(oc_cond_internal));
292 if (NULL != eventInfo)
294 int ret = pthread_condattr_init(&(eventInfo->condattr));
297 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
303 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
305 if (camutex_condattr_setclock)
307 ret = camutex_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
310 ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
311 #endif /* __ANDROID__ */
314 OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
316 pthread_condattr_destroy(&(eventInfo->condattr));
321 #endif /* defined(__ANDROID__) || _POSIX_TIMERS > 0 */
322 ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
325 retVal = (oc_cond) eventInfo;
329 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
330 pthread_condattr_destroy(&(eventInfo->condattr));
336 OIC_LOG_V(ERROR, TAG, "%s: Failed to allocate condition variable!", __func__);
342 void oc_cond_free(oc_cond cond)
344 oc_cond_internal *eventInfo = (oc_cond_internal*) cond;
345 if (eventInfo != NULL)
347 int ret = pthread_cond_destroy(&(eventInfo->cond));
348 int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
349 if (0 == ret && 0 == ret2)
355 OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
356 __func__, ret, ret2);
361 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
365 void oc_cond_signal(oc_cond cond)
367 oc_cond_internal *eventInfo = (oc_cond_internal*) cond;
368 if (eventInfo != NULL)
370 int ret = pthread_cond_signal(&(eventInfo->cond));
373 OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
378 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
382 void oc_cond_broadcast(oc_cond cond)
384 oc_cond_internal* eventInfo = (oc_cond_internal*) cond;
385 if (eventInfo != NULL)
387 int ret = pthread_cond_broadcast(&(eventInfo->cond));
390 OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
395 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
399 void oc_cond_wait(oc_cond cond, oc_mutex mutex)
401 oc_cond_wait_for(cond, mutex, 0L);
404 #ifndef TIMEVAL_TO_TIMESPEC
405 #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
406 (ts)->tv_sec = (tv)->tv_sec; \
407 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
411 struct timespec oc_get_current_time()
413 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
415 clock_gettime(CLOCK_MONOTONIC, &ts);
419 gettimeofday(&tv, NULL);
421 TIMEVAL_TO_TIMESPEC(&tv, &ts);
426 void oc_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
428 time_t secPart = microseconds/USECS_PER_SEC;
429 uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
430 uint64_t totalNs = ts->tv_nsec + nsecPart;
431 time_t secOfNs = totalNs/NANOSECS_PER_SEC;
433 ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
434 ts->tv_sec += secPart + secOfNs;
437 OCWaitResult_t oc_cond_wait_for(oc_cond cond, oc_mutex mutex, uint64_t microseconds)
439 OCWaitResult_t retVal = OC_WAIT_INVAL;
441 oc_cond_internal *eventInfo = (oc_cond_internal*) cond;
442 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
444 if (NULL == mutexInfo)
446 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
447 return OC_WAIT_INVAL;
450 if (NULL == eventInfo)
452 OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
453 return OC_WAIT_INVAL;
456 if (microseconds > 0)
459 struct timespec abstime = { .tv_sec = 0 };
462 if (camutex_cond_timedwait_relative)
464 abstime.tv_sec = microseconds / USECS_PER_SEC;
465 abstime.tv_nsec = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
466 //Wait for the given time
467 ret = camutex_cond_timedwait_relative(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
471 abstime = oc_get_current_time();
472 oc_add_microseconds_to_timespec(&abstime, microseconds);
474 //Wait for the given time
475 ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
482 retVal = OC_WAIT_SUCCESS;
485 retVal = OC_WAIT_TIMEDOUT;
488 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
489 retVal = OC_WAIT_INVAL;
492 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
493 retVal = OC_WAIT_INVAL;
500 int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
501 retVal = ret == 0 ? OC_WAIT_SUCCESS : OC_WAIT_INVAL;