summaryrefslogtreecommitdiff
path: root/src/jthread/pthread/jthread.cpp
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2013-12-02 22:21:58 +0100
committerPerttu Ahola <celeron55@gmail.com>2013-12-03 17:50:00 +0200
commit5004f31575c52b59e1fc654dfa08336a692afeee (patch)
tree6b44e7a6a964ce372b968feba3688518a0647010 /src/jthread/pthread/jthread.cpp
parent6cbd1b8bf739e0d776ee508708b5076b491fb638 (diff)
downloadminetest-5004f31575c52b59e1fc654dfa08336a692afeee.tar.gz
minetest-5004f31575c52b59e1fc654dfa08336a692afeee.tar.bz2
minetest-5004f31575c52b59e1fc654dfa08336a692afeee.zip
Fix broken async locking in release build
Diffstat (limited to 'src/jthread/pthread/jthread.cpp')
-rw-r--r--src/jthread/pthread/jthread.cpp38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/jthread/pthread/jthread.cpp b/src/jthread/pthread/jthread.cpp
index e7bf17612..d818b19ed 100644
--- a/src/jthread/pthread/jthread.cpp
+++ b/src/jthread/pthread/jthread.cpp
@@ -26,15 +26,19 @@
*/
#include "jthread/jthread.h"
+#include <assert.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
+#define UNUSED(expr) do { (void)(expr); } while (0)
+
JThread::JThread()
{
retval = NULL;
requeststop = false;
running = false;
+ started = false;
}
JThread::~JThread()
@@ -48,6 +52,20 @@ void JThread::Stop() {
runningmutex.Unlock();
}
+void JThread::Wait() {
+ void* status;
+ runningmutex.Lock();
+ if (started) {
+ runningmutex.Unlock();
+ int pthread_join_retval = pthread_join(threadid,&status);
+ assert(pthread_join_retval == 0);
+ UNUSED(pthread_join_retval);
+ runningmutex.Lock();
+ started = false;
+ }
+ runningmutex.Unlock();
+}
+
int JThread::Start()
{
int status;
@@ -63,7 +81,7 @@ int JThread::Start()
pthread_attr_t attr;
pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
+ //pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
continuemutex.Lock();
status = pthread_create(&threadid,&attr,TheThread,this);
@@ -89,6 +107,7 @@ int JThread::Start()
runningmutex.Lock();
}
+ started = true;
runningmutex.Unlock();
continuemutex.Unlock();
@@ -100,13 +119,30 @@ int JThread::Start()
int JThread::Kill()
{
+ void* status;
runningmutex.Lock();
if (!running)
{
+ if (started) {
+ runningmutex.Unlock();
+ int pthread_join_retval = pthread_join(threadid,&status);
+ assert(pthread_join_retval == 0);
+ UNUSED(pthread_join_retval);
+ runningmutex.Lock();
+ started = false;
+ }
runningmutex.Unlock();
return ERR_JTHREAD_NOTRUNNING;
}
pthread_cancel(threadid);
+ if (started) {
+ runningmutex.Unlock();
+ int pthread_join_retval = pthread_join(threadid,&status);
+ assert(pthread_join_retval == 0);
+ UNUSED(pthread_join_retval);
+ runningmutex.Lock();
+ started = false;
+ }
running = false;
runningmutex.Unlock();
return 0;