From 604f65e69f2317dd5dc1aeb8faf3c107cd8a2231 Mon Sep 17 00:00:00 2001 From: Alexander Chemeris Date: Sun, 14 Jul 2013 13:22:31 +0400 Subject: CommonLibs: Port InterthreadTest and SocketsTest to the new Thread API. --- CommonLibs/InterthreadTest.cpp | 135 +++++++++++++++++++++++++---------------- CommonLibs/Makefile.am | 4 +- CommonLibs/SocketsTest.cpp | 91 +++++++++++++++------------ 3 files changed, 137 insertions(+), 93 deletions(-) (limited to 'CommonLibs') diff --git a/CommonLibs/InterthreadTest.cpp b/CommonLibs/InterthreadTest.cpp index 03445d9..f5a5fb3 100644 --- a/CommonLibs/InterthreadTest.cpp +++ b/CommonLibs/InterthreadTest.cpp @@ -1,5 +1,6 @@ /* * Copyright 2008 Free Software Foundation, Inc. +* Copyright 2013 Alexander Chemeris * * * This software is distributed under the terms of the GNU Affero Public License. @@ -27,66 +28,91 @@ #include "Threads.h" #include "Interthread.h" +#include "Configuration.h" #include using namespace std; +ConfigurationTable gConfig; InterthreadQueue gQ; InterthreadMap gMap; -void* qWriter(void*) +class QueueWriter : public Thread { - int *p; - for (int i=0; i<20; i++) { +public: + QueueWriter() : Thread("QueueWriter") {} + +protected: + virtual void runThread() + { + int *p; + for (int i=0; i<20; i++) { + p = new int; + *p = i; + COUT("queue write " << *p); + gQ.write(p); + msleep(1); + } p = new int; - *p = i; - COUT("queue write " << *p); + *p = -1; gQ.write(p); - if (random()%2) sleep(1); } - p = new int; - *p = -1; - gQ.write(p); - return NULL; -} +}; -void* qReader(void*) +class QueueReader : public Thread { - bool done = false; - while (!done) { - int *p = gQ.read(); - COUT("queue read " << *p); - if (*p<0) done=true; - delete p; +public: + QueueReader() : Thread("QueueReader") {} + +protected: + virtual void runThread() + { + bool done = false; + while (!done) { + int *p = gQ.read(); + COUT("queue read " << *p); + if (*p<0) done=true; + delete p; + } } - return NULL; -} +}; -void* mapWriter(void*) +class MapWriter : public Thread { - int *p; - for (int i=0; i<20; i++) { - p = new int; - *p = i; - COUT("map write " << *p); - gMap.write(i,p); - if (random()%2) sleep(1); +public: + MapWriter() : Thread("MapWriter") {} + +protected: + virtual void runThread() + { + int *p; + for (int i=0; i<20; i++) { + p = new int; + *p = i; + COUT("map write " << *p); + gMap.write(i,p); + msleep(1); + } } - return NULL; -} +}; -void* mapReader(void*) +class MapReader : public Thread { - for (int i=0; i<20; i++) { - int *p = gMap.read(i); - COUT("map read " << *p); - // InterthreadMap will delete the pointers - // delete p; +public: + MapReader() : Thread("MapReader") {} + +protected: + virtual void runThread() + { + for (int i=0; i<20; i++) { + int *p = gMap.read(i); + COUT("map read " << *p); + // InterthreadMap will delete the pointers + } } - return NULL; -} +}; @@ -95,20 +121,25 @@ void* mapReader(void*) int main(int argc, char *argv[]) { - Thread qReaderThread; - qReaderThread.start(qReader,NULL); - Thread mapReaderThread; - mapReaderThread.start(mapReader,NULL); - - Thread qWriterThread; - qWriterThread.start(qWriter,NULL); - Thread mapWriterThread; - mapWriterThread.start(mapWriter,NULL); - - qReaderThread.join(); - qWriterThread.join(); - mapReaderThread.join(); - mapWriterThread.join(); + COUT("TEST 1: InterthreadQueue") + QueueReader qReaderThread; + QueueWriter qWriterThread; + qReaderThread.startThread(); + qWriterThread.startThread(); + // stopThread() will wait for a thread to stop for 5 seconds, which + // is more than enough for this test to finish. + qReaderThread.stopThread(); + qWriterThread.stopThread(); + + COUT("TEST 2: InterthreadMap") + MapReader mapReaderThread; + mapReaderThread.startThread(); + MapWriter mapWriterThread; + mapWriterThread.startThread(); + // stopThread() will wait for a thread to stop for 5 seconds, which + // is more than enough for this test to finish. + mapReaderThread.stopThread(); + mapWriterThread.stopThread(); } diff --git a/CommonLibs/Makefile.am b/CommonLibs/Makefile.am index 26a55ed..271a8ef 100644 --- a/CommonLibs/Makefile.am +++ b/CommonLibs/Makefile.am @@ -81,11 +81,11 @@ BitVectorTest_SOURCES = BitVectorTest.cpp BitVectorTest_LDADD = libcommon.la $(SQLITE_LA) InterthreadTest_SOURCES = InterthreadTest.cpp -InterthreadTest_LDADD = libcommon.la +InterthreadTest_LDADD = libcommon.la $(SQLITE_LA) InterthreadTest_LDFLAGS = -lpthread SocketsTest_SOURCES = SocketsTest.cpp -SocketsTest_LDADD = libcommon.la +SocketsTest_LDADD = libcommon.la $(SQLITE_LA) SocketsTest_LDFLAGS = -lpthread TimevalTest_SOURCES = TimevalTest.cpp diff --git a/CommonLibs/SocketsTest.cpp b/CommonLibs/SocketsTest.cpp index 9a4997b..5aab615 100644 --- a/CommonLibs/SocketsTest.cpp +++ b/CommonLibs/SocketsTest.cpp @@ -1,5 +1,6 @@ /* * Copyright 2008 Free Software Foundation, Inc. +* Copyright 2013 Alexander Chemeris * * * This software is distributed under the terms of the GNU Affero Public License. @@ -28,59 +29,73 @@ #include "Sockets.h" #include "Threads.h" -#include +#include "Configuration.h" +#include "Timeval.h" #include +ConfigurationTable gConfig; static const int gNumToSend = 10; -void *testReaderIP(void *) +class TestReaderIP : public Thread { - UDPSocket readSocket(5934, "localhost", 5061); - readSocket.nonblocking(); - int rc = 0; - while (rc0) { - COUT("read: " << buf); - rc++; - } else { - sleep(2); +public: + TestReaderIP() : Thread("TestReaderIP") {} + +protected: + virtual void runThread() + { + UDPSocket readSocket(5934, "localhost", 5061); + readSocket.nonblocking(); + int rc = 0; + while (rc0) { + COUT("IP read: " << buf); + rc++; + } else { + COUT("IP sleeping..."); + sleep(2); + } } } - return NULL; -} - +}; - -void *testReaderUnix(void *) +class TestReaderUnix : public Thread { - UDDSocket readSocket("testDestination"); - readSocket.nonblocking(); - int rc = 0; - while (rc0) { - COUT("read: " << buf); - rc++; - } else { - sleep(2); +public: + TestReaderUnix() : Thread("TestReaderUnix") {} + +protected: + virtual void runThread() + { + UDDSocket readSocket("testDestination"); + readSocket.nonblocking(); + int rc = 0; + while (rc0) { + COUT("UNIX read: " << buf); + rc++; + } else { + COUT("UNIX sleeping..."); + sleep(2); + } } } - return NULL; -} +}; int main(int argc, char * argv[] ) { - Thread readerThreadIP; - readerThreadIP.start(testReaderIP,NULL); - Thread readerThreadUnix; - readerThreadUnix.start(testReaderUnix,NULL); + TestReaderIP readerThreadIP; + TestReaderUnix readerThreadUnix; + readerThreadIP.startThread(); + readerThreadUnix.startThread(); UDPSocket socket1(5061, "127.0.0.1",5934); UDDSocket socket1U("testSource","testDestination"); @@ -92,12 +107,10 @@ int main(int argc, char * argv[] ) for (int i=0; i