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));
261 DBG_printf("test : sleeping\n");
263 while (!pData1.thread_up || !pData2.thread_up)
265 // For test purposes only, use of condition variables is being
266 // avoided, so a minor sleep is used.
267 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
269 // At this point the threads are running and both have locked. One
270 // has already started waiting on the condition and the other is at
273 ca_mutex_lock(sharedMutex);
274 // once the lock is acquired it means both threads were waiting.
275 DBG_printf("test : signaling first thread\n");
276 ca_cond_signal(sharedCond);
277 ca_mutex_unlock(sharedMutex);
279 // At this point either of the child threads might lock the mutex in
280 // their cond_wait call, or this test thread might lock it again if
281 // mutex_lock gets executed before the child threads can react to
282 // the signaling. Thus we wait on their flag variables
283 int waitCount = 1; // start with 1 for minumum targetWait value.
284 while (!pData1.finished && !pData2.finished)
286 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
290 // As a rough hueristic wait twice as long for the second to possibly
292 int targetWait = waitCount * 2;
294 (i < targetWait) && (!pData1.finished && !pData2.finished); i++)
296 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
298 usleep(MINIMAL_EXTRA_SLEEP);
300 // only one should be finished
301 ca_mutex_lock(sharedMutex);
302 EXPECT_NE(pData1.finished, pData2.finished);
303 ca_mutex_unlock(sharedMutex);
305 DBG_printf("test : signaling another thread\n");
307 ca_mutex_lock(sharedMutex);
308 ca_cond_signal(sharedCond);
309 ca_mutex_unlock(sharedMutex);
312 while ((!pData1.finished || !pData2.finished)
313 && ((waitCount * MINIMAL_EXTRA_SLEEP) < MAX_WAIT_MS))
315 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
319 // both should finally be finished
320 EXPECT_TRUE(pData1.finished);
321 EXPECT_TRUE(pData2.finished);
323 // Cleanup Everything
325 ca_mutex_free(pData1.mutex);
328 ca_cond_free(pData1.condition);
330 ca_thread_pool_free(mythreadpool);
333 TEST(ConditionTests, TC_03_BROADCAST)
335 const int MAX_WAIT_MS = 2000;
336 ca_thread_pool_t mythreadpool;
338 EXPECT_EQ(CA_STATUS_OK, ca_thread_pool_init(3, &mythreadpool));
340 ca_mutex sharedMutex = ca_mutex_new();
341 ca_cond sharedCond = ca_cond_new();
343 _func2_struct pData1 =
344 { 1, sharedMutex, sharedCond, false, false };
345 _func2_struct pData2 =
346 { 2, sharedMutex, sharedCond, false, false };
348 EXPECT_TRUE(pData1.mutex != NULL);
349 if (pData1.mutex != NULL)
351 DBG_printf("starting thread\n");
353 EXPECT_EQ(CA_STATUS_OK,
354 ca_thread_pool_add_task(mythreadpool, condFunc, &pData1));
355 EXPECT_EQ(CA_STATUS_OK,
356 ca_thread_pool_add_task(mythreadpool, condFunc, &pData2));
358 DBG_printf("test : sleeping\n");
360 while (!pData1.thread_up || !pData2.thread_up)
362 // For test purposes only, use of condition variables is being
363 // avoided, so a minor sleep is used.
364 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
366 // At this point the threads are running and both have locked. One
367 // has already started waiting on the condition and the other is at
370 DBG_printf("test : signaling all threads\n");
372 ca_mutex_lock(sharedMutex);
373 // once the lock is acquired it means both threads were waiting.
374 ca_cond_broadcast(sharedCond);
375 ca_mutex_unlock(sharedMutex);
378 while ((!pData1.finished || !pData2.finished)
379 && ((waitCount * MINIMAL_EXTRA_SLEEP) < MAX_WAIT_MS))
381 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
385 // both should finally be finished
386 EXPECT_TRUE(pData1.finished);
387 EXPECT_TRUE(pData2.finished);
389 // Cleanup Everything
391 ca_mutex_free(sharedMutex);
394 ca_cond_free(sharedCond);
396 ca_thread_pool_free(mythreadpool);
399 TEST(CondTests, TC_04_TIMECHECK)
401 uint64_t begin = getAbsTime();
405 uint64_t end = getAbsTime();
407 EXPECT_LT(begin, end); // should never be the same value
410 void timedFunc(void *context)
412 _func2_struct* pData = (_func2_struct*) context;
414 DBG_printf("Thread_%d: waiting for timeout \n", pData->id);
416 ca_mutex_lock(pData->mutex);
418 uint64_t abs = USECS_PER_SEC / 2; // 1/2 seconds
421 CAWaitResult_t ret = ca_cond_wait_for(pData->condition,
423 EXPECT_EQ(CA_WAIT_TIMEDOUT, ret);
425 pData->thread_up = true;
427 DBG_printf("Thread_%d: waiting for signal \n", pData->id);
429 abs = 5 * USECS_PER_SEC; // 5 seconds
432 ret = ca_cond_wait_for(pData->condition, pData->mutex, abs);
433 EXPECT_EQ(CA_WAIT_SUCCESS, ret);
435 pData->finished = true; // assignment guarded by lock
437 ca_mutex_unlock(pData->mutex);
439 DBG_printf("Thread_%d: stopping\n", pData->id);
442 TEST(ConditionTests, TC_05_WAIT)
444 const int MAX_WAIT_MS = 5000;
445 ca_thread_pool_t mythreadpool;
447 EXPECT_EQ(CA_STATUS_OK, ca_thread_pool_init(3, &mythreadpool));
449 ca_mutex sharedMutex = ca_mutex_new();
450 ca_cond sharedCond = ca_cond_new();
452 _func2_struct pData1 =
453 { 1, sharedMutex, sharedCond, false, false };
455 EXPECT_TRUE(sharedMutex != NULL);
456 if (sharedMutex != NULL)
458 DBG_printf("test : starting thread\n");
460 EXPECT_EQ(CA_STATUS_OK,
461 ca_thread_pool_add_task(mythreadpool, timedFunc, &pData1));
463 DBG_printf("test : waiting for thread to timeout once.\n");
465 while (!pData1.thread_up)
467 // For test purposes only, use of condition variables is being
468 // avoided, so a minor sleep is used.
469 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
473 DBG_printf("test : signaling first thread\n");
475 ca_mutex_lock(sharedMutex);
476 ca_cond_signal(sharedCond);
477 ca_mutex_unlock(sharedMutex);
480 while (!pData1.finished
481 && ((waitCount * MINIMAL_EXTRA_SLEEP) < MAX_WAIT_MS))
483 usleep(MINIMAL_LOOP_SLEEP * USECS_PER_MSEC);
487 EXPECT_TRUE(pData1.finished); // thread should finally be finished
489 // Cleanup Everything
491 ca_mutex_free(sharedMutex);
494 ca_cond_free(sharedCond);
496 ca_thread_pool_free(mythreadpool);
499 // Disabled because this should no longer be a valid test
500 TEST(ConditionTests, DISABLED_TC_06_INVALIDWAIT)
503 ca_mutex sharedMutex = ca_mutex_new();
504 ca_cond sharedCond = ca_cond_new();
506 ca_mutex_lock(sharedMutex);
508 int ret = ca_cond_wait_for(NULL, sharedMutex, 5000);
509 EXPECT_EQ(CA_WAIT_INVAL,ret);
511 ret = ca_cond_wait_for(sharedCond, NULL, 5000);
512 EXPECT_EQ(CA_WAIT_INVAL,ret);
514 ret = ca_cond_wait_for(NULL, NULL, 5000);
515 EXPECT_EQ(CA_WAIT_INVAL,ret);
517 ca_mutex_unlock(sharedMutex);
519 // Cleanup Everything
521 ca_mutex_free(sharedMutex);
523 ca_cond_free(sharedCond);
526 TEST(ConditionTests, TC_07_WAITDURATION)
528 const double TARGET_WAIT = 1.125;
530 ca_mutex sharedMutex = ca_mutex_new();
531 ca_cond sharedCond = ca_cond_new();
533 ca_mutex_lock(sharedMutex);
535 uint64_t beg = getAbsTime();
537 CAWaitResult_t ret = ca_cond_wait_for(sharedCond, sharedMutex,
538 TARGET_WAIT * USECS_PER_SEC);
539 EXPECT_EQ(CA_WAIT_TIMEDOUT,ret);
541 uint64_t end = getAbsTime();
543 double secondsDiff = (end - beg) / (double) USECS_PER_SEC;
545 EXPECT_NEAR(TARGET_WAIT, secondsDiff, 0.05);
547 ca_mutex_unlock(sharedMutex);
549 // Cleanup Everything
551 ca_mutex_free(sharedMutex);
553 ca_cond_free(sharedCond);