thread_test.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2010-2020 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/thread.h>
  7. bx::DefaultAllocator s_allocator;
  8. bx::MpScUnboundedBlockingQueue<void> s_mpsc(&s_allocator);
  9. int32_t threadExit0(bx::Thread* _thread, void*)
  10. {
  11. _thread->pop();
  12. s_mpsc.push(reinterpret_cast<void*>(uintptr_t(0x1300) ) );
  13. return bx::kExitSuccess;
  14. }
  15. int32_t threadExit1(bx::Thread* _thread, void*)
  16. {
  17. BX_UNUSED(_thread);
  18. s_mpsc.push(reinterpret_cast<void*>(uintptr_t(0x89) ) );
  19. return bx::kExitFailure;
  20. }
  21. TEST_CASE("Semaphore", "")
  22. {
  23. bx::Semaphore sem;
  24. REQUIRE(!sem.wait(10) );
  25. sem.post(1);
  26. REQUIRE(sem.wait() );
  27. }
  28. TEST_CASE("Thread", "")
  29. {
  30. bx::Thread th;
  31. REQUIRE(!th.isRunning() );
  32. th.init(threadExit0);
  33. REQUIRE(th.isRunning() );
  34. th.push(NULL);
  35. th.shutdown();
  36. REQUIRE(!th.isRunning() );
  37. REQUIRE(th.getExitCode() == 0);
  38. th.init(threadExit1);
  39. REQUIRE(th.isRunning() );
  40. th.shutdown();
  41. REQUIRE(!th.isRunning() );
  42. REQUIRE(th.getExitCode() == 1);
  43. }
  44. TEST_CASE("MpScUnboundedBlockingQueue", "")
  45. {
  46. void* p0 = s_mpsc.pop();
  47. void* p1 = s_mpsc.pop();
  48. uintptr_t result = uintptr_t(p0) | uintptr_t(p1);
  49. REQUIRE(result == 0x1389);
  50. }