test_threading.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "test.h"
  17. #include <atomic>
  18. #include "threading/semaphore.h"
  19. #include "threading/thread.h"
  20. class TestThreading : public TestBase {
  21. public:
  22. TestThreading() { TestManager::registerTestModule(this); }
  23. const char *getName() { return "TestThreading"; }
  24. void runTests(IGameDef *gamedef);
  25. void testStartStopWait();
  26. void testAtomicSemaphoreThread();
  27. };
  28. static TestThreading g_test_instance;
  29. void TestThreading::runTests(IGameDef *gamedef)
  30. {
  31. TEST(testStartStopWait);
  32. TEST(testAtomicSemaphoreThread);
  33. }
  34. class SimpleTestThread : public Thread {
  35. public:
  36. SimpleTestThread(unsigned int interval) :
  37. Thread("SimpleTest"),
  38. m_interval(interval)
  39. {
  40. }
  41. private:
  42. void *run()
  43. {
  44. void *retval = this;
  45. if (isCurrentThread() == false)
  46. retval = (void *)0xBAD;
  47. while (!stopRequested())
  48. sleep_ms(m_interval);
  49. return retval;
  50. }
  51. unsigned int m_interval;
  52. };
  53. void TestThreading::testStartStopWait()
  54. {
  55. void *thread_retval;
  56. SimpleTestThread *thread = new SimpleTestThread(25);
  57. // Try this a couple times, since a Thread should be reusable after waiting
  58. for (size_t i = 0; i != 5; i++) {
  59. // Can't wait() on a joined, stopped thread
  60. UASSERT(thread->wait() == false);
  61. // start() should work the first time, but not the second.
  62. UASSERT(thread->start() == true);
  63. UASSERT(thread->start() == false);
  64. UASSERT(thread->isRunning() == true);
  65. UASSERT(thread->isCurrentThread() == false);
  66. // Let it loop a few times...
  67. sleep_ms(70);
  68. // It's still running, so the return value shouldn't be available to us.
  69. UASSERT(thread->getReturnValue(&thread_retval) == false);
  70. // stop() should always succeed
  71. UASSERT(thread->stop() == true);
  72. // wait() only needs to wait the first time - the other two are no-ops.
  73. UASSERT(thread->wait() == true);
  74. UASSERT(thread->wait() == false);
  75. UASSERT(thread->wait() == false);
  76. // Now that the thread is stopped, we should be able to get the
  77. // return value, and it should be the object itself.
  78. thread_retval = NULL;
  79. UASSERT(thread->getReturnValue(&thread_retval) == true);
  80. UASSERT(thread_retval == thread);
  81. }
  82. delete thread;
  83. }
  84. class AtomicTestThread : public Thread {
  85. public:
  86. AtomicTestThread(std::atomic<u32> &v, Semaphore &trigger) :
  87. Thread("AtomicTest"),
  88. val(v),
  89. trigger(trigger)
  90. {
  91. }
  92. private:
  93. void *run()
  94. {
  95. trigger.wait();
  96. for (u32 i = 0; i < 0x10000; ++i)
  97. ++val;
  98. return NULL;
  99. }
  100. std::atomic<u32> &val;
  101. Semaphore &trigger;
  102. };
  103. void TestThreading::testAtomicSemaphoreThread()
  104. {
  105. std::atomic<u32> val;
  106. val = 0;
  107. Semaphore trigger;
  108. static const u8 num_threads = 4;
  109. AtomicTestThread *threads[num_threads];
  110. for (auto &thread : threads) {
  111. thread = new AtomicTestThread(val, trigger);
  112. UASSERT(thread->start());
  113. }
  114. trigger.post(num_threads);
  115. for (AtomicTestThread *thread : threads) {
  116. thread->wait();
  117. delete thread;
  118. }
  119. UASSERT(val == num_threads * 0x10000);
  120. }