Kenrel: Separate pthread API from kernel API

It didn't really make sense for the syscalls to be named as the pthread
equivalents. Now the kernel just uses thread ids
This commit is contained in:
2026-07-02 19:11:49 +03:00
parent 5540bc4147
commit f449ca8161
10 changed files with 58 additions and 62 deletions

View File

@@ -209,12 +209,12 @@ namespace Kernel
BAN::ErrorOr<long> sys_set_gsbase(void*);
BAN::ErrorOr<long> sys_get_gsbase();
BAN::ErrorOr<long> sys_pthread_create(const pthread_attr_t* attr, void (*entry)(void*), void* arg);
BAN::ErrorOr<long> sys_pthread_exit(void* value);
BAN::ErrorOr<long> sys_pthread_join(pthread_t thread, void** value);
BAN::ErrorOr<long> sys_pthread_self();
BAN::ErrorOr<long> sys_pthread_kill(pthread_t thread, int signal);
BAN::ErrorOr<long> sys_pthread_detach(pthread_t thread);
BAN::ErrorOr<long> sys_thread_create(void (*entry)(void*), void* arg);
BAN::ErrorOr<long> sys_thread_exit(void* value);
BAN::ErrorOr<long> sys_thread_join(pid_t tid, void** value);
BAN::ErrorOr<long> sys_thread_getid();
BAN::ErrorOr<long> sys_thread_kill(pid_t tid, int signal);
BAN::ErrorOr<long> sys_thread_detach(pid_t tid);
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*);
@@ -364,13 +364,13 @@ namespace Kernel
BAN::Vector<Thread*> m_threads;
struct pthread_info_t
struct exited_thread_info_t
{
pthread_t thread;
pid_t tid;
void* value;
};
BAN::Vector<pthread_info_t> m_exited_pthreads;
ThreadBlocker m_pthread_exit_blocker;
BAN::Vector<exited_thread_info_t> m_exited_threads;
ThreadBlocker m_thread_exit_blocker;
uint64_t m_alarm_interval_ns { 0 };
uint64_t m_alarm_wake_time_ns { 0 };

View File

@@ -50,7 +50,7 @@ namespace Kernel
static BAN::ErrorOr<Thread*> create_userspace(Process*, PageTable&);
~Thread();
BAN::ErrorOr<Thread*> pthread_create(entry_t, void*);
BAN::ErrorOr<Thread*> thread_create(entry_t, void*);
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t sp, uintptr_t ip);
void setup_process_cleanup();

View File

