source: vscpl/test/testevent.cc@ 15:4eba224d58f9

Last change on this file since 15:4eba224d58f9 was 7:e0118c752a06, checked in by István Váradi <ivaradi@…>, 12 years ago

Imported the WaitableEvent class

File size: 5.6 KB
RevLine 
[7]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 <hu/varadiistvan/scpl/io/Waiter.h>
33#include <hu/varadiistvan/scpl/io/WaitableEvent.h>
34
35#include <hu/varadiistvan/scpl/Thread.h>
36
37#include <cstdio>
38
39//------------------------------------------------------------------------------
40
41using hu::varadiistvan::scpl::Thread;
42using hu::varadiistvan::scpl::io::Waiter;
43using hu::varadiistvan::scpl::io::WaitableEvent;
44
45//------------------------------------------------------------------------------
46
47class ServerThread : public Thread
48{
49private:
50 Waiter waiter;
51
52 WaitableEvent event;
53
54public:
55 ServerThread();
56
57 WaitableEvent& getEvent();
58
59 virtual void run();
60};
61
62//------------------------------------------------------------------------------
63
64inline ServerThread::ServerThread() :
65 event(&waiter)
66{
67}
68
69//------------------------------------------------------------------------------
70
71inline WaitableEvent& ServerThread::getEvent()
72{
73 return event;
74}
75
76//------------------------------------------------------------------------------
77
78void ServerThread::run()
79{
80 printf("ServerThread::run\n");
81 while(!event.check()) {
82 printf("ServerThread::run: waiting...\n");
83 waiter.wait();
84 printf("ServerThread::run: waiting done\n");
85 }
86 printf("ServerThread::run: finished\n");
87}
88
89//------------------------------------------------------------------------------
90//------------------------------------------------------------------------------
91
92class BlockingServerThread : public Thread
93{
94private:
95 WaitableEvent event;
96
97public:
98 WaitableEvent& getEvent();
99
100 virtual void run();
101};
102
103//------------------------------------------------------------------------------
104
105inline WaitableEvent& BlockingServerThread::getEvent()
106{
107 return event;
108}
109
110//------------------------------------------------------------------------------
111
112void BlockingServerThread::run()
113{
114 printf("BlockingServerThread::run: waiting...\n");
115 if (event.check()) {
116 printf("BlockingServerThread::run: waiting done\n");
117 } else {
118 printf("BlockingServerThread::run: waiting failed\n");
119 }
120 printf("BlockingServerThread::run: finished\n");
121}
122
123
124//------------------------------------------------------------------------------
125//------------------------------------------------------------------------------
126
127class ClientThread : public Thread
128{
129private:
130 WaitableEvent& event;
131
132public:
133 ClientThread(WaitableEvent& event);
134
135 virtual void run();
136};
137
138//------------------------------------------------------------------------------
139
140inline ClientThread::ClientThread(WaitableEvent& event) :
141 event(event)
142{
143}
144
145//------------------------------------------------------------------------------
146
147void ClientThread::run()
148{
149 printf("ClientThread::run: sleeping\n");
150 sleep(2000);
151 printf("ClientThread::run: firing event\n");
152 event.fire();
153}
154
155//------------------------------------------------------------------------------
156//------------------------------------------------------------------------------
157
158template <class Server> void runTest()
159{
160 Server serverThread;
161 ClientThread clientThread(serverThread.getEvent());
162
163 printf("Starting threads\n");
164 serverThread.start();
165 clientThread.start();
166
167 printf("Waiting for the client thread\n");
168 clientThread.join();
169 printf("Waiting for the server thread\n");
170 serverThread.join();
171 printf("Both threads returned\n");
172}
173
174//------------------------------------------------------------------------------
175//------------------------------------------------------------------------------
176
177int main()
178{
179 printf("Running the event test with a non-blocking WaitableEvent\n\n");
180 runTest<ServerThread>();
181
182 printf("\n\nRunning the event test with a blocking WaitableEvent\n\n");
183 runTest<BlockingServerThread>();
184
185 return 0;
186}
187
188//------------------------------------------------------------------------------
189
190// Local Variables:
191// mode: C++
192// c-basic-offset: 4
193// indent-tabs-mode: nil
194// End:
Note: See TracBrowser for help on using the repository browser.