Fixed svase-warnings for iotivity-product's SQA on |credresource|
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / camutex_pthreads.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 //
21 //
22 //*********************************************************************
23
24 /**
25  * @file
26  * This file provides APIs related to mutex and semaphores.
27  */
28
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
33 //
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
37 #endif
38
39 #include <string.h>
40 #include <pthread.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <time.h>
44 #include <sys/time.h>
45 #include <assert.h>
46 #include <oic_malloc.h>
47
48 #include "camutex.h"
49 #include "logger.h"
50
51 /**
52  * TAG
53  * Logging tag for module name
54  */
55 #define TAG PCF("UMUTEX")
56
57 #ifdef __ANDROID__
58 /**
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
63  * QT 5.5.0 in
64  * http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/thread/qwaitcondition_unix.cpp?h=v5.5.0#n54
65  */
66 static int camutex_condattr_setclock(pthread_condattr_t *, clockid_t)
67         __attribute__ ((weakref("pthread_condattr_setclock")));
68
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__ */
72
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;
76
77 typedef struct _tagMutexInfo_t
78 {
79     pthread_mutex_t mutex;
80 } ca_mutex_internal;
81
82 typedef struct _tagEventInfo_t
83 {
84     pthread_cond_t cond;
85     pthread_condattr_t condattr;
86 } ca_cond_internal;
87
88 ca_mutex ca_mutex_new(void)
89 {
90     ca_mutex retVal = NULL;
91     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) OICMalloc(sizeof(ca_mutex_internal));
92     if (NULL != mutexInfo)
93     {
94         // create the mutex with the attributes set
95         int ret=pthread_mutex_init(&(mutexInfo->mutex), PTHREAD_MUTEX_DEFAULT);
96         if (0 == ret)
97         {
98             retVal = (ca_mutex) mutexInfo;
99         }
100         else
101         {
102             OIC_LOG_V(ERROR, TAG, "%s Failed to initialize mutex !", __func__);
103             OICFree(mutexInfo);
104         }
105     }
106
107     return retVal;
108 }
109
110 bool ca_mutex_free(ca_mutex mutex)
111 {
112     bool bRet=false;
113
114     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
115     if (mutexInfo)
116     {
117         int ret = pthread_mutex_destroy(&mutexInfo->mutex);
118         if (0 == ret)
119         {
120             OICFree(mutexInfo);
121             bRet=true;
122         }
123         else
124         {
125             OIC_LOG_V(ERROR, TAG, "%s Failed to free mutex !", __func__);
126         }
127     }
128     else
129     {
130         OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
131     }
132
133     return bRet;
134 }
135
136 void ca_mutex_lock(ca_mutex mutex)
137 {
138     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
139     if (mutexInfo)
140     {
141         int ret = pthread_mutex_lock(&mutexInfo->mutex);
142         if(ret != 0)
143         {
144             OIC_LOG_V(ERROR, TAG, "Pthread Mutex lock failed: %d", ret);
145             exit(ret);
146         }
147     }
148     else
149     {
150         OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
151         return;
152     }
153 }
154
155 void ca_mutex_unlock(ca_mutex mutex)
156 {
157     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
158     if (mutexInfo)
159     {
160         int ret = pthread_mutex_unlock(&mutexInfo->mutex);
161         if(ret != 0)
162         {
163             OIC_LOG_V(ERROR, TAG, "Pthread Mutex unlock failed: %d", ret);
164             exit(ret);
165         }
166         (void)ret;
167     }
168     else
169     {
170           OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
171           return;
172     }
173 }
174
175 ca_cond ca_cond_new(void)
176 {
177     ca_cond retVal = NULL;
178     ca_cond_internal *eventInfo = (ca_cond_internal*) OICMalloc(sizeof(ca_cond_internal));
179     if (NULL != eventInfo)
180     {
181         int ret = pthread_condattr_init(&(eventInfo->condattr));
182         if(0 != ret)
183         {
184             OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
185                     __func__, ret);
186             OICFree(eventInfo);
187             return retVal;
188         }
189
190 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
191 #ifdef __ANDROID__
192         if (camutex_condattr_setclock)
193         {
194             ret = camutex_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
195 #else
196         {
197             ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
198 #endif /*  __ANDROID__ */
199             if(0 != ret)
200             {
201                 OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
202                         __func__, ret);
203                 pthread_condattr_destroy(&(eventInfo->condattr));
204                 OICFree(eventInfo);
205                 return retVal;
206             }
207         }
208 #endif /* defined(__ANDROID__) || _POSIX_TIMERS > 0 */
209         ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
210         if (0 == ret)
211         {
212             retVal = (ca_cond) eventInfo;
213         }
214         else
215         {
216             OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
217             pthread_condattr_destroy(&(eventInfo->condattr));
218             OICFree(eventInfo);
219         }
220     }
221
222     return retVal;
223 }
224
225 void ca_cond_free(ca_cond cond)
226 {
227     ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
228     if (eventInfo != NULL)
229     {
230         int ret = pthread_cond_destroy(&(eventInfo->cond));
231         int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
232         if (0 == ret && 0 == ret2)
233         {
234             OICFree(cond);
235         }
236         else
237         {
238             OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
239                     __func__, ret, ret2);
240         }
241     }
242     else
243     {
244         OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
245     }
246 }
247
248 void ca_cond_signal(ca_cond cond)
249 {
250     ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
251     if (eventInfo != NULL)
252     {
253         int ret = pthread_cond_signal(&(eventInfo->cond));
254         if (0 != ret)
255         {
256             OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
257         }
258     }
259     else
260     {
261         OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
262     }
263 }
264
265 void ca_cond_broadcast(ca_cond cond)
266 {
267     ca_cond_internal* eventInfo = (ca_cond_internal*) cond;
268     if (eventInfo != NULL)
269     {
270         int ret = pthread_cond_broadcast(&(eventInfo->cond));
271         if (0 != ret)
272         {
273             OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
274         }
275     }
276     else
277     {
278         OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
279     }
280 }
281
282 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
283 {
284     ca_cond_wait_for(cond, mutex, 0L);
285 }
286
287 struct timespec ca_get_current_time()
288 {
289 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
290     struct timespec ts;
291     clock_gettime(CLOCK_MONOTONIC, &ts);
292     return ts;
293 #else
294     struct timeval tv;
295     gettimeofday(&tv, NULL);
296     struct timespec ts;
297     TIMEVAL_TO_TIMESPEC(&tv, &ts);
298     return ts;
299 #endif
300 }
301
302 void ca_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
303 {
304     time_t secPart = microseconds/USECS_PER_SEC;
305     uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
306     uint64_t totalNs = ts->tv_nsec + nsecPart;
307     time_t secOfNs = totalNs/NANOSECS_PER_SEC;
308
309     ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
310     ts->tv_sec += secPart + secOfNs;
311 }
312
313 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
314 {
315     CAWaitResult_t retVal = CA_WAIT_INVAL;
316
317     ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
318     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
319
320     if (NULL == mutexInfo)
321     {
322         OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
323         return CA_WAIT_INVAL;
324     }
325
326     if (NULL == eventInfo)
327     {
328         OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
329         return CA_WAIT_INVAL;
330     }
331
332     if (microseconds > 0)
333     {
334         int ret;
335         struct timespec abstime;
336
337 #ifdef __ANDROID__
338         if (camutex_cond_timedwait_relative)
339         {
340             abstime.tv_sec = microseconds / USECS_PER_SEC;
341             abstime.tv_nsec = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
342             //Wait for the given time
343             ret = camutex_cond_timedwait_relative(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
344         } else
345 #endif
346         {
347              abstime = ca_get_current_time();
348             ca_add_microseconds_to_timespec(&abstime, microseconds);
349
350             //Wait for the given time
351             ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
352         }
353
354         switch (ret)
355         {
356             case 0:
357                 // Success
358                 retVal = CA_WAIT_SUCCESS;
359                 break;
360             case ETIMEDOUT:
361                 retVal = CA_WAIT_TIMEDOUT;
362                 break;
363             case EINVAL:
364                 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
365                 retVal = CA_WAIT_INVAL;
366                 break;
367             default:
368                 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
369                 retVal = CA_WAIT_INVAL;
370                 break;
371         }
372     }
373     else
374     {
375         // Wait forever
376         int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
377         retVal = ret == 0 ? CA_WAIT_SUCCESS : CA_WAIT_INVAL;
378     }
379
380     return retVal;
381 }
382