@@ -8,7 +8,6 @@
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>
namespace Kernel::ELF
{

View File

@@ -28,7 +28,6 @@
#include <LibInput/KeyboardLayout.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/banan-os.h>
#include <sys/eventfd.h>
@@ -245,7 +244,7 @@ namespace Kernel
Process::~Process()
{
ASSERT(m_threads.empty());
ASSERT(m_exited_pthreads.empty());
ASSERT(m_exited_threads.empty());
ASSERT(m_mapped_regions.empty());
ASSERT(!m_page_table);
}
@@ -293,7 +292,7 @@ namespace Kernel
}
}
m_exited_pthreads.clear();
m_exited_threads.clear();
ProcFileSystem::get().on_process_delete(*this);
@@ -3412,41 +3411,39 @@ namespace Kernel
return Thread::current().get_gsbase();
}
BAN::ErrorOr<long> Process::sys_pthread_create(const pthread_attr_t* user_attr, void (*entry)(void*), void* arg)
BAN::ErrorOr<long> Process::sys_thread_create(void (*entry)(void*), void* arg)
{
if (user_attr != nullptr)
dwarnln("TODO: ignoring thread attr");
LockGuard _(m_process_lock);
auto* new_thread = TRY(Thread::current().pthread_create(entry, arg));
auto* new_thread = TRY(Thread::current().thread_create(entry, arg));
MUST(m_threads.push_back(new_thread));
MUST(Processor::scheduler().add_thread(new_thread));
return new_thread->tid();
}
BAN::ErrorOr<long> Process::sys_pthread_exit(void* value)
BAN::ErrorOr<long> Process::sys_thread_exit(void* value)
{
LockGuard _(m_process_lock);
if (m_threads.size() == 1)
return sys_exit(0);
auto& thread = Thread::current();
if (!thread.is_detached())
{
TRY(m_exited_pthreads.emplace_back(thread.tid(), value));
m_pthread_exit_blocker.unblock();
LockGuard _(m_process_lock);
if (m_threads.size() == 1)
return sys_exit(0);
auto& thread = Thread::current();
if (!thread.is_detached())
{
TRY(m_exited_threads.emplace_back(thread.tid(), value));
m_thread_exit_blocker.unblock();
}
}
m_process_lock.unlock();
thread.on_exit();
Thread::current().on_exit();
ASSERT_NOT_REACHED();
}
BAN::ErrorOr<long> Process::sys_pthread_join(pthread_t tid, void** user_value)
BAN::ErrorOr<long> Process::sys_thread_join(pid_t tid, void** user_value)
{
LockGuard _(m_process_lock);
@@ -3456,12 +3453,12 @@ namespace Kernel
const auto check_thread =
[&]() -> BAN::Optional<void*>
{
for (size_t i = 0; i < m_exited_pthreads.size(); i++)
for (size_t i = 0; i < m_exited_threads.size(); i++)
{
if (m_exited_pthreads[i].thread != tid)
if (m_exited_threads[i].tid != tid)
continue;
void* ret = m_exited_pthreads[i].value;
m_exited_pthreads.remove(i);
void* ret = m_exited_threads[i].value;
m_exited_threads.remove(i);
return ret;
}
@@ -3494,16 +3491,16 @@ namespace Kernel
return BAN::Error::from_errno(EINVAL);
}
TRY(Thread::current().block_or_eintr_indefinite(m_pthread_exit_blocker, &m_process_lock));
TRY(Thread::current().block_or_eintr_indefinite(m_thread_exit_blocker, &m_process_lock));
}
}
BAN::ErrorOr<long> Process::sys_pthread_self()
BAN::ErrorOr<long> Process::sys_thread_getid()
{
return Thread::current().tid();
}
BAN::ErrorOr<long> Process::sys_pthread_kill(pthread_t tid, int signal)
BAN::ErrorOr<long> Process::sys_thread_kill(pid_t tid, int signal)
{
if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX))
return BAN::Error::from_errno(EINVAL);
@@ -3533,7 +3530,7 @@ namespace Kernel
return BAN::Error::from_errno(ESRCH);
}
BAN::ErrorOr<long> Process::sys_pthread_detach(pthread_t tid)
BAN::ErrorOr<long> Process::sys_thread_detach(pid_t tid)
{
LockGuard _(m_process_lock);
@@ -3544,7 +3541,7 @@ namespace Kernel
if (thread->is_detached())
return BAN::Error::from_errno(EINVAL);
thread->detach();
m_pthread_exit_blocker.unblock();
m_thread_exit_blocker.unblock();
return 0;
}

View File

