thread_test.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. bool init = th.init(threadExit0, NULL, 0, NULL);
  33. REQUIRE(init);
  34. REQUIRE(th.isRunning() );
  35. th.push(NULL);
  36. th.shutdown();
  37. REQUIRE(!th.isRunning() );
  38. REQUIRE(th.getExitCode() == 0);
  39. th.init(threadExit1);
  40. REQUIRE(th.isRunning() );
  41. th.shutdown();
  42. REQUIRE(!th.isRunning() );
  43. REQUIRE(th.getExitCode() == 1);
  44. }
  45. TEST_CASE("MpScUnboundedBlockingQueue", "")
  46. {
  47. void* p0 = s_mpsc.pop();
  48. void* p1 = s_mpsc.pop();
  49. uintptr_t result = uintptr_t(p0) | uintptr_t(p1);
  50. REQUIRE(result == 0x1389);
  51. }