summaryrefslogtreecommitdiffstats
path: root/tests/OSSupport/StressEvent.cpp
blob: 05c47b59919ce971b82cd42b46c13f8ac26806c0 (plain) (blame)
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

// StressEvent.cpp

// Stress-tests the cEvent implementation

#include "Globals.h"
#include <thread>





/** Number of repetitions of the thread loops. */
const int NUM_REPETITIONS = 5000;




// Forward declarations are needed for clang
void runThread(cEvent * a_Event1, cEvent * a_Event2, const char * a_ThreadName);





/** Function that runs in a separate thread, notifies event1 and waits for event2, in a loop, NUM_REPETITIONS times.
This basically simulates a producer / consumer pattern with 2 events, one for "queue empty", the other for "queue full". */
void runThread(cEvent * a_Event1, cEvent * a_Event2, const char * a_ThreadName)
{
	LOG("Thread %s started", a_ThreadName);
	for (int i = 0; i < NUM_REPETITIONS; ++i)
	{
		// LOGD("%s: Waiting for event %p (%d)", a_ThreadName, a_Event2, i);
		a_Event2->Wait();
		// LOGD("%s: Setting event %p (%d)", a_ThreadName, a_Event1, i);
		a_Event1->SetAll();
	}
	LOG("Thread %s finished", a_ThreadName);
}





int main()
{
	LOG("Test started");
	cEvent event1, event2;
	event1.Set();
	std::thread thread1(&runThread, &event1, &event2, "A");
	std::thread thread2(&runThread, &event2, &event1, "B");
	thread1.join();
	thread2.join();
	LOG("Test finished");
	return 0;
}