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")
59 * Android has pthread_condattr_setclock() only in version >= 5.0, older
60 * version do have a function called __pthread_cond_timedwait_relative()
61 * which waits *for* the given timespec, this function is not visible in
62 * android version >= 5.0 anymore. This is the same way as it is handled in
64 * http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/thread/qwaitcondition_unix.cpp?h=v5.5.0#n54
66 static int camutex_condattr_setclock(pthread_condattr_t *, clockid_t)
67 __attribute__ ((weakref("pthread_condattr_setclock")));
69 static int camutex_cond_timedwait_relative(pthread_cond_t*, pthread_mutex_t*, const struct timespec*)
70 __attribute__ ((weakref("__pthread_cond_timedwait_relative")));
71 #endif /* __ANDROID__ */
73 static const uint64_t USECS_PER_SEC = 1000000;
74 static const uint64_t NANOSECS_PER_USECS = 1000;
75 static const uint64_t NANOSECS_PER_SEC = 1000000000L;
77 typedef struct _tagMutexInfo_t
79 pthread_mutex_t mutex;
82 typedef struct _tagEventInfo_t
85 pthread_condattr_t condattr;
88 ca_mutex ca_mutex_new(void)
90 ca_mutex retVal = NULL;
91 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) OICMalloc(sizeof(ca_mutex_internal));
92 if (NULL != mutexInfo)
94 // create the mutex with the attributes set
95 int ret=pthread_mutex_init(&(mutexInfo->mutex), PTHREAD_MUTEX_DEFAULT);
98 retVal = (ca_mutex) mutexInfo;
102 OIC_LOG_V(ERROR, TAG, "%s Failed to initialize mutex !", __func__);
110 bool ca_mutex_free(ca_mutex mutex)
114 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
117 int ret = pthread_mutex_destroy(&mutexInfo->mutex);
125 OIC_LOG_V(ERROR, TAG, "%s Failed to free mutex !", __func__);
130 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
136 void ca_mutex_lock(ca_mutex mutex)
138 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
141 int ret = pthread_mutex_lock(&mutexInfo->mutex);
144 OIC_LOG_V(ERROR, TAG, "Pthread Mutex lock failed: %d", ret);
150 OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
155 void ca_mutex_unlock(ca_mutex mutex)
157 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
160 int ret = pthread_mutex_unlock(&mutexInfo->mutex);
163 OIC_LOG_V(ERROR, TAG, "Pthread Mutex unlock failed: %d", ret);
170 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
175 ca_cond ca_cond_new(void)
177 ca_cond retVal = NULL;
178 ca_cond_internal *eventInfo = (ca_cond_internal*) OICMalloc(sizeof(ca_cond_internal));
179 if (NULL != eventInfo)
181 int ret = pthread_condattr_init(&(eventInfo->condattr));
184 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
190 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
192 if (camutex_condattr_setclock) {
193 ret = camutex_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
196 ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
197 #endif /* __ANDROID__ */
200 OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
202 pthread_condattr_destroy(&(eventInfo->condattr));
207 #endif /* defined(__ANDROID__) || _POSIX_TIMERS > 0 */
208 ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
211 retVal = (ca_cond) eventInfo;
215 OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
216 pthread_condattr_destroy(&(eventInfo->condattr));
224 void ca_cond_free(ca_cond cond)
226 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
227 if (eventInfo != NULL)
229 int ret = pthread_cond_destroy(&(eventInfo->cond));
230 int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
231 if (0 == ret && 0 == ret2)
237 OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
238 __func__, ret, ret2);
243 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
247 void ca_cond_signal(ca_cond cond)
249 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
250 if (eventInfo != NULL)
252 int ret = pthread_cond_signal(&(eventInfo->cond));
255 OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
260 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
264 void ca_cond_broadcast(ca_cond cond)
266 ca_cond_internal* eventInfo = (ca_cond_internal*) cond;
267 if (eventInfo != NULL)
269 int ret = pthread_cond_broadcast(&(eventInfo->cond));
272 OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
277 OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
281 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
283 ca_cond_wait_for(cond, mutex, 0L);
286 struct timespec ca_get_current_time()
288 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
290 clock_gettime(CLOCK_MONOTONIC, &ts);
294 gettimeofday(&tv, NULL);
296 TIMEVAL_TO_TIMESPEC(&tv, &ts);
301 void ca_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
303 time_t secPart = microseconds/USECS_PER_SEC;
304 uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
305 uint64_t totalNs = ts->tv_nsec + nsecPart;
306 time_t secOfNs = totalNs/NANOSECS_PER_SEC;
308 ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
309 ts->tv_sec += secPart + secOfNs;
312 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
314 CAWaitResult_t retVal = CA_WAIT_INVAL;
316 ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
317 ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
319 if (NULL == mutexInfo)
321 OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
322 return CA_WAIT_INVAL;
325 if (NULL == eventInfo)
327 OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
328 return CA_WAIT_INVAL;
331 if (microseconds > 0)
334 struct timespec abstime;
337 if (camutex_cond_timedwait_relative) {
338 abstime.tv_sec = microseconds / USECS_PER_SEC;
339 abstime.tv_nsec = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
340 //Wait for the given time
341 ret = camutex_cond_timedwait_relative(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
345 abstime = ca_get_current_time();
346 ca_add_microseconds_to_timespec(&abstime, microseconds);
348 //Wait for the given time
349 ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
356 retVal = CA_WAIT_SUCCESS;
359 retVal = CA_WAIT_TIMEDOUT;
362 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
363 retVal = CA_WAIT_INVAL;
366 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
367 retVal = CA_WAIT_INVAL;
374 int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
375 retVal = ret == 0 ? CA_WAIT_SUCCESS : CA_WAIT_INVAL;