1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:57:44 +00:00

LibPthread: Start working on a POSIX threading library

This patch adds pthread_create() and pthread_exit(), which currently
simply wrap our existing create_thread() and exit_thread() syscalls.

LibThread is also ported to using LibPthread.
This commit is contained in:
Andreas Kling 2019-11-13 21:49:24 +01:00
parent 4fe2ee0221
commit 69ca9cfd78
16 changed files with 89 additions and 31 deletions

View file

@ -1,5 +1,5 @@
#include <AK/String.h>
#include <AK/ScopedValueRollback.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <Kernel/Syscall.h>
#include <assert.h>
@ -540,13 +540,13 @@ char* getlogin()
return nullptr;
}
int create_thread(int (*entry)(void*), void* argument)
int create_thread(void *(*entry)(void*), void* argument)
{
int rc = syscall(SC_create_thread, entry, argument);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
void exit_thread(int code)
void exit_thread(void* code)
{
syscall(SC_exit_thread, code);
ASSERT_NOT_REACHED();
@ -633,5 +633,4 @@ int get_process_name(char* buffer, int buffer_size)
int rc = syscall(SC_get_process_name, buffer, buffer_size);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

View file

@ -36,8 +36,8 @@ void sysbeep();
int systrace(pid_t);
int gettid();
int donate(int tid);
int create_thread(int (*)(void*), void*);
void exit_thread(int);
int create_thread(void *(*)(void*), void* argument);
void exit_thread(void*);
int create_shared_buffer(int, void** buffer);
int share_buffer_with(int, pid_t peer_pid);
int share_buffer_globally(int);

View file

@ -0,0 +1,19 @@
include ../../Makefile.common
OBJS = pthread.o
LIBRARY = libpthread.a
DEFINES += -DUSERLAND
all: $(LIBRARY)
$(LIBRARY): $(OBJS)
@echo "LIB $@"; $(AR) rcs $@ $(OBJS)
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
-include $(OBJS:%.o=%.d)
clean:
@echo "CLEAN"; rm -f $(LIBRARY) $(OBJS) *.d

View file

@ -0,0 +1,9 @@
#!/bin/sh
set -e
SERENITY_ROOT=../../
mkdir -p $SERENITY_ROOT/Root/usr/include/sys/
mkdir -p $SERENITY_ROOT/Root/usr/lib/
cp ./*.h $SERENITY_ROOT/Root/usr/include/
cp libpthread.a $SERENITY_ROOT/Root/usr/lib/

View file

@ -0,0 +1,24 @@
#include <AK/StdLibExtras.h>
#include <pthread.h>
#include <unistd.h>
extern "C" {
int pthread_create(pthread_t* thread, pthread_attr_t* attributes, void *(*start_routine)(void*), void* argument_to_start_routine)
{
if (!thread)
return -EINVAL;
UNUSED_PARAM(attributes);
int rc = create_thread(start_routine, argument_to_start_routine);
if (rc < 0)
return rc;
*thread = rc;
return 0;
}
void pthread_exit(void* value_ptr)
{
exit_thread(value_ptr);
}
}

View file

@ -5,7 +5,7 @@
__BEGIN_DECLS
typedef void* pthread_t;
typedef int pthread_t;
typedef void* pthread_key_t;
typedef void* pthread_once_t;
typedef void* pthread_mutex_t;
@ -15,7 +15,7 @@ typedef void* pthread_cond_t;
typedef void* pthread_spinlock_t;
typedef void* pthread_condattr_t;
int pthread_create(pthread_t, pthread_attr_t*, void* (*)(void*), void*);
int pthread_create(pthread_t*, pthread_attr_t*, void* (*)(void*), void*);
void pthread_exit(void*);
int pthread_kill(pthread_t, int);
void pthread_cleanup_push(void (*)(void*), void*);
@ -31,7 +31,6 @@ int pthread_attr_destroy(pthread_attr_t*);
int pthread_once(pthread_once_t*, void (*)(void));
#define PTHREAD_ONCE_INIT 0
pthread_once_t once_control = PTHREAD_ONCE_INIT;
void* pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void* value);

View file

@ -1,4 +1,5 @@
#include <LibThread/Thread.h>
#include <pthread.h>
#include <unistd.h>
LibThread::Thread::Thread(Function<int()> action)
@ -17,17 +18,20 @@ LibThread::Thread::~Thread()
void LibThread::Thread::start()
{
int rc = create_thread([](void* arg) {
Thread* self = static_cast<Thread*>(arg);
int exit_code = self->m_action();
self->m_tid = -1;
exit_thread(exit_code);
return exit_code;
}, static_cast<void*>(this));
int rc = pthread_create(
&m_tid,
nullptr,
[](void* arg) -> void* {
Thread* self = static_cast<Thread*>(arg);
int exit_code = self->m_action();
self->m_tid = -1;
pthread_exit((void*)exit_code);
return (void*)exit_code;
},
static_cast<void*>(this));
ASSERT(rc > 0);
dbg() << "Started a thread, tid = " << rc;
m_tid = rc;
ASSERT(rc == 0);
dbg() << "Started a thread, tid = " << m_tid;
}
void LibThread::Thread::quit(int code)
@ -35,5 +39,5 @@ void LibThread::Thread::quit(int code)
ASSERT(m_tid == gettid());
m_tid = -1;
exit_thread(code);
pthread_exit((void*)code);
}