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;
179 OCThreadResult_t oc_thread_detach(oc_thread t)
181 OCThreadResult_t res = OC_THREAD_SUCCESS;
183 oc_thread_internal *threadInfo = (oc_thread_internal*) t;
184 int detachres = pthread_detach(threadInfo->thread);
187 OIC_LOG_V(ERROR, TAG, "Failed to detach thread with error %d", detachres);
188 res = OC_THREAD_DETACH_FAILURE;
195 OCThreadResult_t oc_thread_cancel(oc_thread t)
197 OCThreadResult_t res = OC_THREAD_SUCCESS;
198 oc_thread_internal *threadInfo = (oc_thread_internal*) t;
199 int ret = pthread_cancel(threadInfo->thread);
202 OIC_LOG_V(ERROR, TAG, "Failed to cancel thread with error %d", ret);
203 res = OC_THREAD_CANCEL_FAILURE;
210 oc_mutex oc_mutex_new(void)
212 oc_mutex retVal = NULL;
213 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) OICMalloc(sizeof(oc_mutex_internal));
214 if (NULL != mutexInfo)
216 // create the mutex with the attributes set
217 int ret=pthread_mutex_init(&(mutexInfo->mutex), PTHREAD_MUTEX_DEFAULT);
220 retVal = (oc_mutex) mutexInfo;
224 OIC_LOG_V(ERROR, TAG, "%s Failed to initialize mutex !", __func__);
230 OIC_LOG_V(ERROR, TAG, "%s Failed to allocate mutex!", __func__);
236 bool oc_mutex_free(oc_mutex mutex)
239 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
242 int ret = pthread_mutex_destroy(&mutexInfo->mutex);
250 OIC_LOG_V(ERROR, TAG, "%s Failed to free mutex !", __func__);
255 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
261 void oc_mutex_lock(oc_mutex mutex)
263 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
266 int ret = pthread_mutex_lock(&mutexInfo->mutex);
269 OIC_LOG_V(ERROR, TAG, "Pthread Mutex lock failed: %d", ret);
275 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
279 void oc_mutex_unlock(oc_mutex mutex)
281 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
284 #if defined(__TIZENRT__)
285 if (mutexInfo->mutex.pid == 0)
290 int ret = pthread_mutex_unlock(&mutexInfo->mutex);
293 OIC_LOG_V(ERROR, TAG, "Pthread Mutex unlock failed: %d", ret);
299 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
303 oc_cond oc_cond_new(void)
305 oc_cond retVal = NULL;
306 oc_cond_internal *eventInfo = (oc_cond_internal*) OICMalloc(sizeof(oc_cond_internal));
307 if (NULL != eventInfo)
309 int ret = pthread_condattr_init(&(eventInfo->condattr));
312 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
318 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
320 if (camutex_condattr_setclock)
322 ret = camutex_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
325 ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
326 #endif /* __ANDROID__ */
329 OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
331 pthread_condattr_destroy(&(eventInfo->condattr));
336 #endif /* defined(__ANDROID__) || _POSIX_TIMERS > 0 */
337 ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
340 retVal = (oc_cond) eventInfo;
344 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
345 pthread_condattr_destroy(&(eventInfo->condattr));
351 OIC_LOG_V(ERROR, TAG, "%s: Failed to allocate condition variable!", __func__);
357 void oc_cond_free(oc_cond cond)
359 oc_cond_internal *eventInfo = (oc_cond_internal*) cond;
360 if (eventInfo != NULL)
362 int ret = pthread_cond_destroy(&(eventInfo->cond));
363 int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
364 if (0 == ret && 0 == ret2)
370 OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
371 __func__, ret, ret2);
376 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
380 void oc_cond_signal(oc_cond cond)
382 oc_cond_internal *eventInfo = (oc_cond_internal*) cond;
383 if (eventInfo != NULL)
385 int ret = pthread_cond_signal(&(eventInfo->cond));
388 OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
393 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
397 void oc_cond_broadcast(oc_cond cond)
399 oc_cond_internal* eventInfo = (oc_cond_internal*) cond;
400 if (eventInfo != NULL)
402 int ret = pthread_cond_broadcast(&(eventInfo->cond));
405 OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
410 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
414 void oc_cond_wait(oc_cond cond, oc_mutex mutex)
416 oc_cond_wait_for(cond, mutex, 0L);
419 #ifndef TIMEVAL_TO_TIMESPEC
420 #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
421 (ts)->tv_sec = (tv)->tv_sec; \
422 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
426 struct timespec oc_get_current_time()
428 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
430 clock_gettime(CLOCK_MONOTONIC, &ts);
434 gettimeofday(&tv, NULL);
436 TIMEVAL_TO_TIMESPEC(&tv, &ts);
441 void oc_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
443 time_t secPart = microseconds/USECS_PER_SEC;
444 uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
445 uint64_t totalNs = ts->tv_nsec + nsecPart;
446 time_t secOfNs = totalNs/NANOSECS_PER_SEC;
448 ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
449 ts->tv_sec += secPart + secOfNs;
452 OCWaitResult_t oc_cond_wait_for(oc_cond cond, oc_mutex mutex, uint64_t microseconds)
454 OCWaitResult_t retVal = OC_WAIT_INVAL;
456 oc_cond_internal *eventInfo = (oc_cond_internal*) cond;
457 oc_mutex_internal *mutexInfo = (oc_mutex_internal*) mutex;
459 if (NULL == mutexInfo)
461 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
462 return OC_WAIT_INVAL;
465 if (NULL == eventInfo)
467 OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
468 return OC_WAIT_INVAL;
471 if (microseconds > 0)
474 struct timespec abstime = { .tv_sec = 0 };
477 if (camutex_cond_timedwait_relative)
479 abstime.tv_sec = microseconds / USECS_PER_SEC;
480 abstime.tv_nsec = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
481 //Wait for the given time
482 ret = camutex_cond_timedwait_relative(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
486 abstime = oc_get_current_time();
487 oc_add_microseconds_to_timespec(&abstime, microseconds);
489 //Wait for the given time
490 ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
497 retVal = OC_WAIT_SUCCESS;
500 retVal = OC_WAIT_TIMEDOUT;
503 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
504 retVal = OC_WAIT_INVAL;
507 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
508 retVal = OC_WAIT_INVAL;
515 int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
516 retVal = ret == 0 ? OC_WAIT_SUCCESS : OC_WAIT_INVAL;