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>
|
|
|
|
|
2021-05-22 16:47:42 +00:00
|
|
|
Threading::Thread::Thread(Function<intptr_t()> action, StringView thread_name)
|
2020-02-02 11:34:39 +00:00
|
|
|
: Core::Object(nullptr)
|
2019-08-25 15:55:56 +00:00
|
|
|
, m_action(move(action))
|
2019-12-07 19:49:05 +00:00
|
|
|
, m_thread_name(thread_name.is_null() ? "" : thread_name)
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
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
|
|
|
register_property("thread_name", [&] { return JsonValue { m_thread_name }; });
|
|
|
|
register_property("tid", [&] { return JsonValue { m_tid }; });
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 16:47:42 +00:00
|
|
|
Threading::Thread::~Thread()
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
2020-01-30 23:46:13 +00:00
|
|
|
if (m_tid) {
|
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
|
|
|
dbgln("Destroying thread \"{}\"({}) while it is still running!", m_thread_name, m_tid);
|
|
|
|
[[maybe_unused]] auto res = join();
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 16:47:42 +00:00
|
|
|
void Threading::Thread::start()
|
2019-08-25 15:55:56 +00:00
|
|
|
{
|
2019-11-13 20:49:24 +00:00
|
|
|
int rc = pthread_create(
|
|
|
|
&m_tid,
|
|
|
|
nullptr,
|
|
|
|
[](void* arg) -> void* {
|
|
|
|
Thread* self = static_cast<Thread*>(arg);
|
2021-04-26 17:09:04 +00:00
|
|
|
auto exit_code = self->m_action();
|
2020-09-18 07:49:51 +00:00
|
|
|
self->m_tid = 0;
|
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);
|
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
|
|
|
}
|
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
|
|
|
dbgln("Started thread \"{}\", tid = {}", m_thread_name, m_tid);
|
2019-08-25 15:55:56 +00:00
|
|
|
}
|