Skip to content

Commit 6c5ba5a

Browse files
committed
Fixed some possible race conditions
When starting the same thread from two different threads, problems could occur. When calling Kill and Start at the same time, a deadlock could occur.
1 parent c2a5f42 commit 6c5ba5a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
---------
77

8+
1.3.3 (?)
9+
* Fixed race conditions when starting the same thread from
10+
two different threads, or when starting and killing a
11+
thread at the same time. Thanks to Andreas Ringlstetter for
12+
providing the patch.
813

914
1.3.2 (March 2017)
1015
* CMake tests if pthread_cancel exists (doesn't on Android)

src/pthread/jthread.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ int JThread::Start()
6969
mutexinit = true;
7070
}
7171

72+
continuemutex.Lock();
7273
runningmutex.Lock();
7374
if (running)
7475
{
7576
runningmutex.Unlock();
77+
continuemutex.Unlock();
7678
return ERR_JTHREAD_ALREADYRUNNING;
7779
}
7880
runningmutex.Unlock();
@@ -81,7 +83,6 @@ int JThread::Start()
8183
pthread_attr_init(&attr);
8284
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
8385

84-
continuemutex.Lock();
8586
status = pthread_create(&threadid,&attr,TheThread,this);
8687
pthread_attr_destroy(&attr);
8788
if (status != 0)
@@ -116,17 +117,20 @@ int JThread::Start()
116117

117118
int JThread::Kill()
118119
{
120+
continuemutex.Lock();
119121
runningmutex.Lock();
120122
if (!running)
121123
{
122124
runningmutex.Unlock();
125+
continuemutex.Unlock();
123126
return ERR_JTHREAD_NOTRUNNING;
124127
}
125128
#ifndef JTHREAD_SKIP_PTHREAD_CANCEL
126129
pthread_cancel(threadid);
127130
#endif // JTHREAD_SKIP_PTHREAD_CANCEL
128131
running = false;
129132
runningmutex.Unlock();
133+
continuemutex.Unlock();
130134
return 0;
131135
}
132136

src/win32/jthread.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ JThread::JThread()
4040
retval = NULL;
4141
mutexinit = false;
4242
running = false;
43+
threadid = 0;
44+
threadhandle = 0;
4345
}
4446

4547
JThread::~JThread()
@@ -68,15 +70,16 @@ int JThread::Start()
6870
} mutexinit = true;
6971
}
7072

73+
continuemutex.Lock();
7174
runningmutex.Lock();
7275
if (running)
7376
{
7477
runningmutex.Unlock();
78+
continuemutex.Unlock();
7579
return ERR_JTHREAD_ALREADYRUNNING;
7680
}
7781
runningmutex.Unlock();
7882

79-
continuemutex.Lock();
8083
#ifndef _WIN32_WCE
8184
threadhandle = (HANDLE)_beginthreadex(NULL,0,TheThread,this,0,&threadid);
8285
#else
@@ -109,16 +112,20 @@ int JThread::Start()
109112

110113
int JThread::Kill()
111114
{
115+
continuemutex.Lock();
112116
runningmutex.Lock();
113117
if (!running)
114118
{
115119
runningmutex.Unlock();
120+
continuemutex.Unlock();
116121
return ERR_JTHREAD_NOTRUNNING;
117122
}
118123
TerminateThread(threadhandle,0);
119124
CloseHandle(threadhandle);
125+
threadhandle = 0;
120126
running = false;
121127
runningmutex.Unlock();
128+
continuemutex.Unlock();
122129
return 0;
123130
}
124131

@@ -174,6 +181,7 @@ DWORD WINAPI JThread::TheThread(void *param)
174181
jthread->running = false;
175182
jthread->retval = ret;
176183
CloseHandle(jthread->threadhandle);
184+
jthread->threadhandle = 0;
177185
jthread->runningmutex.Unlock();
178186
return 0;
179187
}

0 commit comments

Comments
 (0)