[4] | 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 |
|
---|
| 40 | using xplcommon::Thread;
|
---|
| 41 | using xplcommon::Waiter;
|
---|
| 42 | using xplcommon::WaitableEvent;
|
---|
| 43 |
|
---|
| 44 | //------------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | class ServerThread : public Thread
|
---|
| 47 | {
|
---|
| 48 | private:
|
---|
| 49 | Waiter waiter;
|
---|
| 50 |
|
---|
| 51 | WaitableEvent event;
|
---|
| 52 |
|
---|
| 53 | public:
|
---|
| 54 | ServerThread();
|
---|
| 55 |
|
---|
| 56 | WaitableEvent& getEvent();
|
---|
| 57 |
|
---|
| 58 | virtual void run();
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | //------------------------------------------------------------------------------
|
---|
| 62 |
|
---|
| 63 | inline ServerThread::ServerThread() :
|
---|
| 64 | event(&waiter)
|
---|
| 65 | {
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | //------------------------------------------------------------------------------
|
---|
| 69 |
|
---|
| 70 | inline WaitableEvent& ServerThread::getEvent()
|
---|
| 71 | {
|
---|
| 72 | return event;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | //------------------------------------------------------------------------------
|
---|
| 76 |
|
---|
| 77 | void 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 |
|
---|
[8] | 91 | class BlockingServerThread : public Thread
|
---|
| 92 | {
|
---|
| 93 | private:
|
---|
| 94 | WaitableEvent event;
|
---|
| 95 |
|
---|
| 96 | public:
|
---|
| 97 | WaitableEvent& getEvent();
|
---|
| 98 |
|
---|
| 99 | virtual void run();
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | //------------------------------------------------------------------------------
|
---|
| 103 |
|
---|
| 104 | inline WaitableEvent& BlockingServerThread::getEvent()
|
---|
| 105 | {
|
---|
| 106 | return event;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | //------------------------------------------------------------------------------
|
---|
| 110 |
|
---|
| 111 | void 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 |
|
---|
[4] | 126 | class ClientThread : public Thread
|
---|
| 127 | {
|
---|
| 128 | private:
|
---|
| 129 | WaitableEvent& event;
|
---|
| 130 |
|
---|
| 131 | public:
|
---|
| 132 | ClientThread(WaitableEvent& event);
|
---|
| 133 |
|
---|
| 134 | virtual void run();
|
---|
| 135 | };
|
---|
| 136 |
|
---|
| 137 | //------------------------------------------------------------------------------
|
---|
| 138 |
|
---|
| 139 | inline ClientThread::ClientThread(WaitableEvent& event) :
|
---|
| 140 | event(event)
|
---|
| 141 | {
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | //------------------------------------------------------------------------------
|
---|
| 145 |
|
---|
| 146 | void 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 |
|
---|
[8] | 157 | template <class Server> void runTest()
|
---|
[4] | 158 | {
|
---|
[8] | 159 | Server serverThread;
|
---|
[4] | 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");
|
---|
[8] | 171 | }
|
---|
| 172 |
|
---|
| 173 | //------------------------------------------------------------------------------
|
---|
| 174 | //------------------------------------------------------------------------------
|
---|
| 175 |
|
---|
| 176 | int 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>();
|
---|
[4] | 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:
|
---|