Skip to content

Commit 5004f31

Browse files
sapierceleron55
sapier
authored andcommittedDec 3, 2013
Fix broken async locking in release build
1 parent 6cbd1b8 commit 5004f31

File tree

7 files changed

+102
-42
lines changed

7 files changed

+102
-42
lines changed
 

‎src/jthread/jthread.h

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class JThread
5050
bool StopRequested();
5151
void *GetReturnValue();
5252
bool IsSameThread();
53+
54+
/*
55+
* Wait for thread to finish
56+
* Note: this does not stop a thread you have to do this on your own
57+
* WARNING: never ever call this on a thread not started or already killed!
58+
*/
59+
void Wait();
5360
protected:
5461
void ThreadStarted();
5562
private:
@@ -67,6 +74,8 @@ class JThread
6774
static void *TheThread(void *param);
6875

6976
pthread_t threadid;
77+
78+
bool started;
7079
#endif // WIN32
7180
void *retval;
7281
bool running;

‎src/jthread/pthread/jevent.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,28 @@
2727
#include <assert.h>
2828
#include "jthread/jevent.h"
2929

30+
#define UNUSED(expr) do { (void)(expr); } while (0)
31+
3032
Event::Event() {
31-
assert(sem_init(&sem, 0, 0) == 0);
33+
int sem_init_retval = sem_init(&sem, 0, 0);
34+
assert(sem_init_retval == 0);
35+
UNUSED(sem_init_retval);
3236
}
3337

3438
Event::~Event() {
35-
assert(sem_destroy(&sem) == 0);
39+
int sem_destroy_retval = sem_destroy(&sem);
40+
assert(sem_destroy_retval == 0);
41+
UNUSED(sem_destroy_retval);
3642
}
3743

3844
void Event::wait() {
39-
assert(sem_wait(&sem) == 0);
45+
int sem_wait_retval = sem_wait(&sem);
46+
assert(sem_wait_retval == 0);
47+
UNUSED(sem_wait_retval);
4048
}
4149

4250
void Event::signal() {
43-
assert(sem_post(&sem) == 0);
51+
int sem_post_retval = sem_post(&sem);
52+
assert(sem_post_retval == 0);
53+
UNUSED(sem_post_retval);
4454
}

‎src/jthread/pthread/jmutex.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,33 @@
2626
*/
2727
#include <assert.h>
2828
#include "jthread/jmutex.h"
29-
29+
#define UNUSED(expr) do { (void)(expr); } while (0)
3030
JMutex::JMutex()
3131
{
32-
assert(pthread_mutex_init(&mutex,NULL) == 0);
32+
int mutex_init_retval = pthread_mutex_init(&mutex,NULL);
33+
assert( mutex_init_retval == 0 );
34+
UNUSED(mutex_init_retval);
3335
}
3436

3537
JMutex::~JMutex()
3638
{
37-
assert(pthread_mutex_destroy(&mutex) == 0);
39+
int mutex_dextroy_retval = pthread_mutex_destroy(&mutex);
40+
assert( mutex_dextroy_retval == 0 );
41+
UNUSED(mutex_dextroy_retval);
3842
}
3943

4044
int JMutex::Lock()
4145
{
42-
assert(pthread_mutex_lock(&mutex) == 0);
43-
return 0;
46+
int mutex_lock_retval = pthread_mutex_lock(&mutex);
47+
assert( mutex_lock_retval == 0 );
48+
return mutex_lock_retval;
49+
UNUSED(mutex_lock_retval);
4450
}
4551

4652
int JMutex::Unlock()
4753
{
48-
assert(pthread_mutex_unlock(&mutex) == 0);
49-
return 0;
54+
int mutex_unlock_retval = pthread_mutex_unlock(&mutex);
55+
assert( mutex_unlock_retval == 0 );
56+
return mutex_unlock_retval;
57+
UNUSED(mutex_unlock_retval);
5058
}

‎src/jthread/pthread/jsemaphore.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,35 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818
*/
1919
#include <assert.h>
2020
#include "jthread/jsemaphore.h"
21-
21+
#define UNUSED(expr) do { (void)(expr); } while (0)
2222
JSemaphore::JSemaphore() {
23-
assert(sem_init(&m_semaphore,0,0) == 0);
23+
int sem_init_retval = sem_init(&m_semaphore,0,0);
24+
assert(sem_init_retval == 0);
25+
UNUSED(sem_init_retval);
2426
}
2527

2628
JSemaphore::~JSemaphore() {
27-
assert(sem_destroy(&m_semaphore) == 0);
29+
int sem_destroy_retval = sem_destroy(&m_semaphore);
30+
assert(sem_destroy_retval == 0);
31+
UNUSED(sem_destroy_retval);
2832
}
2933

3034
JSemaphore::JSemaphore(int initval) {
31-
assert(sem_init(&m_semaphore,0,initval) == 0);
35+
int sem_init_retval = sem_init(&m_semaphore,0,initval);
36+
assert(sem_init_retval == 0);
37+
UNUSED(sem_init_retval);
3238
}
3339

3440
void JSemaphore::Post() {
35-
assert(sem_post(&m_semaphore) == 0);
41+
int sem_post_retval = sem_post(&m_semaphore);
42+
assert(sem_post_retval == 0);
43+
UNUSED(sem_post_retval);
3644
}
3745