@@ -311,7 +311,7 @@ namespace Kernel
Processor::gdt().set_cpu_index(Processor::current_index());
}
BAN::ErrorOr<Thread*> Thread::pthread_create(entry_t entry, void* arg)
BAN::ErrorOr<Thread*> Thread::thread_create(entry_t entry, void* arg)
{
auto* thread = TRY(create_userspace(m_process, m_process->page_table()));

View File

@@ -100,12 +100,12 @@ __BEGIN_DECLS
O(SYS_GET_FSBASE, get_fsbase) \
O(SYS_SET_GSBASE, set_gsbase) \
O(SYS_GET_GSBASE, get_gsbase) \
O(SYS_PTHREAD_CREATE, pthread_create) \
O(SYS_PTHREAD_EXIT, pthread_exit) \
O(SYS_PTHREAD_JOIN, pthread_join) \
O(SYS_PTHREAD_SELF, pthread_self) \
O(SYS_PTHREAD_KILL, pthread_kill) \
O(SYS_PTHREAD_DETACH, pthread_detach) \
O(SYS_THREAD_CREATE, thread_create) \
O(SYS_THREAD_EXIT, thread_exit) \
O(SYS_THREAD_JOIN, thread_join) \
O(SYS_THREAD_GETID, thread_getid) \
O(SYS_THREAD_KILL, thread_kill) \
O(SYS_THREAD_DETACH, thread_detach) \
O(SYS_EPOLL_CREATE1, epoll_create1) \
O(SYS_EPOLL_CTL, epoll_ctl) \
O(SYS_EPOLL_PWAIT2, epoll_pwait2) \

View File

@@ -62,7 +62,7 @@ asm(
extern "C" void _pthread_trampoline_cpp(void* arg)
{
auto info = *reinterpret_cast<pthread_trampoline_info_t*>(arg);
info.uthread->id = syscall(SYS_PTHREAD_SELF);
info.uthread->id = syscall(SYS_THREAD_GETID);
#if defined(__x86_64__)
syscall(SYS_SET_FSBASE, info.uthread);
#elif defined(__i686__)
@@ -432,7 +432,7 @@ int pthread_create(pthread_t* __restrict thread_id, const pthread_attr_t* __rest
info->uthread = uthread;
}
syscall_ret = syscall(SYS_PTHREAD_CREATE, attr, _pthread_trampoline, info);
syscall_ret = syscall(SYS_THREAD_CREATE, _pthread_trampoline, info);
if (syscall_ret == -1)
goto pthread_create_error;
@@ -450,7 +450,7 @@ pthread_create_error:
int pthread_detach(pthread_t thread)
{
return syscall(SYS_PTHREAD_DETACH, thread);
return syscall(SYS_THREAD_DETACH, thread);
}
void pthread_exit(void* value_ptr)
@@ -486,7 +486,7 @@ void pthread_exit(void* value_ptr)
}
free_uthread(uthread);
syscall(SYS_PTHREAD_EXIT, value_ptr);
syscall(SYS_THREAD_EXIT, value_ptr);
ASSERT_NOT_REACHED();
}
@@ -495,7 +495,7 @@ int pthread_join(pthread_t thread, void** value_ptr)
do {
pthread_testcancel();
errno = 0;
} while (syscall(SYS_PTHREAD_JOIN, thread, value_ptr) == -1 && errno == EINTR);
} while (syscall(SYS_THREAD_JOIN, thread, value_ptr) == -1 && errno == EINTR);
return errno;
}
@@ -528,7 +528,7 @@ static pthread_mutex_t s_atfork_mutex = PTHREAD_MUTEX_INITIALIZER;
void _pthread_call_atfork(int state)
{
if (state == _PTHREAD_ATFORK_CHILD)
_get_uthread()->id = syscall(SYS_PTHREAD_SELF);
_get_uthread()->id = syscall(SYS_THREAD_GETID);
pthread_mutex_lock(&s_atfork_mutex);

View File

@@ -1,5 +1,6 @@
#include <BAN/Assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <sys/syscall.h>
@@ -40,7 +41,7 @@ void psignal(int signum, const char* message)
int pthread_kill(pthread_t thread, int sig)
{
if (syscall(SYS_PTHREAD_KILL, thread, sig) == -1)
if (syscall(SYS_THREAD_KILL, thread, sig) == -1)
return errno;
return 0;
}
@@ -54,8 +55,7 @@ int pthread_sigmask(int how, const sigset_t* __restrict set, sigset_t* __restric
int raise(int sig)
{
// FIXME: won't work after multithreaded
return kill(getpid(), sig);
return pthread_kill(pthread_self(), sig);
}
int sigaction(int sig, const struct sigaction* __restrict act, struct sigaction* __restrict oact)

View File

@@ -66,7 +66,7 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t
#endif
{
self->cleanup_stack = nullptr;
self->id = syscall(SYS_PTHREAD_SELF);
self->id = syscall(SYS_THREAD_GETID);
self->errno_ = 0;
self->cancel_type = PTHREAD_CANCEL_DEFERRED;
self->cancel_state = PTHREAD_CANCEL_ENABLE;
@@ -84,7 +84,7 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t
.master_tls_module_count = 1,
.dynamic_tls = nullptr,
.cleanup_stack = nullptr,
.id = static_cast<pthread_t>(syscall(SYS_PTHREAD_SELF)),
.id = static_cast<pthread_t>(syscall(SYS_THREAD_GETID)),
.errno_ = 0,
.cancel_type = PTHREAD_CANCEL_DEFERRED,
.cancel_state = PTHREAD_CANCEL_ENABLE,

View File

@@ -223,7 +223,7 @@ constexpr uintptr_t SYM_NOT_FOUND = -1;
static void lock_global_lock()
{
const pthread_t tid = syscall(SYS_PTHREAD_SELF);
const pthread_t tid = syscall(SYS_THREAD_GETID);
pthread_t expected = 0;
while (!s_global_locker.compare_exchange(expected, tid))
@@ -1359,7 +1359,7 @@ static void initialize_tls(MasterTLS master_tls)
.master_tls_module_count = master_tls.module_count,
.dynamic_tls = s_dynamic_tls,
.cleanup_stack = nullptr,
.id = static_cast<pthread_t>(syscall(SYS_PTHREAD_SELF)),
.id = static_cast<pthread_t>(syscall(SYS_THREAD_GETID)),
.errno_ = 0,
.cancel_type = PTHREAD_CANCEL_DEFERRED,
.cancel_state = PTHREAD_CANCEL_ENABLE,