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.
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 testAtomicSemaphoreThread();
37 static TestThreading g_test_instance;
39 void TestThreading::runTests(IGameDef *gamedef)
41 TEST(testStartStopWait);
42 TEST(testAtomicSemaphoreThread);
45 class SimpleTestThread : public Thread {
47 SimpleTestThread(unsigned int interval) :
58 if (isCurrentThread() == false)
59 retval = (void *)0xBAD;
61 while (!stopRequested())
67 unsigned int m_interval;
70 void TestThreading::testStartStopWait()
73 SimpleTestThread *thread = new SimpleTestThread(25);
75 // Try this a couple times, since a Thread should be reusable after waiting
76 for (size_t i = 0; i != 5; i++) {
77 // Can't wait() on a joined, stopped thread
78 UASSERT(thread->wait() == false);
80 // start() should work the first time, but not the second.
81 UASSERT(thread->start() == true);
82 UASSERT(thread->start() == false);
84 UASSERT(thread->isRunning() == true);
85 UASSERT(thread->isCurrentThread() == false);
87 // Let it loop a few times...
90 // It's still running, so the return value shouldn't be available to us.
91 UASSERT(thread->getReturnValue(&thread_retval) == false);
93 // stop() should always succeed
94 UASSERT(thread->stop() == true);
96 // wait() only needs to wait the first time - the other two are no-ops.
97 UASSERT(thread->wait() == true);
98 UASSERT(thread->wait() == false);
99 UASSERT(thread->wait() == false);
101 // Now that the thread is stopped, we should be able to get the
102 // return value, and it should be the object itself.
103 thread_retval = NULL;
104 UASSERT(thread->getReturnValue(&thread_retval) == true);
105 UASSERT(thread_retval == thread);
113 class AtomicTestThread : public Thread {
115 AtomicTestThread(std::atomic<u32> &v, Semaphore &trigger) :
116 Thread("AtomicTest"),
126 for (u32 i = 0; i < 0x10000; ++i)
131 std::atomic<u32> &val;
136 void TestThreading::testAtomicSemaphoreThread()
138 std::atomic<u32> val;
141 static const u8 num_threads = 4;
143 AtomicTestThread *threads[num_threads];
144 for (auto &thread : threads) {
145 thread = new AtomicTestThread(val, trigger);
146 UASSERT(thread->start());
149 trigger.post(num_threads);
151 for (AtomicTestThread *thread : threads) {
156 UASSERT(val == num_threads * 0x10000);