1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +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:
Andrew Kaster 2020-12-31 20:56:04 -07:00 committed by Andreas Kling
parent 986544600a
commit 8d0b4657e7
3 changed files with 32 additions and 24 deletions

View file

@ -26,13 +26,17 @@
#pragma once
#include <AK/DistinctNumeric.h>
#include <AK/Function.h>
#include <AK/Result.h>
#include <AK/String.h>
#include <LibCore/Object.h>
#include <pthread.h>
namespace LibThread {
TYPEDEF_DISTINCT_ORDERED_ID(int, ThreadError);
class Thread final : public Core::Object {
C_OBJECT(Thread);
@ -40,8 +44,11 @@ public:
virtual ~Thread();
void start();
void join();
void quit(void* code = 0);
template<typename T = void>
Result<T, ThreadError> join();
String thread_name() const { return m_thread_name; }
pthread_t tid() const { return m_tid; }
private:
@ -51,4 +58,20 @@ private:
String m_thread_name;
};
template<typename T>
Result<T, ThreadError> Thread::join()
{
void* thread_return = nullptr;
int rc = pthread_join(m_tid, &thread_return);
if (rc != 0) {
return ThreadError { rc };
}
m_tid = 0;
if constexpr (IsVoid<T>::value)
return {};
else
return { static_cast<T>(thread_return) };
}
}