source: xplcommon/test/testevent.cc@ 39:f9c840ac038d

Last change on this file since 39:f9c840ac038d was 8:dba41f33ddce, checked in by István Váradi <ivaradi@…>, 12 years ago

Clarified the blocking and non-blocking behaviour and extended the test program to test both

File size: 5.5 KB
Line 
1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30//------------------------------------------------------------------------------
31
32#include <xplcommon/Thread.h>
33#include <xplcommon/Waiter.h>
34#include <xplcommon/WaitableEvent.h>
35
36#include <cstdio>
37
38//------------------------------------------------------------------------------
39
40using xplcommon::Thread;
41using xplcommon::Waiter;
42using xplcommon::WaitableEvent;
43
44//------------------------------------------------------------------------------
45
46class ServerThread : public Thread
47{
48private:
49 Waiter waiter;
50
51 WaitableEvent event;
52
53public:
54 ServerThread();
55
56 WaitableEvent& getEvent();
57
58 virtual void run();
59};
60
61//------------------------------------------------------------------------------
62
63inline ServerThread::ServerThread() :
64 event(&waiter)
65{
66}
67
68//------------------------------------------------------------------------------
69
70inline WaitableEvent& ServerThread::getEvent()
71{
72 return event;
73}
74
75//------------------------------------------------------------------------------
76
77void ServerThread::run()
78{
79 printf("ServerThread::run\n");
80 while(!event.check()) {
81 printf("ServerThread::run: waiting...\n");
82 waiter.wait();
83 printf("ServerThread::run: waiting done\n");
84 }
85 printf("ServerThread::run: finished\n");
86}
87
88//------------------------------------------------------------------------------
89//------------------------------------------------------------------------------
90
91class BlockingServerThread : public Thread
92{
93private:
94 WaitableEvent event;
95
96public:
97 WaitableEvent& getEvent();
98
99 virtual void run();
100};
101
102//------------------------------------------------------------------------------
103
104inline WaitableEvent& BlockingServerThread::getEvent()
105{
106 return event;
107}
108
109//------------------------------------------------------------------------------
110
111void BlockingServerThread::run()
112{
113 printf("BlockingServerThread::run: waiting...\n");
114 if (event.check()) {
115 printf("BlockingServerThread::run: waiting done\n");
116 } else {
117 printf("BlockingServerThread::run: waiting failed\n");
118 }
119 printf("BlockingServerThread::run: finished\n");
120}
121
122
123//------------------------------------------------------------------------------
124//------------------------------------------------------------------------------
125
126class ClientThread : public Thread
127{
128private:
129 WaitableEvent& event;
130
131public:
132 ClientThread(WaitableEvent& event);
133
134 virtual void run();
135};
136
137//------------------------------------------------------------------------------
138
139inline ClientThread::ClientThread(WaitableEvent& event) :
140 event(event)
141{
142}
143
144//------------------------------------------------------------------------------
145
146void ClientThread::run()
147{
148 printf("ClientThread::run: sleeping\n");
149 sleep(2000);
150 printf("ClientThread::run: firing event\n");
151 event.fire();
152}
153
154//------------------------------------------------------------------------------
155//------------------------------------------------------------------------------
156
157template <class Server> void runTest()
158{
159 Server serverThread;
160 ClientThread clientThread(serverThread.getEvent());
161
162 printf("Starting threads\n");
163 serverThread.start();
164 clientThread.start();
165
166 printf("Waiting for the client thread\n");
167 clientThread.join();
168 printf("Waiting for the server thread\n");
169 serverThread.join();
170 printf("Both threads returned\n");
171}
172
173//------------------------------------------------------------------------------
174//------------------------------------------------------------------------------
175
176int main()
177{
178 printf("Running the event test with a non-blocking WaitableEvent\n\n");
179 runTest<ServerThread>();
180
181 printf("\n\nRunning the event test with a blocking WaitableEvent\n\n");
182 runTest<BlockingServerThread>();
183
184 return 0;
185}
186
187//------------------------------------------------------------------------------
188
189// Local Variables:
190// mode: C++
191// c-basic-offset: 4
192// indent-tabs-mode: nil
193// End:
Note: See TracBrowser for help on using the repository browser.