thread_test.cpp 1.3 KB

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