aboutsummaryrefslogtreecommitdiff
path: root/src/jthread/pthread
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2014-01-31 22:16:47 +0100
committersapier <Sapier at GMX dot net>2014-04-19 10:05:23 +0200
commitea0df3e4cb75a7a104a81e050c019049219c4fee (patch)
tree02ad409cc7e5b988b2b6093868723b39f9b61dba /src/jthread/pthread
parentc00ed9dac3e3bed36ea06d80fc9856927f1cca95 (diff)
downloadminetest-ea0df3e4cb75a7a104a81e050c019049219c4fee.tar.gz
minetest-ea0df3e4cb75a7a104a81e050c019049219c4fee.tar.bz2
minetest-ea0df3e4cb75a7a104a81e050c019049219c4fee.zip
jthread remove locks that aren't absolutely required
add c++11 atomic support (optional)
Diffstat (limited to 'src/jthread/pthread')
-rw-r--r--src/jthread/pthread/jthread.cpp58
1 files changed, 4 insertions, 54 deletions
diff --git a/src/jthread/pthread/jthread.cpp b/src/jthread/pthread/jthread.cpp
index d818b19ed..a8e54e315 100644
--- a/src/jthread/pthread/jthread.cpp
+++ b/src/jthread/pthread/jthread.cpp
@@ -46,38 +46,25 @@ JThread::~JThread()
Kill();
}
-void JThread::Stop() {
- runningmutex.Lock();
- requeststop = true;
- 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;
- runningmutex.Lock();
if (running)
{
- runningmutex.Unlock();
return ERR_JTHREAD_ALREADYRUNNING;
}
requeststop = false;
- runningmutex.Unlock();
pthread_attr_t attr;
pthread_attr_init(&attr);
@@ -94,21 +81,15 @@ int JThread::Start()
/* Wait until 'running' is set */
- runningmutex.Lock();
while (!running)
{
- runningmutex.Unlock();
-
struct timespec req,rem;
req.tv_sec = 0;
req.tv_nsec = 1000000;
nanosleep(&req,&rem);
-
- runningmutex.Lock();
}
started = true;
- runningmutex.Unlock();
continuemutex.Unlock();
@@ -120,63 +101,37 @@ 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;
}
-bool JThread::IsRunning()
-{
- bool r;
-
- runningmutex.Lock();
- r = running;
- runningmutex.Unlock();
- return r;
-}
-
-bool JThread::StopRequested() {
- bool r;
-
- runningmutex.Lock();
- r = requeststop;
- runningmutex.Unlock();
- return r;
-}
-
void *JThread::GetReturnValue()
{
void *val;
- runningmutex.Lock();
- if (running)
+ if (running) {
val = NULL;
- else
+ } else {
val = retval;
- runningmutex.Unlock();
+ }
+
return val;
}
@@ -193,19 +148,14 @@ void *JThread::TheThread(void *param)
jthread = (JThread *)param;
jthread->continuemutex2.Lock();
- jthread->runningmutex.Lock();
jthread->running = true;
- jthread->runningmutex.Unlock();
jthread->continuemutex.Lock();
jthread->continuemutex.Unlock();
ret = jthread->Thread();
- jthread->runningmutex.Lock();
jthread->running = false;
- jthread->retval = ret;
- jthread->runningmutex.Unlock();
return NULL;
}