TestThread.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2021, Spencer Dixon <spencercdixon@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Time.h>
  7. #include <LibTest/TestCase.h>
  8. #include <LibThreading/Thread.h>
  9. #include <unistd.h>
  10. using namespace AK::TimeLiterals;
  11. static void sleep_until_thread_exits(Threading::Thread const& thread)
  12. {
  13. static constexpr auto delay = 20_ms;
  14. for (auto i = 0; i < 100; ++i) {
  15. if (thread.has_exited())
  16. return;
  17. usleep(delay.to_microseconds());
  18. }
  19. FAIL("Timed out waiting for thread to exit");
  20. }
  21. TEST_CASE(threads_can_detach)
  22. {
  23. IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<int> should_be_42 = 0;
  24. auto thread = Threading::Thread::construct([&should_be_42]() {
  25. usleep(10 * 1000);
  26. should_be_42 = 42;
  27. return 0;
  28. });
  29. thread->start();
  30. thread->detach();
  31. sleep_until_thread_exits(*thread);
  32. EXPECT(should_be_42 == 42);
  33. }
  34. TEST_CASE(detached_threads_do_not_need_to_be_joined)
  35. {
  36. IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> should_exit { false };
  37. auto thread = Threading::Thread::construct([&]() {
  38. while (!should_exit.load())
  39. usleep(10 * 1000);
  40. return 0;
  41. });
  42. thread->start();
  43. thread->detach();
  44. // Because of how the crash test forks and removes the thread, we can't use that to verify that join() crashes. Instead, we check the join crash condition ourselves.
  45. EXPECT(!thread->needs_to_be_joined());
  46. // FIXME: Dropping a running thread crashes because of the Function destructor. For now, force the detached thread to exit.
  47. should_exit.store(true);
  48. sleep_until_thread_exits(*thread);
  49. }
  50. TEST_CASE(join_dead_thread)
  51. {
  52. auto thread = Threading::Thread::construct([&]() { return 0 /*nullptr*/; });
  53. thread->start();
  54. // The thread should have exited by then.
  55. sleep_until_thread_exits(*thread);
  56. auto join_result = TRY_OR_FAIL(thread->join<int*>());
  57. EXPECT_EQ(join_result, static_cast<int*>(0));
  58. }