3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "threading/atomic.h"
23 #include "threading/semaphore.h"
24 #include "threading/thread.h"
27 class TestThreading : public TestBase {
29 TestThreading() { TestManager::registerTestModule(this); }
30 const char *getName() { return "TestThreading"; }
31 void runTests(IGameDef *gamedef);
33 void testStartStopWait();
34 void testThreadKill();
35 void testAtomicSemaphoreThread();
38 static TestThreading g_test_instance;
40 void TestThreading::runTests(IGameDef *gamedef)
42 TEST(testStartStopWait);
44 TEST(testAtomicSemaphoreThread);
47 class SimpleTestThread : public Thread {
49 SimpleTestThread(unsigned int interval) :
60 if (isCurrentThread() == false)
61 retval = (void *)0xBAD;
63 while (!stopRequested())
69 unsigned int m_interval;
72 void TestThreading::testStartStopWait()
75 SimpleTestThread *thread = new SimpleTestThread(25);
77 // Try this a couple times, since a Thread should be reusable after waiting
78 for (size_t i = 0; i != 5; i++) {
79 // Can't wait() on a joined, stopped thread
80 UASSERT(thread->wait() == false);
82 // start() should work the first time, but not the second.
83 UASSERT(thread->start() == true);
84 UASSERT(thread->start() == false);
86 UASSERT(thread->isRunning() == true);
87 UASSERT(thread->isCurrentThread() == false);
89 // Let it loop a few times...
92 // It's still running, so the return value shouldn't be available to us.
93 UASSERT(thread->getReturnValue(&thread_retval) == false);
95 // stop() should always succeed
96 UASSERT(thread->stop() == true);
98 // wait() only needs to wait the first time - the other two are no-ops.
99 UASSERT(thread->wait() == true);
100 UASSERT(thread->wait() == false);
101 UASSERT(thread->wait() == false);
103 // Now that the thread is stopped, we should be able to get the
104 // return value, and it should be the object itself.
105 thread_retval = NULL;
106 UASSERT(thread->getReturnValue(&thread_retval) == true);
107 UASSERT(thread_retval == thread);
114 void TestThreading::testThreadKill()
116 SimpleTestThread *thread = new SimpleTestThread(300);
118 UASSERT(thread->start() == true);
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
123 UASSERT(thread->kill() == true);
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);
131 // kill() after already waiting should fail.
132 UASSERT(thread->kill() == false);
138 class AtomicTestThread : public Thread {
140 AtomicTestThread(Atomic<u32> &v, Semaphore &trigger) :
141 Thread("AtomicTest"),
151 for (u32 i = 0; i < 0x10000; ++i)
161 void TestThreading::testAtomicSemaphoreThread()
166 static const u8 num_threads = 4;
168 AtomicTestThread *threads[num_threads];
169 for (u8 i = 0; i < num_threads; ++i) {
170 threads[i] = new AtomicTestThread(val, trigger);
171 UASSERT(threads[i]->start());
174 trigger.post(num_threads);
176 for (u8 i = 0; i < num_threads; ++i) {
181 UASSERT(val == num_threads * 0x10000);