Skip to content

Commit 3fb1f45

Browse files
authoredSep 10, 2020
Remove Thread::kill() and related unittest (#10317)
Closes: #6065
1 parent 0683bea commit 3fb1f45

File tree

3 files changed

+22
-65
lines changed

3 files changed

+22
-65
lines changed
 

‎src/threading/thread.cpp

+22-32
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,28 @@ Thread::Thread(const std::string &name) :
7373

7474
Thread::~Thread()
7575
{
76-
kill();
76+
// kill the thread if running
77+
if (!m_running) {
78+
wait();
79+
} else {
80+
81+
m_running = false;
82+
83+
#if defined(_WIN32)
84+
// See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method
85+
TerminateThread((HANDLE) m_thread_obj->native_handle(), 0);
86+
CloseHandle((HANDLE) m_thread_obj->native_handle());
87+
#else
88+
// We need to pthread_kill instead on Android since NDKv5's pthread
89+
// implementation is incomplete.
90+
# ifdef __ANDROID__
91+
pthread_kill(getThreadHandle(), SIGKILL);
92+
# else
93+
pthread_cancel(getThreadHandle());
94+
# endif
95+
wait();
96+
#endif
97+
}
7798

7899
// Make sure start finished mutex is unlocked before it's destroyed
79100
if (m_start_finished_mutex.try_lock())
@@ -138,37 +159,6 @@ bool Thread::wait()
138159
}
139160

140161

141-
bool Thread::kill()
142-
{
143-
if (!m_running) {
144-
wait();
145-
return false;
146-
}
147-
148-
m_running = false;
149-
150-
#if defined(_WIN32)
151-
// See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method
152-
TerminateThread((HANDLE) m_thread_obj->native_handle(), 0);
153-
CloseHandle((HANDLE) m_thread_obj->native_handle());
154-
#else
155-
// We need to pthread_kill instead on Android since NDKv5's pthread
156-
// implementation is incomplete.
157-
# ifdef __ANDROID__
158-
pthread_kill(getThreadHandle(), SIGKILL);
159-
# else
160-
pthread_cancel(getThreadHandle());
161-
# endif
162-
wait();
163-
#endif
164-
165-
m_retval = nullptr;
166-
m_joinable = false;
167-
m_request_stop = false;
168-
169-
return true;
170-
}
171-
172162

173163
bool Thread::getReturnValue(void **ret)
174164
{

‎src/threading/thread.h

-8
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ class Thread {
7474
*/
7575
bool stop();
7676

77-
/*
78-
* Immediately terminates the thread.
79-
* This should be used with extreme caution, as the thread will not have
80-
* any opportunity to release resources it may be holding (such as memory
81-
* or locks).
82-
*/
83-
bool kill();
84-
8577
/*
8678
* Waits for thread to finish.
8779
* Note: This does not stop a thread, you have to do this on your own.

‎src/unittest/test_threading.cpp

-25
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class TestThreading : public TestBase {
3131
void runTests(IGameDef *gamedef);
3232

3333
void testStartStopWait();
34-
void testThreadKill();
3534
void testAtomicSemaphoreThread();
3635
};
3736

@@ -40,7 +39,6 @@ static TestThreading g_test_instance;
4039
void TestThreading::runTests(IGameDef *gamedef)
4140
{
4241
TEST(testStartStopWait);
43-
TEST(testThreadKill);
4442
TEST(testAtomicSemaphoreThread);
4543
}
4644

@@ -111,29 +109,6 @@ void TestThreading::testStartStopWait()
111109
}
112110

113111

114-
void TestThreading::testThreadKill()
115-
{
116-
SimpleTestThread *thread = new SimpleTestThread(300);
117-
118-
UASSERT(thread->start() == true);
119-
120-
// kill()ing is quite violent, so let's make sure our victim is sleeping
121-
// before we do this... so we don't corrupt the rest of the program's state
122-
sleep_ms(100);
123-
UASSERT(thread->kill() == true);
124-
125-
// The state of the thread object should be reset if all went well
126-
UASSERT(thread->isRunning() == false);
127-
UASSERT(thread->start() == true);
128-
UASSERT(thread->stop() == true);
129-
UASSERT(thread->wait() == true);
130-
131-
// kill() after already waiting should fail.
132-
UASSERT(thread->kill() == false);
133-
134-
delete thread;
135-
}
136-
137112

138113
class AtomicTestThread : public Thread {
139114
public:

0 commit comments

Comments
 (0)
Please sign in to comment.