mirror of
https://github.com/RGBCube/serenity
synced 2025-05-19 00:45:08 +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.
This commit is contained in:
parent
986544600a
commit
8d0b4657e7
3 changed files with 32 additions and 24 deletions
|
@ -34,13 +34,15 @@ LibThread::Thread::Thread(Function<int()> action, StringView thread_name)
|
|||
, m_action(move(action))
|
||||
, m_thread_name(thread_name.is_null() ? "" : thread_name)
|
||||
{
|
||||
register_property("thread_name", [&] { return JsonValue { m_thread_name }; });
|
||||
register_property("tid", [&] { return JsonValue { m_tid }; });
|
||||
}
|
||||
|
||||
LibThread::Thread::~Thread()
|
||||
{
|
||||
if (m_tid) {
|
||||
dbg() << "trying to destroy a running thread!";
|
||||
join();
|
||||
dbgln("Destroying thread \"{}\"({}) while it is still running!", m_thread_name, m_tid);
|
||||
[[maybe_unused]] auto res = join();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,7 +53,7 @@ void LibThread::Thread::start()
|
|||
nullptr,
|
||||
[](void* arg) -> void* {
|
||||
Thread* self = static_cast<Thread*>(arg);
|
||||
size_t exit_code = self->m_action();
|
||||
int exit_code = self->m_action();
|
||||
self->m_tid = 0;
|
||||
return (void*)exit_code;
|
||||
},
|
||||
|
@ -62,22 +64,5 @@ void LibThread::Thread::start()
|
|||
rc = pthread_setname_np(m_tid, m_thread_name.characters());
|
||||
ASSERT(rc == 0);
|
||||
}
|
||||
dbg() << "Started a thread, tid = " << m_tid;
|
||||
}
|
||||
|
||||
void LibThread::Thread::join()
|
||||
{
|
||||
int rc = pthread_join(m_tid, nullptr);
|
||||
if (rc == 0)
|
||||
m_tid = 0;
|
||||
else
|
||||
warnln("pthread_join: {}", strerror(rc));
|
||||
}
|
||||
|
||||
void LibThread::Thread::quit(void* code)
|
||||
{
|
||||
ASSERT(m_tid == pthread_self());
|
||||
|
||||
m_tid = 0;
|
||||
pthread_exit(code);
|
||||
dbgln("Started thread \"{}\", tid = {}", m_thread_name, m_tid);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue