-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads_test.cpp
1114 lines (928 loc) · 30.9 KB
/
threads_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// test program for agentpp threadpool
//
// astyle --style=kr thread*.{cpp,hpp}
// clang-format -style=file -i thread*.{cpp,hpp}
//
#define USE_BUSY_TEST
#ifdef USE_AGENTPP
# define _NO_LOGGING 1
# include "agent_pp/threads.h" // ThreadPool, QueuedThreadPool
# define TEST_INDEPENDENTLY
using namespace Agentpp;
#elif USE_AGENTPP_CK
# include "posix/threadpool.hpp" // ThreadPool, QueuedThreadPool
# define TEST_INDEPENDENTLY
# define TEST_USAGE_AFTER_TERMINATE
using namespace AgentppCK;
#else
# include "threadpool.hpp" // ThreadPool, QueuedThreadPool
# define USE_WAIT_FOR
using namespace Agentpp;
#endif
#include "simple_stopwatch.hpp"
#ifndef _WIN32
// -----------------------------------------
# define BOOST_TEST_MODULE Threads
# define BOOST_TEST_NO_MAIN
# include <boost/test/included/unit_test.hpp>
// -----------------------------------------
#else
# include <boost/test/auto_unit_test.hpp>
#endif
#include <boost/atomic.hpp>
#include <boost/functional/hash.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/lockfree/queue.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/thread/latch.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread_only.hpp>
#include <iostream>
#include <string>
#include <vector>
#if !defined BOOST_THREAD_TEST_TIME_MS
# if defined(__linux__) || defined(__APPLE__)
# define BOOST_THREAD_TEST_TIME_MS 75
# else
// Windows, Cygwin, msys all need this
# define BOOST_THREAD_TEST_TIME_MS 250
# endif
#endif
typedef boost::atomic<size_t> test_counter_t;
typedef boost::lockfree::queue<size_t, boost::lockfree::capacity<20> >
result_queue_t;
class TestTask : public Runnable {
typedef boost::mutex lockable_type;
typedef boost::unique_lock<lockable_type> scoped_lock;
public:
explicit TestTask(
const std::string& msg, result_queue_t& rslt, unsigned ms_delay = 11)
: text(msg)
, result(rslt)
, delay(ms_delay)
{
scoped_lock l(lock);
++counter;
}
~TestTask() BOOST_OVERRIDE
{
scoped_lock l(lock);
--counter;
}
#ifdef USE_UNIQUE_PTR
boost::unique_ptr<Runnable> clone() const BOOST_OVERRIDE
{
return boost::make_unique<TestTask>(text, result, delay);
}
#endif
void run() BOOST_OVERRIDE
{
Thread::sleep((rand() % 3) * delay); // NOLINT
scoped_lock l(lock);
// WARNING: ThreadSanitizer: data race
// BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << " called with: " <<
// text);
size_t hash = boost::hash_value(text);
result.push(hash);
++run_cnt;
}
static size_t run_count()
{
scoped_lock l(lock);
return run_cnt;
}
static size_t task_count()
{
scoped_lock l(lock);
return counter;
}
static void reset_counter()
{
scoped_lock l(lock);
counter = 0;
run_cnt = 0;
}
protected:
static test_counter_t run_cnt;
static test_counter_t counter;
static lockable_type lock;
private:
const std::string text;
result_queue_t& result;
const unsigned delay;
};
TestTask::lockable_type TestTask::lock;
// warning: initialization of 'lock' with static storage
// duration may throw an exception that cannot be caught
// [cert-err58-cpp]
test_counter_t TestTask::run_cnt(0);
test_counter_t TestTask::counter(0);
static boost::latch start_latch(1);
static boost::latch completion_latch(4);
void push_task(ThreadPool* tp)
{
static result_queue_t result;
start_latch.wait();
tp->execute(new TestTask(
"Generate to mutch load.", result, BOOST_THREAD_TEST_TIME_MS));
completion_latch.count_down();
}
#ifndef USE_AGENTPP
BOOST_AUTO_TEST_CASE(ThreadPoolInterface_test)
{
result_queue_t result;
ThreadPool emptyThreadPool(0);
BOOST_TEST(emptyThreadPool.is_busy());
BOOST_TEST(!emptyThreadPool.is_idle());
emptyThreadPool.execute(new TestTask("I want to run!", result));
BOOST_TEST(TestTask::task_count() == 0UL, "ALL task has to be deleted!");
BOOST_TEST(TestTask::run_count() == 0UL, "NO task can to be executed!");
TestTask::reset_counter();
emptyThreadPool.terminate();
}
#endif
BOOST_AUTO_TEST_CASE(ThreadPool_busy_test)
{
constexpr size_t MAX_LOAD { 8 };
constexpr size_t threadCount { 1 };
{
constexpr size_t stacksize { AGENTPP_DEFAULT_STACKSIZE * 2 };
ThreadPool threadPool(threadCount, stacksize);
BOOST_TEST_MESSAGE("threadPool.size: " << threadPool.size());
BOOST_TEST(threadPool.size() == threadCount);
BOOST_TEST(threadPool.stack_size() == stacksize);
BOOST_TEST(threadPool.is_idle());
// call execute parallel from different task!
start_latch.reset(1);
completion_latch.reset(MAX_LOAD);
std::array<boost::thread, (MAX_LOAD)> threads;
for (size_t i = 0; i < (MAX_LOAD); ++i) {
threads.at(i) = boost::thread(push_task, &threadPool);
threads.at(i).detach();
}
start_latch.count_down();
boost::this_thread::yield();
completion_latch.wait();
BOOST_TEST_WARN(threadPool.is_busy());
do {
BOOST_TEST_MESSAGE(
"outstanding tasks: " << TestTask::task_count());
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
} while (!threadPool.is_idle());
BOOST_TEST(threadPool.is_idle());
BOOST_TEST_WARN(!threadPool.is_busy());
threadPool.terminate();
}
BOOST_TEST(TestTask::task_count() == 0UL);
BOOST_TEST(TestTask::run_count() == (MAX_LOAD));
TestTask::reset_counter();
}
BOOST_AUTO_TEST_CASE(ThreadPool_test)
{
result_queue_t result;
size_t i = 0;
{
ThreadPool threadPool(4UL);
BOOST_TEST_MESSAGE("threadPool.size: " << threadPool.size());
BOOST_TEST(threadPool.size() == 4UL);
BOOST_TEST_WARN(threadPool.stack_size() == AGENTPP_DEFAULT_STACKSIZE);
BOOST_TEST(threadPool.is_idle());
#ifdef USE_BUSY_TEST
BOOST_TEST_WARN(!threadPool.is_busy());
threadPool.execute(new TestTask("Hallo world!", result));
++i;
BOOST_TEST_WARN(!threadPool.is_busy());
threadPool.execute(new TestTask("ThreadPool is running!", result));
++i;
BOOST_TEST_WARN(!threadPool.is_busy());
#endif
do {
threadPool.execute(new TestTask("Generate some load.", result));
++i;
} while (threadPool.is_idle());
threadPool.execute(new TestTask("Under full load now!", result));
++i;
threadPool.execute(new TestTask("Good by!", result));
++i;
do {
BOOST_TEST_MESSAGE(
"outstanding tasks: " << TestTask::task_count());
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
} while (!threadPool.is_idle());
BOOST_TEST_WARN(!threadPool.is_busy());
threadPool.terminate();
}
BOOST_TEST(TestTask::task_count() == 0UL, "All task has to be deleted!");
BOOST_TEST(TestTask::run_count() == i, "All task has to be executed!");
TestTask::reset_counter();
}
BOOST_AUTO_TEST_CASE(QueuedThreadPool_busy_test)
{
result_queue_t result;
constexpr size_t MAX_LOAD { 4 };
constexpr size_t threadCount { MAX_LOAD / 2 };
{
constexpr size_t stacksize { AGENTPP_DEFAULT_STACKSIZE * 2 };
QueuedThreadPool threadPool(threadCount, stacksize);
#if !defined(AGENTPP_USE_IMPLIZIT_START)
threadPool.start(); // NOTE: different to ThreadPool, but this
// should not really needed!
#endif
BOOST_TEST_MESSAGE("threadPool.size: " << threadPool.size());
BOOST_TEST(threadPool.size() == threadCount);
BOOST_TEST(threadPool.stack_size() == stacksize);
BOOST_TEST(threadPool.is_idle());
// call execute parallel from different task!
start_latch.reset(1);
completion_latch.reset(MAX_LOAD);
std::array<boost::thread, (MAX_LOAD)> threads;
for (size_t i = 0; i < (MAX_LOAD); ++i) {
threads.at(i) = boost::thread(push_task, &threadPool);
threads.at(i).detach();
}
start_latch.count_down();
boost::this_thread::yield();
completion_latch.wait();
BOOST_TEST_WARN(threadPool.is_busy());
do {
BOOST_TEST_MESSAGE(
"outstanding tasks: " << TestTask::task_count());
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
} while (!threadPool.is_idle());
BOOST_TEST(threadPool.is_idle());
BOOST_TEST_WARN(!threadPool.is_busy());
threadPool.terminate();
}
BOOST_TEST(TestTask::run_count() == (MAX_LOAD));
BOOST_TEST(TestTask::task_count() == 0UL);
TestTask::reset_counter();
}
BOOST_AUTO_TEST_CASE(QueuedThreadPool_test)
{
result_queue_t result;
{
QueuedThreadPool queuedThreadPool(1);
#if !defined(AGENTPP_USE_IMPLIZIT_START)
queuedThreadPool.start(); // NOTE: different to ThreadPool, but this
// should not really needed!
#endif
BOOST_TEST_MESSAGE(
"queuedThreadPool.size: " << queuedThreadPool.size());
BOOST_TEST(queuedThreadPool.size() == 1UL);
BOOST_TEST_WARN(
queuedThreadPool.stack_size() == AGENTPP_DEFAULT_STACKSIZE);
BOOST_TEST(queuedThreadPool.is_idle());
queuedThreadPool.execute(new TestTask("1 Hi again.", result, 10));
queuedThreadPool.execute(
new TestTask("2 Queuing starts.", result, 20));
queuedThreadPool.execute(
new TestTask("3 Under full load!", result, 30));
std::srand(static_cast<unsigned>(std::time(0))); // NOLINT
unsigned i = 4;
do {
unsigned delay = rand() % 100; // NOLINT
std::string msg(std::to_string(i));
queuedThreadPool.execute(
new TestTask(msg + " Queuing ...", result, delay));
} while (++i < (4 + 6));
do {
BOOST_TEST_MESSAGE("queuedThreadPool.queue_length: "
<< queuedThreadPool.queue_length());
Thread::sleep(500); // NOTE: after more than 1/2 sec! CK
} while (!queuedThreadPool.is_idle());
BOOST_TEST_WARN(!queuedThreadPool.is_busy());
queuedThreadPool.terminate();
}
BOOST_TEST(TestTask::task_count() == 0UL, "All task has to be deleted!");
BOOST_TEST(TestTask::run_count() == 9UL, "All task has to be executed!");
TestTask::reset_counter();
BOOST_TEST_MESSAGE("NOTE: checking the order of execution");
for (size_t i = 1; i < 10; i++) {
size_t value = 0;
if (result.pop(value)) {
if (i >= 4) {
std::string msg(
boost::lexical_cast<std::string>(i) + " Queuing ...");
BOOST_TEST_WARN(
boost::hash_value(value) == boost::hash_value(msg),
"expected msg: " << msg);
}
}
}
}
BOOST_AUTO_TEST_CASE(QueuedThreadPoolLoad_test)
{
result_queue_t result;
{
QueuedThreadPool defaultThreadPool(1UL);
#if !defined(AGENTPP_USE_IMPLIZIT_START)
defaultThreadPool.start(); // NOTE: different to ThreadPool, but this
// should not really needed!
#endif
BOOST_TEST(defaultThreadPool.is_idle());
BOOST_TEST_MESSAGE(
"defaultThreadPool.size: " << defaultThreadPool.size());
defaultThreadPool.execute(new TestTask("Started ...", result));
unsigned i = 20;
do {
if (i > 5) {
unsigned delay = rand() % 100; // NOLINT
defaultThreadPool.execute(
new TestTask("Running ...", result, delay));
BOOST_TEST_WARN(defaultThreadPool.is_busy());
}
BOOST_TEST_MESSAGE("defaultThreadPool.queue_length: "
<< defaultThreadPool.queue_length());
} while (--i > 0);
do {
BOOST_TEST_MESSAGE(
"outstanding tasks: " << TestTask::task_count());
Thread::sleep(100); // ms
} while (!defaultThreadPool.is_idle());
BOOST_TEST_WARN(!defaultThreadPool.is_busy());
BOOST_TEST_MESSAGE("outstanding tasks: " << TestTask::task_count());
BOOST_TEST_MESSAGE("executed tasks: " << TestTask::run_count());
// NOTE: implicit called: defaultThreadPool.terminate();
}
BOOST_TEST(TestTask::task_count() == 0UL, "All task has to be deleted!");
BOOST_TEST_WARN(TestTask::run_count() == 16UL);
TestTask::reset_counter();
}
#ifndef USE_AGENTPP
BOOST_AUTO_TEST_CASE(QueuedThreadPoolInterface_test)
{
result_queue_t result;
{
QueuedThreadPool emptyThreadPool(
0UL, 0x20000); // NOTE: without any worker thread! CK
BOOST_TEST(emptyThreadPool.size() == 0UL);
# if defined(USE_AGENTPP)
BOOST_TEST(emptyThreadPool.is_idle());
// NOTE: NO! XXX CK emptyThreadPool.terminate();
// NOTE: NO! XXX CK BOOST_TEST(!emptyThreadPool.is_idle());
// TODO: not clear! CK
emptyThreadPool.set_stack_size(
0x20000); // NOTE: this change the queue thread only! CK
BOOST_TEST(emptyThreadPool.stack_size() == 0x20000);
# endif
BOOST_TEST_MESSAGE("emptyThreadPool.size: " << emptyThreadPool.size());
emptyThreadPool.execute(new TestTask("I want to run!", result));
BOOST_TEST_WARN(emptyThreadPool.is_busy());
# if !defined(AGENTPP_USE_IMPLIZIT_START)
emptyThreadPool.start();
emptyThreadPool.execute(new TestTask("after Starting ...", result));
# endif
// NOTE: NO! XXX CK BOOST_TEST(!emptyThreadPool.is_idle());
size_t i = 10;
do {
if (i > 5) {
emptyThreadPool.execute(new TestTask("Running ...", result));
}
BOOST_TEST_MESSAGE("emptyThreadPool.queue_length: "
<< emptyThreadPool.queue_length());
Thread::sleep(10); // ms
} while (--i > 0);
// NOTE: NO! XXX CK BOOST_TEST(!emptyThreadPool.is_idle());
BOOST_TEST_MESSAGE("outstanding tasks: " << TestTask::task_count());
// NOTE: NO! XXX CK BOOST_TEST(TestTask::task_count() == 6UL);
// NOTE: implicit called: emptyThreadPool.terminate();
}
BOOST_TEST(TestTask::task_count() == 0UL, "ALL task has to be deleted!");
BOOST_TEST(TestTask::run_count() == 0UL, "NO task has to be executed!");
TestTask::reset_counter();
}
#endif
BOOST_AUTO_TEST_CASE(QueuedThreadPoolIndependency_test)
{
result_queue_t result;
QueuedThreadPool firstThreadPool(1);
BOOST_TEST(firstThreadPool.size() == 1UL);
#if !defined(AGENTPP_USE_IMPLIZIT_START)
firstThreadPool.start();
#endif
BOOST_TEST_MESSAGE("firstThreadPool.size: " << firstThreadPool.size());
firstThreadPool.execute(new TestTask("Starting ...", result));
BOOST_TEST_WARN(firstThreadPool.is_busy());
size_t n = 1;
{
QueuedThreadPool secondThreadPool(4);
BOOST_TEST_MESSAGE(
"secondThreadPool.size: " << secondThreadPool.size());
#if !defined(AGENTPP_USE_IMPLIZIT_START)
secondThreadPool.start();
#endif
BOOST_TEST(secondThreadPool.is_idle());
secondThreadPool.execute(new TestTask("Starting ...", result));
n++;
#ifdef USE_BUSY_TEST
while (secondThreadPool.is_busy()) {
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
}
BOOST_TEST(!secondThreadPool.is_busy());
#else
do {
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
} while (!secondThreadPool.is_idle());
BOOST_TEST(secondThreadPool.is_idle());
#endif
secondThreadPool.terminate();
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
#ifdef TEST_USAGE_AFTER_TERMINATE
secondThreadPool.execute(new TestTask("After terminate ...", result));
// NOTE: NO! XXX CK n++;
size_t i = 10;
do {
if (i > 5) {
secondThreadPool.execute(new TestTask("Queuing ...", result));
// NOTE: NO! XXX CK n++;
}
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
} while (--i > 0);
#endif
BOOST_TEST_MESSAGE("outstanding tasks: " << TestTask::task_count());
BOOST_TEST(TestTask::run_count() == n);
}
firstThreadPool.execute(new TestTask("Stopping ...", result));
n++;
BOOST_TEST_MESSAGE(
"firstThreadPool.queue_length: " << firstThreadPool.queue_length());
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
#ifdef USE_BUSY_TEST
while (firstThreadPool.is_busy()) {
BOOST_TEST(!firstThreadPool.is_idle());
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
}
#else
do {
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
} while (!firstThreadPool.is_idle());
#endif
BOOST_TEST(firstThreadPool.is_idle());
firstThreadPool.terminate();
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // ms
BOOST_TEST_MESSAGE("outstanding tasks: " << TestTask::task_count());
//############################
// FIXME! outstanding tasks: 2? CK
// BOOST_TEST(TestTask::task_count() == 0UL, "ALL task has to be
// deleted!");
BOOST_TEST(TestTask::run_count() == n, "All task has to be executed!");
//############################
TestTask::reset_counter();
}
BOOST_AUTO_TEST_CASE(Synchronized_test)
{
Synchronized sync;
{
BOOST_TEST(sync.lock());
BOOST_TEST(sync.unlock());
#ifndef __linux__ // ThreadSanitizer: unlock of an unlocked mutex (or by a
// wrong thread)
BOOST_TEST(!sync.unlock(), "second unlock() returns OK");
#endif
}
#ifndef __linux__ // ThreadSanitizer: unlock of an unlocked mutex (or by a
// wrong thread)
BOOST_TEST(!sync.unlock(), "unlock() without previous lock() returns OK");
#endif
}
BOOST_AUTO_TEST_CASE(SyncTrylock_test)
{
Synchronized sync;
BOOST_TEST(sync.trylock() == Synchronized::LOCKED);
sync.unlock();
{
Lock l(sync);
#if defined(USE_AGENTPP_CK) || defined(USE_AGENTPP)
BOOST_TEST(sync.trylock() == Synchronized::BUSY);
#else
BOOST_TEST(sync.trylock() == Synchronized::OWNED);
#endif
}
#ifndef __linux__ // ThreadSanitizer: unlock of an unlocked mutex (or by a
// wrong thread)
BOOST_TEST(!sync.unlock(), "second unlock() returns OK");
#endif
}
BOOST_AUTO_TEST_CASE(SyncDeadlock_test)
{
Synchronized sync;
try {
Lock l(sync);
BOOST_TEST(sync.lock());
} catch (std::exception& e) {
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION);
BOOST_TEST_MESSAGE(e.what());
}
BOOST_TEST(sync.lock());
BOOST_TEST(sync.unlock());
}
#ifndef _WIN32
void handler(int signum)
{
switch (signum) {
case SIGALRM:
signal(signum, SIG_DFL);
break;
default: // ignored
break;
}
}
#endif
BOOST_AUTO_TEST_CASE(SyncDeleteLocked_test)
{
#ifndef _WIN32
signal(SIGALRM, &handler);
ualarm(1000, 0); // us
#endif
Stopwatch sw;
try {
auto sync = boost::make_shared<Synchronized>();
BOOST_TEST(sync->lock());
#ifndef __WIN32
sync->wait(1234); // for signal with timout
#endif
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
} catch (std::exception& e) {
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION);
BOOST_TEST_MESSAGE(e.what());
}
}
#ifndef _WIN32
BOOST_AUTO_TEST_CASE(SyncWait_test)
{
Synchronized sync;
{
Lock l(sync);
Stopwatch sw;
# ifdef USE_WAIT_FOR
BOOST_TEST(!sync.wait_for(BOOST_THREAD_TEST_TIME_MS),
"no timeout occurred on wait!");
# else
BOOST_TEST(sync.wait(BOOST_THREAD_TEST_TIME_MS),
"no timeout occurred on wait!");
# endif // !defined(USE_WAIT_FOR)
ns d = sw.elapsed();
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
BOOST_TEST(d >= ms(BOOST_THREAD_TEST_TIME_MS - 1));
// TODO: error: in "SyncWait_test": check d >= ms(75) has failed
// [74892559 nanoseconds < 75 milliseconds]
}
}
#endif
class BadTask : public Runnable {
private:
Synchronized sync;
boost::atomic<bool> stopped { false };
const bool doit;
public:
explicit BadTask(bool do_throw)
: doit(do_throw) {};
~BadTask() override
{
stopped = true;
sync.notify_all();
}
void stop()
{
Lock l(sync); // wait for the scoped lock
stopped = true;
sync.notify();
}
void run() BOOST_OVERRIDE
{
Lock l(sync); // wait for the scoped lock
start_latch.wait();
// WARNING: ThreadSanitizer: data race:
// BOOST_TEST_MESSAGE(
// BOOST_CURRENT_FUNCTION << ": Hello world! I'am waiting ...");
if (doit) {
throw std::runtime_error("Fatal Error, can't continue!");
}
while (!stopped) {
sync.wait(); // wait for the stop signal ..
Thread::sleep(rand() % 113); // NOLINT
}
};
#ifdef USE_UNIQUE_PTR
boost::unique_ptr<Runnable> clone() const BOOST_OVERRIDE
{
return boost::make_unique<BadTask>();
}
#endif
};
//==================================================
BOOST_AUTO_TEST_CASE(ThreadTaskThrow_test)
{
Stopwatch sw;
{
BadTask task(true);
Thread thread(task);
start_latch.reset(1);
thread.start(); // first the task will wait for the lock
start_latch.count_down();
boost::this_thread::yield();
BOOST_TEST(thread.is_alive());
}
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
}
BOOST_AUTO_TEST_CASE(ThreadTaskJoin_test)
{
Stopwatch sw;
{
BadTask task(false);
Thread thread(task);
start_latch.reset(1);
thread.start();
start_latch.count_down();
boost::this_thread::yield();
task.stop();
BOOST_TEST(thread.is_alive());
}
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
}
BOOST_AUTO_TEST_CASE(ThreadLivetime_test)
{
Stopwatch sw;
{
Thread thread;
boost::shared_ptr<Thread> ptrThread(thread.clone());
thread.start();
BOOST_TEST(thread.is_alive());
thread.join();
BOOST_TEST(!ptrThread->is_alive());
ptrThread->join();
}
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
}
BOOST_AUTO_TEST_CASE(ThreadNanoSleep_test)
{
#ifndef _WIN32
signal(SIGALRM, &handler);
alarm(1); // s
#endif
{
Stopwatch sw;
Thread::sleep(1234, 999); // ms + ns
ns d = sw.elapsed();
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
BOOST_TEST_WARN(d >= (ms(1234) + ns(999)));
}
{
Stopwatch sw;
Thread::sleep(BOOST_THREAD_TEST_TIME_MS, 999999); // ms + ns
ns d = sw.elapsed();
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
BOOST_TEST_WARN(d >= (ms(BOOST_THREAD_TEST_TIME_MS) + ns(999999)));
}
}
BOOST_AUTO_TEST_CASE(ThreadSleep_test)
{
Stopwatch sw;
Thread::sleep(BOOST_THREAD_TEST_TIME_MS); // 75 ms -> 75000000 nanoseconds
ns d = sw.elapsed();
BOOST_TEST_MESSAGE(
BOOST_CURRENT_FUNCTION << sw.elapsed()); // i.e.: 168410479 nanoseconds
BOOST_TEST_WARN(d >= ns(BOOST_THREAD_TEST_TIME_MS));
}
#ifdef USE_WAIT_FOR
struct wait_data {
# ifndef TEST_INDEPENDENTLY
typedef Synchronized lockable_type;
# else
typedef boost::mutex lockable_type;
# endif
typedef boost::unique_lock<lockable_type> scoped_lock;
bool flag;
lockable_type mtx;
boost::condition_variable cond;
wait_data()
: flag(false)
{ }
// NOTE: return false if condition waiting for is not true! CK
bool predicate() const { return flag; }
void wait()
{
scoped_lock l(mtx);
# ifndef TEST_INDEPENDENTLY
mtx.wait();
# else
while (!predicate()) {
cond.wait(l);
}
# endif
}
// Returns: false if the call is returning because the time specified by
// abs_time was reached, true otherwise.
template <typename Duration> bool timed_wait(Duration d)
{
scoped_lock l(mtx);
# ifndef TEST_INDEPENDENTLY
return mtx.wait_for(ms(d).count());
# else
while (!predicate()) {
if (cond.wait_for(l, d) == boost::cv_status::timeout) {
return false;
}
}
return true; // OK
# endif
}
void signal()
{
scoped_lock l(mtx);
# ifndef TEST_INDEPENDENTLY
mtx.notify_all();
# else
flag = true;
cond.notify_all();
# endif
}
};
const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
typedef Synchronized mutex_type;
void lock_mutexes_slowly(
mutex_type* m1, mutex_type* m2, wait_data* locked, wait_data* quit)
{
using namespace boost;
lock_guard<mutex_type> l1(*m1);
this_thread::sleep_for(ms(BOOST_THREAD_TEST_TIME_MS));
lock_guard<mutex_type> l2(*m2);
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION);
locked->signal();
quit->wait();
}
void lock_pair(mutex_type* m1, mutex_type* m2)
{
using namespace boost;
lock(*m1, *m2);
unique_lock<mutex_type> l1(*m1, adopt_lock), l2(*m2, adopt_lock);
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION);
BOOST_TEST(l1.owns_lock());
BOOST_TEST(l2.owns_lock());
}
BOOST_AUTO_TEST_CASE(test_lock_two_other_thread_locks_in_order)
{
using namespace boost;
mutex_type m1, m2;
wait_data locked;
wait_data release;
thread t(lock_mutexes_slowly, &m1, &m2, &locked, &release);
thread t2(lock_pair, &m1, &m2);
BOOST_TEST(locked.timed_wait(ms(2 * BOOST_THREAD_TEST_TIME_MS)));
release.signal();
BOOST_TEST(t2.try_join_for(ms(4 * BOOST_THREAD_TEST_TIME_MS)));
t2.join(); // just in case of timeout! CK
t.join();
}
BOOST_AUTO_TEST_CASE(test_lock_two_other_thread_locks_in_opposite_order)
{
using namespace boost;
mutex_type m1, m2;
wait_data locked;
wait_data release;
thread t(lock_mutexes_slowly, &m1, &m2, &locked, &release);
thread t2(lock_pair, &m2, &m1); // NOTE: m2 first!
BOOST_TEST(locked.timed_wait(ms(2 * BOOST_THREAD_TEST_TIME_MS)));
release.signal();
BOOST_TEST(t2.try_join_for(ms(4 * BOOST_THREAD_TEST_TIME_MS)));
t2.join(); // just in case of timeout! CK
t.join();
}
void lock_five_mutexes_slowly(mutex_type* m1, mutex_type* m2, mutex_type* m3,
mutex_type* m4, mutex_type* m5, wait_data* locked, wait_data* quit)
{
using namespace boost;
lock_guard<mutex_type> l1(*m1);
this_thread::sleep_for(ms(BOOST_THREAD_TEST_TIME_MS));
lock_guard<mutex_type> l2(*m2);
this_thread::sleep_for(ms(BOOST_THREAD_TEST_TIME_MS));
lock_guard<mutex_type> l3(*m3);
this_thread::sleep_for(ms(BOOST_THREAD_TEST_TIME_MS));
lock_guard<mutex_type> l4(*m4);
this_thread::sleep_for(ms(BOOST_THREAD_TEST_TIME_MS));
lock_guard<mutex_type> l5(*m5);
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION);
locked->signal();
quit->wait();
}
void lock_n(mutex_type* mutexes, unsigned count)
{
using namespace boost;
lock(mutexes, mutexes + count);
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION);
if (count == 1) {
Stopwatch sw;
BOOST_TEST(mutexes[0].wait_for(BOOST_THREAD_TEST_TIME_MS));
BOOST_TEST_MESSAGE(BOOST_CURRENT_FUNCTION << sw.elapsed());
BOOST_TEST(mutexes[0].unlock());
return;
}
for (unsigned i = 0; i < count; ++i) {
ms d(BOOST_THREAD_TEST_TIME_MS);
this_thread::sleep_for(d);
BOOST_TEST(mutexes[i].unlock());
}
}
BOOST_AUTO_TEST_CASE(test_lock_ten_other_thread_locks_in_different_order)
{
using namespace boost;
unsigned const num_mutexes = 10;