3846
void JSemaphore::Wait() {
39-
assert(sem_wait(&m_semaphore) == 0);
47+
int sem_wait_retval = sem_wait(&m_semaphore);
48+
assert(sem_wait_retval == 0);
49+
UNUSED(sem_wait_retval);
4050
}
4151

4252
int JSemaphore::GetValue() {

‎src/jthread/pthread/jthread.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@
2626
*/
2727

2828
#include "jthread/jthread.h"
29+
#include <assert.h>
2930
#include <sys/time.h>
3031
#include <time.h>
3132
#include <stdlib.h>
3233

34+
#define UNUSED(expr) do { (void)(expr); } while (0)
35+
3336
JThread::JThread()
3437
{
3538
retval = NULL;
3639
requeststop = false;
3740
running = false;
41+
started = false;
3842
}
3943

4044
JThread::~JThread()
@@ -48,6 +52,20 @@ void JThread::Stop() {
4852
runningmutex.Unlock();
4953
}
5054

55+
void JThread::Wait() {
56+
void* status;
57+
runningmutex.Lock();
58+
if (started) {
59+
runningmutex.Unlock();
60+
int pthread_join_retval = pthread_join(threadid,&status);
61+
assert(pthread_join_retval == 0);
62+
UNUSED(pthread_join_retval);
63+
runningmutex.Lock();
64+
started = false;
65+
}
66+
runningmutex.Unlock();
67+
}
68+
5169
int JThread::Start()
5270
{
5371
int status;
@@ -63,7 +81,7 @@ int JThread::Start()
6381

6482
pthread_attr_t attr;
6583
pthread_attr_init(&attr);
66-
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
84+
//pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
6785

6886
continuemutex.Lock();
6987
status = pthread_create(&threadid,&attr,TheThread,this);
@@ -89,6 +107,7 @@ int JThread::Start()
89107

90108
runningmutex.Lock();
91109
}
110+
started = true;
92111
runningmutex.Unlock();
93112

94113
continuemutex.Unlock();
@@ -100,13 +119,30 @@ int JThread::Start()
100119

101120
int JThread::Kill()
102121
{
122+
void* status;
103123
runningmutex.Lock();
104124
if (!running)
105125
{
126+
if (started) {
127+
runningmutex.Unlock();
128+
int pthread_join_retval = pthread_join(threadid,&status);
129+
assert(pthread_join_retval == 0);
130+
UNUSED(pthread_join_retval);
131+
runningmutex.Lock();
132+
started = false;
133+
}
106134
runningmutex.Unlock();
107135
return ERR_JTHREAD_NOTRUNNING;
108136
}
109137
pthread_cancel(threadid);
138+
if (started) {
139+
runningmutex.Unlock();
140+
int pthread_join_retval = pthread_join(threadid,&status);
141+
assert(pthread_join_retval == 0);
142+
UNUSED(pthread_join_retval);
143+
runningmutex.Lock();
144+
started = false;
145+
}
110146
running = false;
111147
runningmutex.Unlock();
112148
return 0;

‎src/jthread/win32/jthread.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
*/
2727

2828
#include "jthread/jthread.h"
29-
29+
#include <assert.h>
30+
#define UNUSED(expr) do { (void)(expr); } while (0)
3031
#ifndef _WIN32_WCE
3132
#include <process.h>
3233
#endif // _WIN32_WCE
@@ -49,6 +50,12 @@ void JThread::Stop() {
4950
runningmutex.Unlock();
5051
}
5152

53+
void JThread::Wait() {
54+
int WaitForSingleObject_retval = WaitForSingleObject(threadhandle, INFINITE);
55+
assert(WaitForSingleObject_retval == 0);
56+
UNUSED(WaitForSingleObject_retval);
57+
}
58+
5259
int JThread::Start()
5360
{
5461
runningmutex.Lock();

‎src/script/lua_api/l_async_events.h

+3-23
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1717
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
*/
1919

20-
#ifndef C_ASYNC_EVENTS_H_
21-
#define C_ASYNC_EVENTS_H_
20+
#ifndef L_ASYNC_EVENTS_H_
21+
#define L_ASYNC_EVENTS_H_
2222

2323
#include <vector>
2424
#include <map>
2525

26-
#ifndef _WIN32
27-
#include <unistd.h>
28-
#else
29-
#define _WINSOCKAPI_
30-
#include <windows.h>
31-
static unsigned sleep(unsigned seconds) {
32-
Sleep(seconds * 1000);
33-
return 0;
34-
}
35-
#endif
36-
3726
/******************************************************************************/
3827
/* Includes */
3928
/******************************************************************************/
@@ -93,15 +82,6 @@ class AsyncWorkerThread : public JThread {
9382
return worker_thread_wrapper(this);
9483
}
9584

96-
/**
97-
* wait for thread to stop
98-
*/
99-
void Wait() {
100-
while(IsRunning()) {
101-
sleep(1);
102-
}
103-
}
104-
10585
private:
10686
/**
10787
* helper function to run a lua script
@@ -237,4 +217,4 @@ class AsyncEngine {
237217
JSemaphore m_JobQueueCounter;
238218
};
239219

240-
#endif /* C_ASYNC_EVENTS_H_ */
220+
#endif /* L_ASYNC_EVENTS_H_ */

0 commit comments

Comments
 (0)
Please sign in to comment.