diff --git a/YSFParrot/Makefile b/YSFParrot/Makefile index d27c4cd..9becb31 100644 --- a/YSFParrot/Makefile +++ b/YSFParrot/Makefile @@ -4,7 +4,7 @@ CFLAGS = -g -O3 -Wall -std=c++0x LIBS = LDFLAGS = -g -OBJECTS = Log.o Network.o Parrot.o StopWatch.o Timer.o UDPSocket.o Utils.o YSFParrot.o +OBJECTS = Log.o Network.o Parrot.o StopWatch.o Thread.o Timer.o UDPSocket.o Utils.o YSFParrot.o all: YSFParrot diff --git a/YSFParrot/Makefile.Solaris b/YSFParrot/Makefile.Solaris index 8cad679..3a1bc1b 100644 --- a/YSFParrot/Makefile.Solaris +++ b/YSFParrot/Makefile.Solaris @@ -4,7 +4,7 @@ CFLAGS = -g -O3 -Wall -std=c++0x LIBS = -lsocket LDFLAGS = -g -OBJECTS = Log.o Network.o Parrot.o StopWatch.o Timer.o UDPSocket.o Utils.o YSFParrot.o +OBJECTS = Log.o Network.o Parrot.o StopWatch.o Thread.o Timer.o UDPSocket.o Utils.o YSFParrot.o all: YSFParrot diff --git a/YSFParrot/Thread.cpp b/YSFParrot/Thread.cpp new file mode 100644 index 0000000..b334436 --- /dev/null +++ b/YSFParrot/Thread.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "Thread.h" + +#if defined(_WIN32) || defined(_WIN64) + +CThread::CThread() : +m_handle() +{ +} + +CThread::~CThread() +{ +} + +bool CThread::run() +{ + m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); + + return m_handle != NULL; +} + + +void CThread::wait() +{ + ::WaitForSingleObject(m_handle, INFINITE); + + ::CloseHandle(m_handle); +} + + +DWORD CThread::helper(LPVOID arg) +{ + CThread* p = (CThread*)arg; + + p->entry(); + + return 0UL; +} + +void CThread::sleep(unsigned int ms) +{ + ::Sleep(ms); +} + +#else + +#include + +CThread::CThread() : +m_thread() +{ +} + +CThread::~CThread() +{ +} + +bool CThread::run() +{ + return ::pthread_create(&m_thread, NULL, helper, this) == 0; +} + + +void CThread::wait() +{ + ::pthread_join(m_thread, NULL); +} + + +void* CThread::helper(void* arg) +{ + CThread* p = (CThread*)arg; + + p->entry(); + + return NULL; +} + +void CThread::sleep(unsigned int ms) +{ + ::usleep(ms * 1000); +} + +#endif diff --git a/YSFParrot/Thread.h b/YSFParrot/Thread.h new file mode 100644 index 0000000..352d938 --- /dev/null +++ b/YSFParrot/Thread.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#if !defined(THREAD_H) +#define THREAD_H + +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#endif + +class CThread +{ +public: + CThread(); + virtual ~CThread(); + + virtual bool run(); + + virtual void entry() = 0; + + virtual void wait(); + + static void sleep(unsigned int ms); + +private: +#if defined(_WIN32) || defined(_WIN64) + HANDLE m_handle; +#else + pthread_t m_thread; +#endif + +#if defined(_WIN32) || defined(_WIN64) + static DWORD __stdcall helper(LPVOID arg); +#else + static void* helper(void* arg); +#endif +}; + +#endif diff --git a/YSFParrot/YSFParrot.cpp b/YSFParrot/YSFParrot.cpp index 6927fcb..7939215 100644 --- a/YSFParrot/YSFParrot.cpp +++ b/YSFParrot/YSFParrot.cpp @@ -21,6 +21,7 @@ #include "Parrot.h" #include "Network.h" #include "Version.h" +#include "Thread.h" #include "Timer.h" #include "Log.h" @@ -148,13 +149,8 @@ void CYSFParrot::run() parrot.end(); } - if (ms < 5U) { -#if defined(_WIN32) || defined(_WIN64) - ::Sleep(5UL); // 5ms -#else - ::usleep(5000); // 5ms -#endif - } + if (ms < 5U) + CThread::sleep(5U); } network.close(); diff --git a/YSFParrot/YSFParrot.vcxproj b/YSFParrot/YSFParrot.vcxproj index cf30687..21bf7d3 100644 --- a/YSFParrot/YSFParrot.vcxproj +++ b/YSFParrot/YSFParrot.vcxproj @@ -148,6 +148,7 @@ + @@ -160,6 +161,7 @@ + diff --git a/YSFParrot/YSFParrot.vcxproj.filters b/YSFParrot/YSFParrot.vcxproj.filters index 14606c8..476a86b 100644 --- a/YSFParrot/YSFParrot.vcxproj.filters +++ b/YSFParrot/YSFParrot.vcxproj.filters @@ -41,6 +41,9 @@ Header Files + + Header Files + @@ -67,5 +70,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/YSFReflector/Makefile b/YSFReflector/Makefile index 783d774..05fdc09 100644 --- a/YSFReflector/Makefile +++ b/YSFReflector/Makefile @@ -4,7 +4,7 @@ CFLAGS = -g -O3 -Wall -std=c++0x LIBS = LDFLAGS = -g -OBJECTS = Conf.o Log.o Network.o StopWatch.o Timer.o UDPSocket.o Utils.o YSFReflector.o +OBJECTS = Conf.o Log.o Network.o StopWatch.o Thread.o Timer.o UDPSocket.o Utils.o YSFReflector.o all: YSFReflector diff --git a/YSFReflector/Makefile.Solaris b/YSFReflector/Makefile.Solaris index 48af4fa..1376bb3 100644 --- a/YSFReflector/Makefile.Solaris +++ b/YSFReflector/Makefile.Solaris @@ -4,7 +4,7 @@ CFLAGS = -g -O3 -Wall -std=c++0x LIBS = -lsocket LDFLAGS = -g -OBJECTS = Conf.o Log.o Network.o StopWatch.o Timer.o UDPSocket.o Utils.o YSFReflector.o +OBJECTS = Conf.o Log.o Network.o StopWatch.o Thread.o Timer.o UDPSocket.o Utils.o YSFReflector.o all: YSFReflector diff --git a/YSFReflector/Thread.cpp b/YSFReflector/Thread.cpp new file mode 100644 index 0000000..b334436 --- /dev/null +++ b/YSFReflector/Thread.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "Thread.h" + +#if defined(_WIN32) || defined(_WIN64) + +CThread::CThread() : +m_handle() +{ +} + +CThread::~CThread() +{ +} + +bool CThread::run() +{ + m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); + + return m_handle != NULL; +} + + +void CThread::wait() +{ + ::WaitForSingleObject(m_handle, INFINITE); + + ::CloseHandle(m_handle); +} + + +DWORD CThread::helper(LPVOID arg) +{ + CThread* p = (CThread*)arg; + + p->entry(); + + return 0UL; +} + +void CThread::sleep(unsigned int ms) +{ + ::Sleep(ms); +} + +#else + +#include + +CThread::CThread() : +m_thread() +{ +} + +CThread::~CThread() +{ +} + +bool CThread::run() +{ + return ::pthread_create(&m_thread, NULL, helper, this) == 0; +} + + +void CThread::wait() +{ + ::pthread_join(m_thread, NULL); +} + + +void* CThread::helper(void* arg) +{ + CThread* p = (CThread*)arg; + + p->entry(); + + return NULL; +} + +void CThread::sleep(unsigned int ms) +{ + ::usleep(ms * 1000); +} + +#endif diff --git a/YSFReflector/Thread.h b/YSFReflector/Thread.h new file mode 100644 index 0000000..352d938 --- /dev/null +++ b/YSFReflector/Thread.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#if !defined(THREAD_H) +#define THREAD_H + +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#endif + +class CThread +{ +public: + CThread(); + virtual ~CThread(); + + virtual bool run(); + + virtual void entry() = 0; + + virtual void wait(); + + static void sleep(unsigned int ms); + +private: +#if defined(_WIN32) || defined(_WIN64) + HANDLE m_handle; +#else + pthread_t m_thread; +#endif + +#if defined(_WIN32) || defined(_WIN64) + static DWORD __stdcall helper(LPVOID arg); +#else + static void* helper(void* arg); +#endif +}; + +#endif diff --git a/YSFReflector/YSFReflector.cpp b/YSFReflector/YSFReflector.cpp index cefec6d..241d5eb 100644 --- a/YSFReflector/YSFReflector.cpp +++ b/YSFReflector/YSFReflector.cpp @@ -20,6 +20,7 @@ #include "StopWatch.h" #include "Network.h" #include "Version.h" +#include "Thread.h" #include "Log.h" #if defined(_WIN32) || defined(_WIN64) @@ -300,13 +301,8 @@ void CYSFReflector::run() dumpTimer.start(); } - if (ms < 5U) { -#if defined(_WIN32) || defined(_WIN64) - ::Sleep(5UL); // 5ms -#else - ::usleep(5000); // 5ms -#endif - } + if (ms < 5U) + CThread::sleep(5U); } network.close(); diff --git a/YSFReflector/YSFReflector.vcxproj b/YSFReflector/YSFReflector.vcxproj index 6e02fd1..a4acc04 100644 --- a/YSFReflector/YSFReflector.vcxproj +++ b/YSFReflector/YSFReflector.vcxproj @@ -150,6 +150,7 @@ + @@ -162,6 +163,7 @@ + diff --git a/YSFReflector/YSFReflector.vcxproj.filters b/YSFReflector/YSFReflector.vcxproj.filters index c2fb66c..a76cd78 100644 --- a/YSFReflector/YSFReflector.vcxproj.filters +++ b/YSFReflector/YSFReflector.vcxproj.filters @@ -41,6 +41,9 @@ Header Files + + Header Files + @@ -67,5 +70,8 @@ Source Files + + Source Files + \ No newline at end of file