summaryrefslogtreecommitdiffstats
path: root/src/OSSupport
diff options
context:
space:
mode:
Diffstat (limited to 'src/OSSupport')
-rw-r--r--src/OSSupport/CMakeLists.txt2
-rw-r--r--src/OSSupport/CriticalSection.cpp4
-rw-r--r--src/OSSupport/CriticalSection.h2
-rw-r--r--src/OSSupport/IsThread.cpp158
-rw-r--r--src/OSSupport/IsThread.h44
-rw-r--r--src/OSSupport/Thread.cpp137
-rw-r--r--src/OSSupport/Thread.h26
7 files changed, 34 insertions, 339 deletions
diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt
index 429949c59..1c8da5ddd 100644
--- a/src/OSSupport/CMakeLists.txt
+++ b/src/OSSupport/CMakeLists.txt
@@ -16,7 +16,6 @@ SET (SRCS
Sleep.cpp
Socket.cpp
SocketThreads.cpp
- Thread.cpp
Timer.cpp)
SET (HDRS
@@ -32,7 +31,6 @@ SET (HDRS
Sleep.h
Socket.h
SocketThreads.h
- Thread.h
Timer.h)
if(NOT MSVC)
diff --git a/src/OSSupport/CriticalSection.cpp b/src/OSSupport/CriticalSection.cpp
index 5dfc8b5f9..3e8e7498d 100644
--- a/src/OSSupport/CriticalSection.cpp
+++ b/src/OSSupport/CriticalSection.cpp
@@ -59,7 +59,7 @@ void cCriticalSection::Lock()
#ifdef _DEBUG
m_IsLocked += 1;
- m_OwningThreadID = cIsThread::GetCurrentID();
+ m_OwningThreadID = std::this_thread::get_id();
#endif // _DEBUG
}
@@ -97,7 +97,7 @@ bool cCriticalSection::IsLocked(void)
bool cCriticalSection::IsLockedByCurrentThread(void)
{
- return ((m_IsLocked > 0) && (m_OwningThreadID == cIsThread::GetCurrentID()));
+ return ((m_IsLocked > 0) && (m_OwningThreadID == std::this_thread::get_id()));
}
#endif // _DEBUG
diff --git a/src/OSSupport/CriticalSection.h b/src/OSSupport/CriticalSection.h
index c3c6e57f0..7822a5471 100644
--- a/src/OSSupport/CriticalSection.h
+++ b/src/OSSupport/CriticalSection.h
@@ -27,7 +27,7 @@ public:
private:
#ifdef _DEBUG
int m_IsLocked; // Number of times this CS is locked
- unsigned long m_OwningThreadID;
+ std::thread::id m_OwningThreadID;
#endif // _DEBUG
#ifdef _WIN32
diff --git a/src/OSSupport/IsThread.cpp b/src/OSSupport/IsThread.cpp
index 1a436623a..4864ef28b 100644
--- a/src/OSSupport/IsThread.cpp
+++ b/src/OSSupport/IsThread.cpp
@@ -5,65 +5,18 @@
// This class will eventually suupersede the old cThread class
#include "Globals.h"
-
#include "IsThread.h"
-// When in MSVC, the debugger provides "thread naming" by catching special exceptions. Interface here:
-#if defined(_MSC_VER) && defined(_DEBUG)
-//
-// Usage: SetThreadName (-1, "MainThread");
-//
-
-// Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
-
-const DWORD MS_VC_EXCEPTION = 0x406D1388;
-
-#pragma pack(push, 8)
-typedef struct tagTHREADNAME_INFO
-{
- DWORD dwType; // Must be 0x1000.
- LPCSTR szName; // Pointer to name (in user addr space).
- DWORD dwThreadID; // Thread ID (-1 = caller thread).
- DWORD dwFlags; // Reserved for future use, must be zero.
-} THREADNAME_INFO;
-#pragma pack(pop)
-
-static void SetThreadName(DWORD dwThreadID, const char * threadName)
-{
- THREADNAME_INFO info;
- info.dwType = 0x1000;
- info.szName = threadName;
- info.dwThreadID = dwThreadID;
- info.dwFlags = 0;
-
- __try
- {
- RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
- }
- __except (EXCEPTION_EXECUTE_HANDLER)
- {
- }
-}
-#endif // _MSC_VER && _DEBUG
-
-
-
-
-
////////////////////////////////////////////////////////////////////////////////
// cIsThread:
-cIsThread::cIsThread(const AString & iThreadName) :
+cIsThread::cIsThread(const AString & a_ThreadName) :
m_ShouldTerminate(false),
- m_ThreadName(iThreadName),
- #ifdef _WIN32
- m_ThreadID(0),
- #endif
- m_Handle(NULL_HANDLE)
+ m_ThreadName(a_ThreadName)
{
}
@@ -83,35 +36,16 @@ cIsThread::~cIsThread()
bool cIsThread::Start(void)
{
- ASSERT(m_Handle == NULL_HANDLE); // Has already started one thread?
- #ifdef _WIN32
- // Create the thread suspended, so that the mHandle variable is valid in the thread procedure
- m_ThreadID = 0;
- m_Handle = CreateThread(NULL, 0, thrExecute, this, CREATE_SUSPENDED, &m_ThreadID);
- if (m_Handle == NULL)
- {
- LOGERROR("ERROR: Could not create thread \"%s\", GLE = %u!", m_ThreadName.c_str(), (unsigned)GetLastError());
- return false;
- }
- ResumeThread(m_Handle);
-
- #if defined(_DEBUG) && defined(_MSC_VER)
- // Thread naming is available only in MSVC
- if (!m_ThreadName.empty())
- {
- SetThreadName(m_ThreadID, m_ThreadName.c_str());
- }
- #endif // _DEBUG and _MSC_VER
-
- #else // _WIN32
- if (pthread_create(&m_Handle, NULL, thrExecute, this))
- {
- LOGERROR("ERROR: Could not create thread \"%s\", !", m_ThreadName.c_str());
- return false;
- }
- #endif // else _WIN32
-
- return true;
+ try
+ {
+ m_Thread = std::thread(&cIsThread::Execute, this);
+ return true;
+ }
+ catch (std::system_error & a_Exception)
+ {
+ LOGERROR("ERROR: Could not create thread \"%s\", error = %s!", m_ThreadName.c_str(), a_Exception.code(), a_Exception.what());
+ return false;
+ }
}
@@ -120,10 +54,6 @@ bool cIsThread::Start(void)
void cIsThread::Stop(void)
{
- if (m_Handle == NULL_HANDLE)
- {
- return;
- }
m_ShouldTerminate = true;
Wait();
}
@@ -133,60 +63,30 @@ void cIsThread::Stop(void)
bool cIsThread::Wait(void)
-{
- if (m_Handle == NULL_HANDLE)
- {
- return true;
- }
-
+{
#ifdef LOGD // ProtoProxy doesn't have LOGD
LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
#endif // LOGD
-
- #ifdef _WIN32
- int res = WaitForSingleObject(m_Handle, INFINITE);
- m_Handle = NULL;
-
- #ifdef LOGD // ProtoProxy doesn't have LOGD
- LOGD("Thread %s finished", m_ThreadName.c_str());
- #endif // LOGD
-
- return (res == WAIT_OBJECT_0);
- #else // _WIN32
- int res = pthread_join(m_Handle, NULL);
- m_Handle = NULL_HANDLE;
-
- #ifdef LOGD // ProtoProxy doesn't have LOGD
- LOGD("Thread %s finished", m_ThreadName.c_str());
- #endif // LOGD
-
- return (res == 0);
- #endif // else _WIN32
-}
-
-
-
+ if (m_Thread.joinable())
+ {
+ try
+ {
+ m_Thread.join();
+ return true;
+ }
+ catch (std::system_error & a_Exception)
+ {
+ LOGERROR("ERROR: Could wait for thread \"%s\" to finish, error = %s!", m_ThreadName.c_str(), a_Exception.code(), a_Exception.what());
+ return false;
+ }
+ }
-unsigned long cIsThread::GetCurrentID(void)
-{
- #ifdef _WIN32
- return (unsigned long) GetCurrentThreadId();
- #else
- return (unsigned long) pthread_self();
+ #ifdef LOGD // ProtoProxy doesn't have LOGD
+ LOGD("Thread %s finished", m_ThreadName.c_str());
#endif
-}
-
-
-
-bool cIsThread::IsCurrentThread(void) const
-{
- #ifdef _WIN32
- return (GetCurrentThreadId() == m_ThreadID);
- #else
- return (m_Handle == pthread_self());
- #endif
+ return true;
}
diff --git a/src/OSSupport/IsThread.h b/src/OSSupport/IsThread.h
index 5de5f31c4..8dfad84cb 100644
--- a/src/OSSupport/IsThread.h
+++ b/src/OSSupport/IsThread.h
@@ -44,53 +44,13 @@ public:
/// Waits for the thread to finish. Doesn't signalize the ShouldTerminate flag
bool Wait(void);
-
- /// Returns the OS-dependent thread ID for the caller's thread
- static unsigned long GetCurrentID(void);
/** Returns true if the thread calling this function is the thread contained within this object. */
- bool IsCurrentThread(void) const;
+ bool IsCurrentThread(void) const { return std::this_thread::get_id() == m_Thread.get_id(); }
protected:
AString m_ThreadName;
-
- // Value used for "no handle":
- #ifdef _WIN32
- #define NULL_HANDLE NULL
- #else
- #define NULL_HANDLE 0
- #endif
-
- #ifdef _WIN32
-
- DWORD m_ThreadID;
- HANDLE m_Handle;
-
- static DWORD __stdcall thrExecute(LPVOID a_Param)
- {
- // Create a window so that the thread can be identified by 3rd party tools:
- HWND IdentificationWnd = CreateWindowA("STATIC", ((cIsThread *)a_Param)->m_ThreadName.c_str(), 0, 0, 0, 0, WS_OVERLAPPED, NULL, NULL, NULL, NULL);
-
- // Run the thread:
- ((cIsThread *)a_Param)->Execute();
-
- // Destroy the identification window:
- DestroyWindow(IdentificationWnd);
-
- return 0;
- }
-
- #else // _WIN32
-
- pthread_t m_Handle;
-
- static void * thrExecute(void * a_Param)
- {
- ((cIsThread *)a_Param)->Execute();
- return NULL;
- }
-
- #endif // else _WIN32
+ std::thread m_Thread;
} ;
diff --git a/src/OSSupport/Thread.cpp b/src/OSSupport/Thread.cpp
deleted file mode 100644
index faaccce96..000000000
--- a/src/OSSupport/Thread.cpp
+++ /dev/null
@@ -1,137 +0,0 @@
-
-#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
-
-
-
-
-
-// When in MSVC, the debugger provides "thread naming" by catching special exceptions. Interface here:
-#ifdef _MSC_VER
-//
-// Usage: SetThreadName (-1, "MainThread");
-//
-
-// Code adapted from MSDN: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
-
-const DWORD MS_VC_EXCEPTION = 0x406D1388;
-
-#pragma pack(push, 8)
-typedef struct tagTHREADNAME_INFO
-{
- DWORD dwType; // Must be 0x1000.
- LPCSTR szName; // Pointer to name (in user addr space).
- DWORD dwThreadID; // Thread ID (-1 = caller thread).
- DWORD dwFlags; // Reserved for future use, must be zero.
-} THREADNAME_INFO;
-#pragma pack(pop)
-
-static void SetThreadName(DWORD dwThreadID, const char * threadName)
-{
- THREADNAME_INFO info;
- info.dwType = 0x1000;
- info.szName = threadName;
- info.dwThreadID = dwThreadID;
- info.dwFlags = 0;
-
- __try
- {
- RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
- }
- __except (EXCEPTION_EXECUTE_HANDLER)
- {
- }
-}
-#endif // _MSC_VER
-
-
-
-
-
-cThread::cThread( ThreadFunc a_ThreadFunction, void* a_Param, const char* a_ThreadName /* = 0 */)
- : m_ThreadFunction( a_ThreadFunction)
- , m_Param( a_Param)
- , m_Event( new cEvent())
- , m_StopEvent( 0)
-{
- if (a_ThreadName)
- {
- m_ThreadName.assign(a_ThreadName);
- }
-}
-
-
-
-
-
-cThread::~cThread()
-{
- delete m_Event;
- m_Event = NULL;
-
- if (m_StopEvent)
- {
- m_StopEvent->Wait();
- delete m_StopEvent;
- m_StopEvent = NULL;
- }
-}
-
-
-
-
-
-void cThread::Start( bool a_bWaitOnDelete /* = true */)
-{
- if (a_bWaitOnDelete)
- m_StopEvent = new cEvent();
-
-#ifndef _WIN32
- pthread_t SndThread;
- if (pthread_create( &SndThread, NULL, MyThread, this))
- LOGERROR("ERROR: Could not create thread!");
-#else
- DWORD ThreadID = 0;
- HANDLE hThread = CreateThread(NULL // security
- , 0 // stack size
- , (LPTHREAD_START_ROUTINE) MyThread // function name
- , this // parameters
- , 0 // flags
- , &ThreadID); // thread id
- CloseHandle( hThread);
-
- #ifdef _MSC_VER
- if (!m_ThreadName.empty())
- {
- SetThreadName(ThreadID, m_ThreadName.c_str());
- }
- #endif // _MSC_VER
-#endif
-
- // Wait until thread has actually been created
- m_Event->Wait();
-}
-
-
-
-
-
-#ifdef _WIN32
-unsigned long cThread::MyThread(void* a_Param)
-#else
-void *cThread::MyThread( void *a_Param)
-#endif
-{
- cThread* self = (cThread*)a_Param;
- cEvent* StopEvent = self->m_StopEvent;
-
- ThreadFunc* ThreadFunction = self->m_ThreadFunction;
- void* ThreadParam = self->m_Param;
-
- // Set event to let other thread know this thread has been created and it's safe to delete the cThread object
- self->m_Event->Set();
-
- ThreadFunction( ThreadParam);
-
- if (StopEvent) StopEvent->Set();
- return 0;
-}
diff --git a/src/OSSupport/Thread.h b/src/OSSupport/Thread.h
deleted file mode 100644
index 7ee352c82..000000000
--- a/src/OSSupport/Thread.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#pragma once
-
-class cThread
-{
-public:
- typedef void (ThreadFunc)(void*);
- cThread( ThreadFunc a_ThreadFunction, void* a_Param, const char* a_ThreadName = 0);
- ~cThread();
-
- void Start( bool a_bWaitOnDelete = true);
- void WaitForThread();
-private:
- ThreadFunc* m_ThreadFunction;
-
-#ifdef _WIN32
- static unsigned long MyThread(void* a_Param);
-#else
- static void *MyThread( void *lpParam);
-#endif
-
- void* m_Param;
- cEvent* m_Event;
- cEvent* m_StopEvent;
-
- AString m_ThreadName;
-};