Replace glib threadpool usage with a 'dumb' thread implementation.
[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 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;
60
61 typedef struct _tagMutexInfo_t
62 {
63     pthread_mutex_t mutex;
64 } ca_mutex_internal;
65
66 typedef struct _tagEventInfo_t
67 {
68     pthread_cond_t cond;
69     pthread_condattr_t condattr;
70 } ca_cond_internal;
71
72 ca_mutex ca_mutex_new(void)
73 {
74     ca_mutex retVal = NULL;
75     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) OICMalloc(sizeof(ca_mutex_internal));
76     if (NULL != mutexInfo)
77     {
78         // create the mutex with the attributes set
79         int ret=pthread_mutex_init(&(mutexInfo->mutex), PTHREAD_MUTEX_DEFAULT);
80         if (0 == ret)
81         {
82             retVal = (ca_mutex) mutexInfo;
83         }
84         else
85         {
86             OIC_LOG_V(ERROR, TAG, "%s Failed to initialize mutex !", __func__);
87             OICFree(mutexInfo);
88         }
89     }
90
91     return retVal;
92 }
93
94 bool ca_mutex_free(ca_mutex mutex)
95 {
96     bool bRet=false;
97
98     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
99     if (mutexInfo)
100     {
101         int ret = pthread_mutex_destroy(&mutexInfo->mutex);
102         if (0 == ret)
103         {
104             OICFree(mutexInfo);
105             bRet=true;
106         }
107         else
108         {
109             OIC_LOG_V(ERROR, TAG, "%s Failed to free mutex !", __func__);
110         }
111     }
112     else
113     {
114         OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
115     }
116
117     return bRet;
118 }
119
120 void ca_mutex_lock(ca_mutex mutex)
121 {
122     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
123     if (mutexInfo)
124     {
125         int ret = pthread_mutex_lock(&mutexInfo->mutex);
126         assert(0 == ret);
127         (void)ret;
128     }
129     else
130     {
131         OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
132         return;
133     }
134 }
135
136 bool ca_mutex_trylock(ca_mutex mutex)
137 {
138     if (NULL == mutex)
139     {
140         OIC_LOG_V(ERROR, TAG, "%s Invalid mutex !", __func__);
141         return false;
142     }
143
144     bool bRet = false;
145
146     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
147
148     int result = pthread_mutex_trylock(&mutexInfo->mutex);
149
150     switch (result)
151     {
152         case 0:
153             // Success
154             bRet = true;
155             break;
156         case EINVAL:
157             OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
158             break;
159         case EBUSY:
160         default:
161             break;
162     }
163
164     return bRet;
165 }
166
167 void ca_mutex_unlock(ca_mutex mutex)
168 {
169     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
170     if (mutexInfo)
171     {
172         int ret = pthread_mutex_unlock(&mutexInfo->mutex);
173         assert ( 0 == ret);
174         (void)ret;
175     }
176     else
177     {
178           OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex !", __func__);
179           return;
180     }
181 }
182
183 ca_cond ca_cond_new(void)
184 {
185     ca_cond retVal = NULL;
186     ca_cond_internal *eventInfo = (ca_cond_internal*) OICMalloc(sizeof(ca_cond_internal));
187     if (NULL != eventInfo)
188     {
189         int ret = pthread_condattr_init(&(eventInfo->condattr));
190         if(0 != ret)
191         {
192             OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable attribute %d!",
193                     __func__, ret);
194             return retVal;
195         }
196
197 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
198         ret = pthread_condattr_setclock(&(eventInfo->condattr), CLOCK_MONOTONIC);
199
200         if(0 != ret)
201         {
202             OIC_LOG_V(ERROR, TAG, "%s: Failed to set condition variable clock %d!",
203                     __func__, ret);
204             return retVal;
205         }
206 #endif
207         ret = pthread_cond_init(&(eventInfo->cond), &(eventInfo->condattr));
208         if (0 == ret)
209         {
210             retVal = (ca_cond) eventInfo;
211         }
212         else
213         {
214             OIC_LOG_V(ERROR, TAG, "%s: Failed to initialize condition variable %d!", __func__, ret);
215             OICFree(eventInfo);
216         }
217     }
218
219     return retVal;
220 }
221
222 void ca_cond_free(ca_cond cond)
223 {
224     ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
225     if (eventInfo != NULL)
226     {
227         int ret = pthread_cond_destroy(&(eventInfo->cond));
228         int ret2 = pthread_condattr_destroy(&(eventInfo->condattr));
229         if (0 == ret && 0 == ret2)
230         {
231             OICFree(cond);
232         }
233         else
234         {
235             OIC_LOG_V(ERROR, TAG, "%s: Failed to destroy condition variable %d, %d",
236                     __func__, ret, ret2);
237         }
238     }
239     else
240     {
241         OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
242     }
243 }
244
245 void ca_cond_signal(ca_cond cond)
246 {
247     ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
248     if (eventInfo != NULL)
249     {
250         int ret = pthread_cond_signal(&(eventInfo->cond));
251         if (0 != ret)
252         {
253             OIC_LOG_V(ERROR, TAG, "%s: Failed to signal condition variable", __func__);
254         }
255     }
256     else
257     {
258         OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
259     }
260 }
261
262 void ca_cond_broadcast(ca_cond cond)
263 {
264     ca_cond_internal* eventInfo = (ca_cond_internal*) cond;
265     if (eventInfo != NULL)
266     {
267         int ret = pthread_cond_broadcast(&(eventInfo->cond));
268         if (0 != ret)
269         {
270             OIC_LOG_V(ERROR, TAG, "%s: failed to signal condition variable", __func__);
271         }
272     }
273     else
274     {
275         OIC_LOG_V(ERROR, TAG, "%s: Invalid parameter", __func__);
276     }
277 }
278
279 void ca_cond_wait(ca_cond cond, ca_mutex mutex)
280 {
281     ca_cond_wait_for(cond, mutex, 0L);
282 }
283
284 struct timespec ca_get_current_time()
285 {
286 #if defined(__ANDROID__) || _POSIX_TIMERS > 0
287     struct timespec ts;
288     clock_gettime(CLOCK_MONOTONIC, &ts);
289     return ts;
290 #else
291     struct timeval tv;
292     gettimeofday(&tv, NULL);
293     struct timespec ts;
294     TIMEVAL_TO_TIMESPEC(&tv, &ts);
295     return ts;
296 #endif
297 }
298
299 void ca_add_microseconds_to_timespec(struct timespec* ts, uint64_t microseconds)
300 {
301     time_t secPart = microseconds/USECS_PER_SEC;
302     uint64_t nsecPart = (microseconds % USECS_PER_SEC) * NANOSECS_PER_USECS;
303     uint64_t totalNs = ts->tv_nsec + nsecPart;
304     time_t secOfNs = totalNs/NANOSECS_PER_SEC;
305
306     ts->tv_nsec = (totalNs)% NANOSECS_PER_SEC;
307     ts->tv_sec += secPart + secOfNs;
308 }
309
310 CAWaitResult_t ca_cond_wait_for(ca_cond cond, ca_mutex mutex, uint64_t microseconds)
311 {
312     CAWaitResult_t retVal = CA_WAIT_INVAL;
313
314     ca_cond_internal *eventInfo = (ca_cond_internal*) cond;
315     ca_mutex_internal *mutexInfo = (ca_mutex_internal*) mutex;
316
317     if (NULL == mutexInfo)
318     {
319         OIC_LOG_V(ERROR, TAG, "%s: Invalid mutex", __func__);
320         return CA_WAIT_INVAL;
321     }
322
323     if (NULL == eventInfo)
324     {
325         OIC_LOG_V(ERROR, TAG, "%s: Invalid condition", __func__);
326         return CA_WAIT_INVAL;
327     }
328
329     if (microseconds > 0)
330     {
331         struct timespec abstime = ca_get_current_time();
332         ca_add_microseconds_to_timespec(&abstime, microseconds);
333
334         //Wait for the given time
335         int ret = pthread_cond_timedwait(&(eventInfo->cond), &(mutexInfo->mutex), &abstime);
336         switch (ret)
337         {
338             case 0:
339                 // Success
340                 retVal = CA_WAIT_SUCCESS;
341                 break;
342             case ETIMEDOUT:
343                 retVal = CA_WAIT_TIMEDOUT;
344                 break;
345             case EINVAL:
346                 OIC_LOG_V(ERROR, TAG, "%s: condition, mutex, or abstime is Invalid", __func__);
347                 retVal = CA_WAIT_INVAL;
348                 break;
349             default:
350                 OIC_LOG_V(ERROR, TAG, "%s: pthread_cond_timedwait returned %d", __func__, retVal);
351                 retVal = CA_WAIT_INVAL;
352                 break;
353         }
354     }
355     else
356     {
357         // Wait forever
358         int ret = pthread_cond_wait(&eventInfo->cond, &mutexInfo->mutex);
359         retVal = ret == 0 ? CA_WAIT_SUCCESS : CA_WAIT_INVAL;
360     }
361
362     return retVal;
363 }
364