2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2020-01-24 13:45:29 +00:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-22 16:47:42 +00:00
|
|
|
#include <LibThreading/Thread.h>
|
2019-11-13 20:49:24 +00:00
|
|
|
#include <pthread.h>
|
2020-12-31 07:57:44 +00:00
|
|
|
#include <string.h>
|
2019-08-25 15:55:56 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-11-12 11:54:53 +00:00
|
|
|
namespace Threading {
|
|
|
|
|
|
|
|
Thread::Thread(Function<intptr_t()> action, StringView thread_name)
|
2023-08-06 06:21:49 +00:00
|
|
|
: m_action(move(action))
|
2022-07-11 17:32:29 +00:00
|
|
|
, m_thread_name(thread_name.is_null() ? ""sv : thread_name)
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-11-12 11:54:53 +00:00
|
|
|
Thread::~Thread()
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
2022-11-12 13:13:40 +00:00
|
|
|
if (needs_to_be_joined()) {
|
|
|
|
dbgln("Destroying {} while it is still running undetached!", *this);
|
LibThread: Improve semantics of Thread::join, and remove Thread::quit.
Thread::quit was created before the pthread_create_helper in pthread.cpp
that automagically calls pthread_exit from all pthreads after the user's
thread function exits. It is unused, and unecessary now.
Cleanup some logging, and make join return a Result<T, ThreadError>.
This also adds a new type, LibThread::ThreadError as an
AK::DistinctNumeric. Hopefully, this will make it possible to have a
Result<int, ThreadError> and have it compile? It also makes it clear
that the int there is an error at the call site.
By default, the T on join is void, meaning the caller doesn't care about
the return value from the thread.
As Result is a [[nodiscard]] type, also change the current caller of
join to explicitly ignore it.
Move the logging out of join as well, as it's the user's
responsibility whether to log or not.
2021-01-01 03:56:04 +00:00
|
|
|
[[maybe_unused]] auto res = join();
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|
2022-11-12 13:13:40 +00:00
|
|
|
if (m_state == ThreadState::Detached)
|
|
|
|
dbgln("Bug! {} in state {} is being destroyed; AK/Function will crash shortly!", *this, m_state.load());
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 11:54:53 +00:00
|
|
|
ErrorOr<void> Thread::set_priority(int priority)
|
2022-11-13 09:23:24 +00:00
|
|
|
{
|
|
|
|
// MacOS has an extra __opaque field, so list initialization will not compile on MacOS Lagom.
|
|
|
|
sched_param scheduling_parameters {};
|
|
|
|
scheduling_parameters.sched_priority = priority;
|
|
|
|
int result = pthread_setschedparam(m_tid, 0, &scheduling_parameters);
|
|
|
|
if (result != 0)
|
|
|
|
return Error::from_errno(result);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-11-12 11:54:53 +00:00
|
|
|
ErrorOr<int> Thread::get_priority() const
|
2022-11-13 09:23:24 +00:00
|
|
|
{
|
|
|
|
sched_param scheduling_parameters {};
|
|
|
|
int policy;
|
|
|
|
int result = pthread_getschedparam(m_tid, &policy, &scheduling_parameters);
|
|
|
|
if (result != 0)
|
|
|
|
return Error::from_errno(result);
|
|
|
|
return scheduling_parameters.sched_priority;
|
|
|
|
}
|
|
|
|
|
2022-11-12 11:57:21 +00:00
|
|
|
DeprecatedString Thread::thread_name() const { return m_thread_name; }
|
|
|
|
|
|
|
|
pthread_t Thread::tid() const { return m_tid; }
|
|
|
|
|
2022-11-12 13:13:40 +00:00
|
|
|
ThreadState Thread::state() const { return m_state; }
|
|
|
|
|
|
|
|
bool Thread::is_started() const { return m_state != ThreadState::Startable; }
|
|
|
|
|
|
|
|
bool Threading::Thread::needs_to_be_joined() const
|
|
|
|
{
|
|
|
|
auto state = m_state.load();
|
|
|
|
return state == ThreadState::Running || state == ThreadState::Exited;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Threading::Thread::has_exited() const
|
|
|
|
{
|
|
|
|
auto state = m_state.load();
|
|
|
|
return state == ThreadState::Joined || state == ThreadState::Exited || state == ThreadState::DetachedExited;
|
|
|
|
}
|
2022-11-12 11:57:21 +00:00
|
|
|
|
2022-11-12 11:54:53 +00:00
|
|
|
void Thread::start()
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
2022-11-12 13:13:40 +00:00
|
|
|
VERIFY(!is_started());
|
|
|
|
|
|
|
|
// Set this first so that the other thread starts out seeing m_state == Running.
|
|
|
|
m_state = Threading::ThreadState::Running;
|
|
|
|
|
2019-11-13 20:49:24 +00:00
|
|
|
int rc = pthread_create(
|
|
|
|
&m_tid,
|
2022-11-12 13:13:40 +00:00
|
|
|
// FIXME: Use pthread_attr_t to start a thread detached if that was requested by the user before the call to start().
|
2019-11-13 20:49:24 +00:00
|
|
|
nullptr,
|
|
|
|
[](void* arg) -> void* {
|
|
|
|
Thread* self = static_cast<Thread*>(arg);
|
2021-04-26 17:09:04 +00:00
|
|
|
auto exit_code = self->m_action();
|
2022-11-12 13:13:40 +00:00
|
|
|
|
|
|
|
auto expected = Threading::ThreadState::Running;
|
|
|
|
// This code might race with a call to detach().
|
|
|
|
if (!self->m_state.compare_exchange_strong(expected, Threading::ThreadState::Exited)) {
|
|
|
|
// If the original state was Detached, we need to set to DetachedExited instead.
|
|
|
|
if (expected == Threading::ThreadState::Detached) {
|
|
|
|
if (!self->m_state.compare_exchange_strong(expected, Threading::ThreadState::DetachedExited)) {
|
|
|
|
dbgln("Thread logic bug: Found thread state {} while trying to set ExitedDetached state!", expected);
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dbgln("Thread logic bug: Found thread state {} while trying to set Exited state!", expected);
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 17:09:04 +00:00
|
|
|
return reinterpret_cast<void*>(exit_code);
|
2019-11-13 20:49:24 +00:00
|
|
|
},
|
|
|
|
static_cast<void*>(this));
|
2019-08-25 15:55:56 +00:00
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2022-10-16 18:26:12 +00:00
|
|
|
#ifdef AK_OS_SERENITY
|
2019-12-07 19:49:05 +00:00
|
|
|
if (!m_thread_name.is_empty()) {
|
2020-01-11 11:54:30 +00:00
|
|
|
rc = pthread_setname_np(m_tid, m_thread_name.characters());
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(rc == 0);
|
2019-12-07 19:49:05 +00:00
|
|
|
}
|
2022-10-16 18:26:12 +00:00
|
|
|
#endif
|
2022-11-12 13:13:40 +00:00
|
|
|
dbgln("Started {}", *this);
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|
2021-07-02 12:05:07 +00:00
|
|
|
|
2022-11-12 11:54:53 +00:00
|
|
|
void Thread::detach()
|
2021-07-02 12:05:07 +00:00
|
|
|
{
|
2022-11-12 13:13:40 +00:00
|
|
|
auto expected = Threading::ThreadState::Running;
|
|
|
|
// This code might race with the other thread exiting.
|
|
|
|
if (!m_state.compare_exchange_strong(expected, Threading::ThreadState::Detached)) {
|
|
|
|
if (expected == Threading::ThreadState::Exited)
|
2023-05-24 10:54:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Always report a precise error before crashing. These kinds of bugs are hard to reproduce.
|
|
|
|
dbgln("Thread logic bug: trying to detach {} in state {}, which is neither Started nor Exited", this, expected);
|
2022-11-12 13:13:40 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2021-07-02 12:05:07 +00:00
|
|
|
|
|
|
|
int rc = pthread_detach(m_tid);
|
|
|
|
VERIFY(rc == 0);
|
|
|
|
}
|
2022-11-12 11:54:53 +00:00
|
|
|
|
|
|
|
}
|