Skip to content

Commit

Permalink
LibThread: Store thread id as pthread_t, use pthread_self()
Browse files Browse the repository at this point in the history
Serenity calls pthread_self() for gettid() anyway but this makes it
portable.
  • Loading branch information
jcs authored and awesomekling committed Feb 5, 2020
1 parent b5fc1fc commit 4826117
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions Libraries/LibThread/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LibThread::Thread::Thread(Function<int()> action, StringView thread_name)

LibThread::Thread::~Thread()
{
if (m_tid != -1) {
if (m_tid) {
dbg() << "trying to destroy a running thread!";
ASSERT_NOT_REACHED();
}
Expand All @@ -50,8 +50,8 @@ void LibThread::Thread::start()
nullptr,
[](void* arg) -> void* {
Thread* self = static_cast<Thread*>(arg);
int exit_code = self->m_action();
self->m_tid = -1;
size_t exit_code = self->m_action();
self->m_tid = 0;
pthread_exit((void*)exit_code);
return (void*)exit_code;
},
Expand All @@ -65,10 +65,10 @@ void LibThread::Thread::start()
dbg() << "Started a thread, tid = " << m_tid;
}

void LibThread::Thread::quit(int code)
void LibThread::Thread::quit(void *code)
{
ASSERT(m_tid == gettid());
ASSERT(m_tid == pthread_self());

m_tid = -1;
pthread_exit((void*)code);
m_tid = 0;
pthread_exit(code);
}
5 changes: 3 additions & 2 deletions Libraries/LibThread/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <AK/Function.h>
#include <AK/String.h>
#include <LibCore/CObject.h>
#include <pthread.h>

namespace LibThread {

Expand All @@ -40,11 +41,11 @@ class Thread final : public Core::Object {
virtual ~Thread();

void start();
void quit(int code = 0);
void quit(void *code = 0);

private:
Function<int()> m_action;
int m_tid { -1 };
pthread_t m_tid;
String m_thread_name;
};

Expand Down

0 comments on commit 4826117

Please sign in to comment.