Thread.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibThreading/Thread.h>
  7. #include <pthread.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. Threading::Thread::Thread(Function<intptr_t()> action, StringView thread_name)
  11. : Core::Object(nullptr)
  12. , m_action(move(action))
  13. , m_thread_name(thread_name.is_null() ? ""sv : thread_name)
  14. {
  15. register_property("thread_name", [&] { return JsonValue { m_thread_name }; });
  16. #if defined(AK_OS_SERENITY) || defined(AK_OS_LINUX)
  17. // FIXME: Print out a pretty TID for BSD and macOS platforms, too
  18. register_property("tid", [&] { return JsonValue { m_tid }; });
  19. #endif
  20. }
  21. Threading::Thread::~Thread()
  22. {
  23. if (m_tid && !m_detached) {
  24. dbgln("Destroying thread \"{}\"({}) while it is still running!", m_thread_name, m_tid);
  25. [[maybe_unused]] auto res = join();
  26. }
  27. }
  28. ErrorOr<void> Threading::Thread::set_priority(int priority)
  29. {
  30. // MacOS has an extra __opaque field, so list initialization will not compile on MacOS Lagom.
  31. sched_param scheduling_parameters {};
  32. scheduling_parameters.sched_priority = priority;
  33. int result = pthread_setschedparam(m_tid, 0, &scheduling_parameters);
  34. if (result != 0)
  35. return Error::from_errno(result);
  36. return {};
  37. }
  38. ErrorOr<int> Threading::Thread::get_priority() const
  39. {
  40. sched_param scheduling_parameters {};
  41. int policy;
  42. int result = pthread_getschedparam(m_tid, &policy, &scheduling_parameters);
  43. if (result != 0)
  44. return Error::from_errno(result);
  45. return scheduling_parameters.sched_priority;
  46. }
  47. void Threading::Thread::start()
  48. {
  49. int rc = pthread_create(
  50. &m_tid,
  51. nullptr,
  52. [](void* arg) -> void* {
  53. Thread* self = static_cast<Thread*>(arg);
  54. auto exit_code = self->m_action();
  55. self->m_tid = 0;
  56. return reinterpret_cast<void*>(exit_code);
  57. },
  58. static_cast<void*>(this));
  59. VERIFY(rc == 0);
  60. #ifdef AK_OS_SERENITY
  61. if (!m_thread_name.is_empty()) {
  62. rc = pthread_setname_np(m_tid, m_thread_name.characters());
  63. VERIFY(rc == 0);
  64. }
  65. #endif
  66. dbgln("Started thread \"{}\", tid = {}", m_thread_name, m_tid);
  67. m_started = true;
  68. }
  69. void Threading::Thread::detach()
  70. {
  71. VERIFY(!m_detached);
  72. int rc = pthread_detach(m_tid);
  73. VERIFY(rc == 0);
  74. m_detached = true;
  75. }