]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_threading.cpp
Change typedef to normal definitions in GUI code
[dragonfireclient.git] / src / unittest / test_threading.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include <atomic>
23 #include "threading/semaphore.h"
24 #include "threading/thread.h"
25
26
27 class TestThreading : public TestBase {
28 public:
29         TestThreading() { TestManager::registerTestModule(this); }
30         const char *getName() { return "TestThreading"; }
31         void runTests(IGameDef *gamedef);
32
33         void testStartStopWait();
34         void testAtomicSemaphoreThread();
35 };
36
37 static TestThreading g_test_instance;
38
39 void TestThreading::runTests(IGameDef *gamedef)
40 {
41         TEST(testStartStopWait);
42         TEST(testAtomicSemaphoreThread);
43 }
44
45 class SimpleTestThread : public Thread {
46 public:
47         SimpleTestThread(unsigned int interval) :
48                 Thread("SimpleTest"),
49                 m_interval(interval)
50         {
51         }
52
53 private:
54         void *run()
55         {
56                 void *retval = this;
57
58                 if (isCurrentThread() == false)
59                         retval = (void *)0xBAD;
60
61                 while (!stopRequested())
62                         sleep_ms(m_interval);
63
64                 return retval;
65         }
66
67         unsigned int m_interval;
68 };
69
70 void TestThreading::testStartStopWait()
71 {
72         void *thread_retval;
73         SimpleTestThread *thread = new SimpleTestThread(25);
74
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);
79
80                 // start() should work the first time, but not the second.
81                 UASSERT(thread->start() == true);
82                 UASSERT(thread->start() == false);
83
84                 UASSERT(thread->isRunning() == true);
85                 UASSERT(thread->isCurrentThread() == false);
86
87                 // Let it loop a few times...
88                 sleep_ms(70);
89
90                 // It's still running, so the return value shouldn't be available to us.
91                 UASSERT(thread->getReturnValue(&thread_retval) == false);
92
93                 // stop() should always succeed
94                 UASSERT(thread->stop() == true);
95
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);
100
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);
106         }
107
108         delete thread;
109 }
110
111
112
113 class AtomicTestThread : public Thread {
114 public:
115         AtomicTestThread(std::atomic<u32> &v, Semaphore &trigger) :
116                 Thread("AtomicTest"),
117                 val(v),
118                 trigger(trigger)
119         {
120         }
121
122 private:
123         void *run()
124         {
125                 trigger.wait();
126                 for (u32 i = 0; i < 0x10000; ++i)
127                         ++val;
128                 return NULL;
129         }
130
131         std::atomic<u32> &val;
132         Semaphore &trigger;
133 };
134
135
136 void TestThreading::testAtomicSemaphoreThread()
137 {
138         std::atomic<u32> val;
139         val = 0;
140         Semaphore trigger;
141         static const u8 num_threads = 4;
142
143         AtomicTestThread *threads[num_threads];
144         for (auto &thread : threads) {
145                 thread = new AtomicTestThread(val, trigger);
146                 UASSERT(thread->start());
147         }
148
149         trigger.post(num_threads);
150
151         for (AtomicTestThread *thread : threads) {
152                 thread->wait();
153                 delete thread;
154         }
155
156         UASSERT(val == num_threads * 0x10000);
157 }
158