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 //*********************************************************************
24 // Defining _POSIX_C_SOURCE macro with 200809L (or greater) as value
25 // causes header files to expose definitions
26 // corresponding to the POSIX.1-2008 base
27 // specification (excluding the XSI extension).
28 // For POSIX.1-2008 base specification,
29 // Refer http://pubs.opengroup.org/stage7tc1/
31 // For this specific file, see use of usleep
32 #ifndef _POSIX_C_SOURCE
33 #define _POSIX_C_SOURCE 200809L
34 #endif // _POSIX_C_SOURCE
36 #include "gtest/gtest.h"
39 #include <cathreadpool.h>
45 //#define DEBUG_VERBOSE 1
47 // The debug print lines are left in for now since the output can be
48 // helpful for developers trying to debug or extend the tests.
49 // However, by default they are #defined out so as not to get in
50 // the way of normal test runs.
52 #define DBG_printf(...) printf(__VA_ARGS__)
54 #define DBG_printf(...)
57 static const uint64_t USECS_PER_SEC = 1000000;
59 static const uint64_t USECS_PER_MSEC = 1000;
61 static const int MINIMAL_LOOP_SLEEP = 20;
62 static const int MINIMAL_EXTRA_SLEEP = 25;
66 uint64_t currentTime=0;
69 clock_gettime(CLOCK_MONOTONIC, &ts);
70 currentTime = ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
73 gettimeofday(&tv, NULL);
74 currentTime = tv.tv_sec * USECS_PER_SEC + tv.tv_usec;
79 TEST(MutexTests, TC_01_CREATE)
81 ca_mutex mymutex = ca_mutex_new();
83 EXPECT_TRUE(mymutex != NULL);
86 ca_mutex_free(mymutex);
90 TEST(MutexTests, TC_02_TRY_LOCK)
92 ca_mutex mymutex = ca_mutex_new();
94 EXPECT_TRUE(mymutex != NULL);
97 EXPECT_TRUE(ca_mutex_trylock(mymutex)); // acquire it
99 ca_mutex_unlock(mymutex); // release it
101 ca_mutex_lock(mymutex); // acquire it
103 EXPECT_FALSE(ca_mutex_trylock(mymutex)); // he should be lock
105 EXPECT_FALSE(ca_mutex_trylock(NULL));
107 ca_mutex_unlock(mymutex); // release it
108 ca_mutex_free(mymutex);
110 EXPECT_FALSE(ca_mutex_trylock(NULL));
114 typedef struct _tagFunc1
117 volatile bool thread_up;
118 volatile bool finished;
121 void mutexFunc(void *context)
123 _func1_struct* pData = (_func1_struct*) context;
125 DBG_printf("Thread: trying to lock\n");
127 // setting the flag must be done before lock attempt, as the test
128 // thread starts off with the mutex locked
129 pData->thread_up = true;
130 ca_mutex_lock(pData->mutex);
132 DBG_printf("Thread: got lock\n");
133 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
134 DBG_printf("Thread: releasing\n");
136 pData->finished = true; // assignment guarded by lock
138 ca_mutex_unlock(pData->mutex);
141 TEST(MutexTests, TC_03_THREAD_LOCKING)
143 ca_thread_pool_t mythreadpool;
145 EXPECT_EQ(CA_STATUS_OK, ca_thread_pool_init(3, &mythreadpool));
147 _func1_struct pData = {};
149 pData.mutex = ca_mutex_new();
151 EXPECT_TRUE(pData.mutex != NULL);
152 if (pData.mutex != NULL)
154 DBG_printf("test: Holding mutex in test\n");
155 ca_mutex_lock(pData.mutex);
157 DBG_printf("test: starting thread\n");
159 EXPECT_EQ(CA_STATUS_OK,
160 ca_thread_pool_add_task(mythreadpool, mutexFunc, &pData));
162 DBG_printf("test: waiting for thread to be up.\n");
164 while (!pData.thread_up)
166 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
168 // At this point the thread is running and close to trying to lock.
169 // For test purposes only, use of condition variables is being avoided,
170 // so a minor sleep is used.
171 usleep(MINIMAL_EXTRA_SLEEP * USECS_PER_MSEC);
173 DBG_printf("test: unlocking\n");
175 ca_mutex_unlock(pData.mutex);
177 DBG_printf("test: waiting for thread to release\n");
178 while (!pData.finished)
180 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
183 ca_mutex_lock(pData.mutex);
185 // Cleanup Everything
187 ca_mutex_unlock(pData.mutex);
188 ca_mutex_free(pData.mutex);
191 ca_thread_pool_free(mythreadpool);
194 TEST(ConditionTests, TC_01_CREATE)
196 ca_cond mycond = ca_cond_new();
198 EXPECT_TRUE(mycond != NULL);
201 ca_cond_free(mycond);
205 // Normally we would use one pair of mutex/cond-var communicating to the
206 // worker threads and one pair back to the main thread. However since
207 // testing the ca_cond itself is the point, only one pair is used here.
208 typedef struct _tagFunc2
213 volatile bool thread_up;
214 volatile bool finished;
217 void condFunc(void *context)
219 _func2_struct* pData = (_func2_struct*) context;
221 DBG_printf("Thread_%d: waiting on condition\n", pData->id);
223 ca_mutex_lock(pData->mutex);
225 pData->thread_up = true;
227 ca_cond_wait(pData->condition, pData->mutex);
229 pData->finished = true; // assignment guarded by lock
231 ca_mutex_unlock(pData->mutex);
233 DBG_printf("Thread_%d: completed.\n", pData->id);
236 TEST(ConditionTests, TC_02_SIGNAL)
238 const int MAX_WAIT_MS = 2000;
239 ca_thread_pool_t mythreadpool;
241 EXPECT_EQ(CA_STATUS_OK, ca_thread_pool_init(3, &mythreadpool));
243 ca_mutex sharedMutex = ca_mutex_new();
244 ca_cond sharedCond = ca_cond_new();
246 _func2_struct pData1 =
247 { 1, sharedMutex, sharedCond, false, false };
248 _func2_struct pData2 =
249 { 2, sharedMutex, sharedCond, false, false };
251 EXPECT_TRUE(pData1.mutex != NULL);
252 if (pData1.mutex != NULL)
254 DBG_printf("starting thread\n");
256 EXPECT_EQ(CA_STATUS_OK,
257 ca_thread_pool_add_task(mythreadpool, condFunc, &pData1));
258 EXPECT_EQ(CA_STATUS_OK,
259 ca_thread_pool_add_task(mythreadpool, condFunc, &pData2));
262 EXPECT_EQ(CA_STATUS_OK,
263 ca_thread_pool_add_task(mythreadpool, condFunc, &pData2));
265 DBG_printf("test : sleeping\n");
267 while (!pData1.thread_up || !pData2.thread_up)
269 // For test purposes only, use of condition variables is being
270 // avoided, so a minor sleep is used.
271 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
273 // At this point the threads are running and both have locked. One
274 // has already started waiting on the condition and the other is at
277 ca_mutex_lock(sharedMutex);
278 // once the lock is acquired it means both threads were waiting.
279 DBG_printf("test : signaling first thread\n");
280 ca_cond_signal(sharedCond);
281 ca_mutex_unlock(sharedMutex);
283 // At this point either of the child threads might lock the mutex in
284 // their cond_wait call, or this test thread might lock it again if
285 // mutex_lock gets executed before the child threads can react to
286 // the signaling. Thus we wait on their flag variables
287 int waitCount = 1; // start with 1 for minumum targetWait value.
288 while (!pData1.finished && !pData2.finished)
290 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
294 // As a rough hueristic wait twice as long for the second to possibly
296 int targetWait = waitCount * 2;
298 (i < targetWait) && (!pData1.finished && !pData2.finished); i++)
300 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
302 usleep(MINIMAL_EXTRA_SLEEP);
304 // only one should be finished
305 ca_mutex_lock(sharedMutex);
306 EXPECT_NE(pData1.finished, pData2.finished);
307 ca_mutex_unlock(sharedMutex);
309 DBG_printf("test : signaling another thread\n");
311 ca_mutex_lock(sharedMutex);
312 ca_cond_signal(sharedCond);
313 ca_mutex_unlock(sharedMutex);
316 while ((!pData1.finished || !pData2.finished)
317 && ((waitCount * MINIMAL_EXTRA_SLEEP) < MAX_WAIT_MS))
319 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
323 // both should finally be finished
324 EXPECT_TRUE(pData1.finished);
325 EXPECT_TRUE(pData2.finished);
327 // Cleanup Everything
329 ca_mutex_free(pData1.mutex);
332 ca_cond_free(pData1.condition);
334 ca_thread_pool_free(mythreadpool);
337 TEST(ConditionTests, TC_03_BROADCAST)
339 const int MAX_WAIT_MS = 2000;
340 ca_thread_pool_t mythreadpool;
342 EXPECT_EQ(CA_STATUS_OK, ca_thread_pool_init(3, &mythreadpool));
344 ca_mutex sharedMutex = ca_mutex_new();
345 ca_cond sharedCond = ca_cond_new();
347 _func2_struct pData1 =
348 { 1, sharedMutex, sharedCond, false, false };
349 _func2_struct pData2 =
350 { 2, sharedMutex, sharedCond, false, false };
352 EXPECT_TRUE(pData1.mutex != NULL);
353 if (pData1.mutex != NULL)
355 DBG_printf("starting thread\n");
357 EXPECT_EQ(CA_STATUS_OK,
358 ca_thread_pool_add_task(mythreadpool, condFunc, &pData1));
359 EXPECT_EQ(CA_STATUS_OK,
360 ca_thread_pool_add_task(mythreadpool, condFunc, &pData2));
363 EXPECT_EQ(CA_STATUS_OK,
364 ca_thread_pool_add_task(mythreadpool, condFunc, &pData2));
366 DBG_printf("test : sleeping\n");
368 while (!pData1.thread_up || !pData2.thread_up)
370 // For test purposes only, use of condition variables is being
371 // avoided, so a minor sleep is used.
372 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
374 // At this point the threads are running and both have locked. One
375 // has already started waiting on the condition and the other is at
378 DBG_printf("test : signaling all threads\n");
380 ca_mutex_lock(sharedMutex);
381 // once the lock is acquired it means both threads were waiting.
382 ca_cond_broadcast(sharedCond);
383 ca_mutex_unlock(sharedMutex);
386 while ((!pData1.finished || !pData2.finished)
387 && ((waitCount * MINIMAL_EXTRA_SLEEP) < MAX_WAIT_MS))
389 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
393 // both should finally be finished
394 EXPECT_TRUE(pData1.finished);
395 EXPECT_TRUE(pData2.finished);
397 // Cleanup Everything
399 ca_mutex_free(sharedMutex);
402 ca_cond_free(sharedCond);
404 ca_thread_pool_free(mythreadpool);
407 TEST(CondTests, TC_04_TIMECHECK)
409 uint64_t begin = getAbsTime();
413 uint64_t end = getAbsTime();
415 EXPECT_LT(begin, end); // should never be the same value
418 void timedFunc(void *context)
420 _func2_struct* pData = (_func2_struct*) context;
422 DBG_printf("Thread_%d: waiting for timeout \n", pData->id);
424 ca_mutex_lock(pData->mutex);
426 uint64_t abs = USECS_PER_SEC / 2; // 1/2 seconds
429 CAWaitResult_t ret = ca_cond_wait_for(pData->condition,
431 EXPECT_EQ(CA_WAIT_TIMEDOUT, ret);
433 pData->thread_up = true;
435 DBG_printf("Thread_%d: waiting for signal \n", pData->id);
437 abs = 5 * USECS_PER_SEC; // 5 seconds
440 ret = ca_cond_wait_for(pData->condition, pData->mutex, abs);
441 EXPECT_EQ(CA_WAIT_SUCCESS, ret);
443 pData->finished = true; // assignment guarded by lock
445 ca_mutex_unlock(pData->mutex);
447 DBG_printf("Thread_%d: stopping\n", pData->id);
450 TEST(ConditionTests, TC_05_WAIT)
452 const int MAX_WAIT_MS = 5000;
453 ca_thread_pool_t mythreadpool;
455 EXPECT_EQ(CA_STATUS_OK, ca_thread_pool_init(3, &mythreadpool));
457 ca_mutex sharedMutex = ca_mutex_new();
458 ca_cond sharedCond = ca_cond_new();
460 _func2_struct pData1 =
461 { 1, sharedMutex, sharedCond, false, false };
463 EXPECT_TRUE(sharedMutex != NULL);
464 if (sharedMutex != NULL)
466 DBG_printf("test : starting thread\n");
468 EXPECT_EQ(CA_STATUS_OK,
469 ca_thread_pool_add_task(mythreadpool, timedFunc, &pData1));
471 DBG_printf("test : waiting for thread to timeout once.\n");
473 while (!pData1.thread_up)
475 // For test purposes only, use of condition variables is being
476 // avoided, so a minor sleep is used.
477 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
481 DBG_printf("test : signaling first thread\n");
483 ca_mutex_lock(sharedMutex);
484 ca_cond_signal(sharedCond);
485 ca_mutex_unlock(sharedMutex);
488 while (!pData1.finished
489 && ((waitCount * MINIMAL_EXTRA_SLEEP) < MAX_WAIT_MS))
491 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
495 EXPECT_TRUE(pData1.finished); // thread should finally be finished
497 // Cleanup Everything
499 ca_mutex_free(sharedMutex);
502 ca_cond_free(sharedCond);
504 ca_thread_pool_free(mythreadpool);
507 // Disabled because this should no longer be a valid test
508 TEST(ConditionTests, DISABLED_TC_06_INVALIDWAIT)
511 ca_mutex sharedMutex = ca_mutex_new();
512 ca_cond sharedCond = ca_cond_new();
514 ca_mutex_lock(sharedMutex);
516 int ret = ca_cond_wait_for(NULL, sharedMutex, 5000);
517 EXPECT_EQ(CA_WAIT_INVAL,ret);
519 ret = ca_cond_wait_for(sharedCond, NULL, 5000);
520 EXPECT_EQ(CA_WAIT_INVAL,ret);
522 ret = ca_cond_wait_for(NULL, NULL, 5000);
523 EXPECT_EQ(CA_WAIT_INVAL,ret);
525 ca_mutex_unlock(sharedMutex);
527 // Cleanup Everything
529 ca_mutex_free(sharedMutex);
531 ca_cond_free(sharedCond);
534 TEST(ConditionTests, TC_07_WAITDURATION)
536 const double TARGET_WAIT = 1.125;
538 ca_mutex sharedMutex = ca_mutex_new();
539 ca_cond sharedCond = ca_cond_new();
541 ca_mutex_lock(sharedMutex);
543 uint64_t beg = getAbsTime();
545 CAWaitResult_t ret = ca_cond_wait_for(sharedCond, sharedMutex,
546 TARGET_WAIT * USECS_PER_SEC);
547 EXPECT_EQ(CA_WAIT_TIMEDOUT,ret);
549 uint64_t end = getAbsTime();
551 double secondsDiff = (end - beg) / (double) USECS_PER_SEC;
553 EXPECT_NEAR(TARGET_WAIT, secondsDiff, 0.05);
555 ca_mutex_unlock(sharedMutex);
557 // Cleanup Everything
559 ca_mutex_free(sharedMutex);
561 ca_cond_free(sharedCond);