// Copyright (c) 2012 by István Váradi // This file is part of libxplcommon, a common utility library for // development related to X-Plane // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. //------------------------------------------------------------------------------ #include #include #include #include //------------------------------------------------------------------------------ using hu::varadiistvan::scpl::Thread; using hu::varadiistvan::scpl::io::Waiter; using hu::varadiistvan::scpl::io::WaitableEvent; //------------------------------------------------------------------------------ class ServerThread : public Thread { private: Waiter waiter; WaitableEvent event; public: ServerThread(); WaitableEvent& getEvent(); virtual void run(); }; //------------------------------------------------------------------------------ inline ServerThread::ServerThread() : event(&waiter) { } //------------------------------------------------------------------------------ inline WaitableEvent& ServerThread::getEvent() { return event; } //------------------------------------------------------------------------------ void ServerThread::run() { printf("ServerThread::run\n"); while(!event.check()) { printf("ServerThread::run: waiting...\n"); waiter.wait(); printf("ServerThread::run: waiting done\n"); } printf("ServerThread::run: finished\n"); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ class BlockingServerThread : public Thread { private: WaitableEvent event; public: WaitableEvent& getEvent(); virtual void run(); }; //------------------------------------------------------------------------------ inline WaitableEvent& BlockingServerThread::getEvent() { return event; } //------------------------------------------------------------------------------ void BlockingServerThread::run() { printf("BlockingServerThread::run: waiting...\n"); if (event.check()) { printf("BlockingServerThread::run: waiting done\n"); } else { printf("BlockingServerThread::run: waiting failed\n"); } printf("BlockingServerThread::run: finished\n"); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ class ClientThread : public Thread { private: WaitableEvent& event; public: ClientThread(WaitableEvent& event); virtual void run(); }; //------------------------------------------------------------------------------ inline ClientThread::ClientThread(WaitableEvent& event) : event(event) { } //------------------------------------------------------------------------------ void ClientThread::run() { printf("ClientThread::run: sleeping\n"); sleep(2000); printf("ClientThread::run: firing event\n"); event.fire(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ template void runTest() { Server serverThread; ClientThread clientThread(serverThread.getEvent()); printf("Starting threads\n"); serverThread.start(); clientThread.start(); printf("Waiting for the client thread\n"); clientThread.join(); printf("Waiting for the server thread\n"); serverThread.join(); printf("Both threads returned\n"); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ int main() { printf("Running the event test with a non-blocking WaitableEvent\n\n"); runTest(); printf("\n\nRunning the event test with a blocking WaitableEvent\n\n"); runTest(); return 0; } //------------------------------------------------------------------------------ // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: