aboutsummaryrefslogtreecommitdiff
path: root/src/threading
diff options
context:
space:
mode:
Diffstat (limited to 'src/threading')
-rw-r--r--src/threading/event.cpp2
-rw-r--r--src/threading/event.h10
-rw-r--r--src/threading/mutex.cpp9
-rw-r--r--src/threading/mutex.h2
-rw-r--r--src/threading/semaphore.h14
-rw-r--r--src/threading/thread.cpp17
-rw-r--r--src/threading/thread.h1
7 files changed, 42 insertions, 13 deletions
diff --git a/src/threading/event.cpp b/src/threading/event.cpp
index 0d5928f10..a22c6628b 100644
--- a/src/threading/event.cpp
+++ b/src/threading/event.cpp
@@ -35,6 +35,8 @@ Event::Event()
pthread_mutex_init(&mutex, NULL);
notified = false;
# endif
+#elif USE_CPP11_MUTEX
+ notified = false;
#endif
}
diff --git a/src/threading/event.h b/src/threading/event.h
index 26cb8997a..79a99ce1f 100644
--- a/src/threading/event.h
+++ b/src/threading/event.h
@@ -29,19 +29,19 @@ DEALINGS IN THE SOFTWARE.
#include "threads.h"
#if USE_CPP11_MUTEX
- #include <condition_variable>
- #include "threading/mutex.h"
- #include "threading/mutex_auto_lock.h"
+#include <condition_variable>
+#include "threading/mutex.h"
+#include "threading/mutex_auto_lock.h"
#endif
-
/** A syncronization primitive that will wake up one waiting thread when signaled.
* Calling @c signal() multiple times before a waiting thread has had a chance
* to notice the signal will wake only one thread. Additionally, if no threads
* are waiting on the event when it is signaled, the next call to @c wait()
* will return (almost) immediately.
*/
-class Event {
+class Event
+{
public:
Event();
#ifndef USE_CPP11_MUTEX
diff --git a/src/threading/mutex.cpp b/src/threading/mutex.cpp
index 0908b5d37..d864f12c5 100644
--- a/src/threading/mutex.cpp
+++ b/src/threading/mutex.cpp
@@ -88,6 +88,15 @@ void Mutex::lock()
#endif
}
+bool Mutex::try_lock()
+{
+#if USE_WIN_MUTEX
+ return TryEnterCriticalSection(&mutex) != 0;
+#else
+ return pthread_mutex_trylock(&mutex) == 0;
+#endif
+}
+
void Mutex::unlock()
{
#if USE_WIN_MUTEX
diff --git a/src/threading/mutex.h b/src/threading/mutex.h
index fb5c029fc..6feb23c25 100644
--- a/src/threading/mutex.h
+++ b/src/threading/mutex.h
@@ -56,6 +56,8 @@ public:
void lock();
void unlock();
+ bool try_lock();
+
protected:
Mutex(bool recursive);
void init_mutex(bool recursive);
diff --git a/src/threading/semaphore.h b/src/threading/semaphore.h
index 822856396..8ff376666 100644
--- a/src/threading/semaphore.h
+++ b/src/threading/semaphore.h
@@ -21,21 +21,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define THREADING_SEMAPHORE_H
#if defined(_WIN32)
- #include <windows.h>
+#include <windows.h>
#elif defined(__MACH__) && defined(__APPLE__)
- #include <mach/semaphore.h>
+#include <mach/semaphore.h>
#else
- #include <semaphore.h>
+#include <semaphore.h>
#endif
#include "util/basic_macros.h"
-class Semaphore {
+class Semaphore
+{
public:
- Semaphore(int val=0);
+ Semaphore(int val = 0);
~Semaphore();
- void post(unsigned int num=1);
+ void post(unsigned int num = 1);
void wait();
bool wait(unsigned int time_ms);
@@ -52,4 +53,3 @@ private:
};
#endif
-
diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp
index fbe4ba1f3..1909da61d 100644
--- a/src/threading/thread.cpp
+++ b/src/threading/thread.cpp
@@ -101,6 +101,11 @@ Thread::Thread(const std::string &name) :
Thread::~Thread()
{
kill();
+
+ // Make sure start finished mutex is unlocked before it's destroyed
+ m_start_finished_mutex.try_lock();
+ m_start_finished_mutex.unlock();
+
}
@@ -113,6 +118,9 @@ bool Thread::start()
m_request_stop = false;
+ // The mutex may already be locked if the thread is being restarted
+ m_start_finished_mutex.try_lock();
+
#if USE_CPP11_THREADS
try {
@@ -135,6 +143,9 @@ bool Thread::start()
#endif
+ // Allow spawned thread to continue
+ m_start_finished_mutex.unlock();
+
while (!m_running)
sleep_ms(1);
@@ -241,7 +252,7 @@ DWORD WINAPI Thread::threadProc(LPVOID param)
Thread *thr = (Thread *)param;
#ifdef _AIX
- m_kernel_thread_id = thread_self();
+ thr->m_kernel_thread_id = thread_self();
#endif
thr->setName(thr->m_name);
@@ -249,6 +260,10 @@ DWORD WINAPI Thread::threadProc(LPVOID param)
g_logger.registerThread(thr->m_name);
thr->m_running = true;
+ // Wait for the thread that started this one to finish initializing the
+ // thread handle so that getThreadId/getThreadHandle will work.
+ thr->m_start_finished_mutex.lock();
+
thr->m_retval = thr->run();
thr->m_running = false;
diff --git a/src/threading/thread.h b/src/threading/thread.h
index 14a0e13ab..4785d3e03 100644
--- a/src/threading/thread.h
+++ b/src/threading/thread.h
@@ -153,6 +153,7 @@ private:
Atomic<bool> m_request_stop;
Atomic<bool> m_running;
Mutex m_mutex;
+ Mutex m_start_finished_mutex;
#if USE_CPP11_THREADS
std::thread *m_thread_obj;