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:
@@ -209,12 +209,12 @@ namespace Kernel
|
|||||||
BAN::ErrorOr<long> sys_set_gsbase(void*);
|
BAN::ErrorOr<long> sys_set_gsbase(void*);
|
||||||
BAN::ErrorOr<long> sys_get_gsbase();
|
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_thread_create(void (*entry)(void*), void* arg);
|
||||||
BAN::ErrorOr<long> sys_pthread_exit(void* value);
|
BAN::ErrorOr<long> sys_thread_exit(void* value);
|
||||||
BAN::ErrorOr<long> sys_pthread_join(pthread_t thread, void** value);
|
BAN::ErrorOr<long> sys_thread_join(pid_t tid, void** value);
|
||||||
BAN::ErrorOr<long> sys_pthread_self();
|
BAN::ErrorOr<long> sys_thread_getid();
|
||||||
BAN::ErrorOr<long> sys_pthread_kill(pthread_t thread, int signal);
|
BAN::ErrorOr<long> sys_thread_kill(pid_t tid, int signal);
|
||||||
BAN::ErrorOr<long> sys_pthread_detach(pthread_t thread);
|
BAN::ErrorOr<long> sys_thread_detach(pid_t tid);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*);
|
BAN::ErrorOr<long> sys_clock_gettime(clockid_t, timespec*);
|
||||||
|
|
||||||
@@ -364,13 +364,13 @@ namespace Kernel
|
|||||||
|
|
||||||
BAN::Vector<Thread*> m_threads;
|
BAN::Vector<Thread*> m_threads;
|
||||||
|
|
||||||
struct pthread_info_t
|
struct exited_thread_info_t
|
||||||
{
|
{
|
||||||
pthread_t thread;
|
pid_t tid;
|
||||||
void* value;
|
void* value;
|
||||||
};
|
};
|
||||||
BAN::Vector<pthread_info_t> m_exited_pthreads;
|
BAN::Vector<exited_thread_info_t> m_exited_threads;
|
||||||
ThreadBlocker m_pthread_exit_blocker;
|
ThreadBlocker m_thread_exit_blocker;
|
||||||
|
|
||||||
uint64_t m_alarm_interval_ns { 0 };
|
uint64_t m_alarm_interval_ns { 0 };
|
||||||
uint64_t m_alarm_wake_time_ns { 0 };
|
uint64_t m_alarm_wake_time_ns { 0 };
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ namespace Kernel
|
|||||||
static BAN::ErrorOr<Thread*> create_userspace(Process*, PageTable&);
|
static BAN::ErrorOr<Thread*> create_userspace(Process*, PageTable&);
|
||||||
~Thread();
|
~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);
|
BAN::ErrorOr<Thread*> clone(Process*, uintptr_t sp, uintptr_t ip);
|
||||||
void setup_process_cleanup();
|
void setup_process_cleanup();
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
namespace Kernel::ELF
|
namespace Kernel::ELF
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,7 +28,6 @@
|
|||||||
#include <LibInput/KeyboardLayout.h>
|
#include <LibInput/KeyboardLayout.h>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pthread.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/banan-os.h>
|
#include <sys/banan-os.h>
|
||||||
#include <sys/eventfd.h>
|
#include <sys/eventfd.h>
|
||||||
@@ -245,7 +244,7 @@ namespace Kernel
|
|||||||
Process::~Process()
|
Process::~Process()
|
||||||
{
|
{
|
||||||
ASSERT(m_threads.empty());
|
ASSERT(m_threads.empty());
|
||||||
ASSERT(m_exited_pthreads.empty());
|
ASSERT(m_exited_threads.empty());
|
||||||
ASSERT(m_mapped_regions.empty());
|
ASSERT(m_mapped_regions.empty());
|
||||||
ASSERT(!m_page_table);
|
ASSERT(!m_page_table);
|
||||||
}
|
}
|
||||||
@@ -293,7 +292,7 @@ namespace Kernel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_exited_pthreads.clear();
|
m_exited_threads.clear();
|
||||||
|
|
||||||
ProcFileSystem::get().on_process_delete(*this);
|
ProcFileSystem::get().on_process_delete(*this);
|
||||||
|
|
||||||
@@ -3412,41 +3411,39 @@ namespace Kernel
|
|||||||
return Thread::current().get_gsbase();
|
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);
|
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(m_threads.push_back(new_thread));
|
||||||
MUST(Processor::scheduler().add_thread(new_thread));
|
MUST(Processor::scheduler().add_thread(new_thread));
|
||||||
|
|
||||||
return new_thread->tid();
|
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));
|
LockGuard _(m_process_lock);
|
||||||
m_pthread_exit_blocker.unblock();
|
|
||||||
|
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::current().on_exit();
|
||||||
thread.on_exit();
|
|
||||||
|
|
||||||
ASSERT_NOT_REACHED();
|
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);
|
LockGuard _(m_process_lock);
|
||||||
|
|
||||||
@@ -3456,12 +3453,12 @@ namespace Kernel
|
|||||||
const auto check_thread =
|
const auto check_thread =
|
||||||
[&]() -> BAN::Optional<void*>
|
[&]() -> 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;
|
continue;
|
||||||
void* ret = m_exited_pthreads[i].value;
|
void* ret = m_exited_threads[i].value;
|
||||||
m_exited_pthreads.remove(i);
|
m_exited_threads.remove(i);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3494,16 +3491,16 @@ namespace Kernel
|
|||||||
return BAN::Error::from_errno(EINVAL);
|
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();
|
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))
|
if (signal != 0 && (signal < _SIGMIN || signal > _SIGMAX))
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
@@ -3533,7 +3530,7 @@ namespace Kernel
|
|||||||
return BAN::Error::from_errno(ESRCH);
|
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);
|
LockGuard _(m_process_lock);
|
||||||
|
|
||||||
@@ -3544,7 +3541,7 @@ namespace Kernel
|
|||||||
if (thread->is_detached())
|
if (thread->is_detached())
|
||||||
return BAN::Error::from_errno(EINVAL);
|
return BAN::Error::from_errno(EINVAL);
|
||||||
thread->detach();
|
thread->detach();
|
||||||
m_pthread_exit_blocker.unblock();
|
m_thread_exit_blocker.unblock();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -311,7 +311,7 @@ namespace Kernel
|
|||||||
Processor::gdt().set_cpu_index(Processor::current_index());
|
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()));
|
auto* thread = TRY(create_userspace(m_process, m_process->page_table()));
|
||||||
|
|
||||||
|
|||||||
@@ -100,12 +100,12 @@ __BEGIN_DECLS
|
|||||||
O(SYS_GET_FSBASE, get_fsbase) \
|
O(SYS_GET_FSBASE, get_fsbase) \
|
||||||
O(SYS_SET_GSBASE, set_gsbase) \
|
O(SYS_SET_GSBASE, set_gsbase) \
|
||||||
O(SYS_GET_GSBASE, get_gsbase) \
|
O(SYS_GET_GSBASE, get_gsbase) \
|
||||||
O(SYS_PTHREAD_CREATE, pthread_create) \
|
O(SYS_THREAD_CREATE, thread_create) \
|
||||||
O(SYS_PTHREAD_EXIT, pthread_exit) \
|
O(SYS_THREAD_EXIT, thread_exit) \
|
||||||
O(SYS_PTHREAD_JOIN, pthread_join) \
|
O(SYS_THREAD_JOIN, thread_join) \
|
||||||
O(SYS_PTHREAD_SELF, pthread_self) \
|
O(SYS_THREAD_GETID, thread_getid) \
|
||||||
O(SYS_PTHREAD_KILL, pthread_kill) \
|
O(SYS_THREAD_KILL, thread_kill) \
|
||||||
O(SYS_PTHREAD_DETACH, pthread_detach) \
|
O(SYS_THREAD_DETACH, thread_detach) \
|
||||||
O(SYS_EPOLL_CREATE1, epoll_create1) \
|
O(SYS_EPOLL_CREATE1, epoll_create1) \
|
||||||
O(SYS_EPOLL_CTL, epoll_ctl) \
|
O(SYS_EPOLL_CTL, epoll_ctl) \
|
||||||
O(SYS_EPOLL_PWAIT2, epoll_pwait2) \
|
O(SYS_EPOLL_PWAIT2, epoll_pwait2) \
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ asm(
|
|||||||
extern "C" void _pthread_trampoline_cpp(void* arg)
|
extern "C" void _pthread_trampoline_cpp(void* arg)
|
||||||
{
|
{
|
||||||
auto info = *reinterpret_cast<pthread_trampoline_info_t*>(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__)
|
#if defined(__x86_64__)
|
||||||
syscall(SYS_SET_FSBASE, info.uthread);
|
syscall(SYS_SET_FSBASE, info.uthread);
|
||||||
#elif defined(__i686__)
|
#elif defined(__i686__)
|
||||||
@@ -432,7 +432,7 @@ int pthread_create(pthread_t* __restrict thread_id, const pthread_attr_t* __rest
|
|||||||
info->uthread = uthread;
|
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)
|
if (syscall_ret == -1)
|
||||||
goto pthread_create_error;
|
goto pthread_create_error;
|
||||||
|
|
||||||
@@ -450,7 +450,7 @@ pthread_create_error:
|
|||||||
|
|
||||||
int pthread_detach(pthread_t thread)
|
int pthread_detach(pthread_t thread)
|
||||||
{
|
{
|
||||||
return syscall(SYS_PTHREAD_DETACH, thread);
|
return syscall(SYS_THREAD_DETACH, thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pthread_exit(void* value_ptr)
|
void pthread_exit(void* value_ptr)
|
||||||
@@ -486,7 +486,7 @@ void pthread_exit(void* value_ptr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
free_uthread(uthread);
|
free_uthread(uthread);
|
||||||
syscall(SYS_PTHREAD_EXIT, value_ptr);
|
syscall(SYS_THREAD_EXIT, value_ptr);
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,7 +495,7 @@ int pthread_join(pthread_t thread, void** value_ptr)
|
|||||||
do {
|
do {
|
||||||
pthread_testcancel();
|
pthread_testcancel();
|
||||||
errno = 0;
|
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;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -528,7 +528,7 @@ static pthread_mutex_t s_atfork_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|||||||
void _pthread_call_atfork(int state)
|
void _pthread_call_atfork(int state)
|
||||||
{
|
{
|
||||||
if (state == _PTHREAD_ATFORK_CHILD)
|
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);
|
pthread_mutex_lock(&s_atfork_mutex);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <BAN/Assert.h>
|
#include <BAN/Assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
@@ -40,7 +41,7 @@ void psignal(int signum, const char* message)
|
|||||||
|
|
||||||
int pthread_kill(pthread_t thread, int sig)
|
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 errno;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -54,8 +55,7 @@ int pthread_sigmask(int how, const sigset_t* __restrict set, sigset_t* __restric
|
|||||||
|
|
||||||
int raise(int sig)
|
int raise(int sig)
|
||||||
{
|
{
|
||||||
// FIXME: won't work after multithreaded
|
return pthread_kill(pthread_self(), sig);
|
||||||
return kill(getpid(), sig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int sigaction(int sig, const struct sigaction* __restrict act, struct sigaction* __restrict oact)
|
int sigaction(int sig, const struct sigaction* __restrict act, struct sigaction* __restrict oact)
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ extern "C" void _init_libc(char** environ, init_funcs_t init_funcs, init_funcs_t
|
|||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
self->cleanup_stack = nullptr;
|
self->cleanup_stack = nullptr;
|
||||||
self->id = syscall(SYS_PTHREAD_SELF);
|
self->id = syscall(SYS_THREAD_GETID);
|
||||||
self->errno_ = 0;
|
self->errno_ = 0;
|
||||||
self->cancel_type = PTHREAD_CANCEL_DEFERRED;
|
self->cancel_type = PTHREAD_CANCEL_DEFERRED;
|
||||||
self->cancel_state = PTHREAD_CANCEL_ENABLE;
|
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,
|
.master_tls_module_count = 1,
|
||||||
.dynamic_tls = nullptr,
|
.dynamic_tls = nullptr,
|
||||||
.cleanup_stack = nullptr,
|
.cleanup_stack = nullptr,
|
||||||
.id = static_cast<pthread_t>(syscall(SYS_PTHREAD_SELF)),
|
.id = static_cast<pthread_t>(syscall(SYS_THREAD_GETID)),
|
||||||
.errno_ = 0,
|
.errno_ = 0,
|
||||||
.cancel_type = PTHREAD_CANCEL_DEFERRED,
|
.cancel_type = PTHREAD_CANCEL_DEFERRED,
|
||||||
.cancel_state = PTHREAD_CANCEL_ENABLE,
|
.cancel_state = PTHREAD_CANCEL_ENABLE,
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ constexpr uintptr_t SYM_NOT_FOUND = -1;
|
|||||||
|
|
||||||
static void lock_global_lock()
|
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;
|
pthread_t expected = 0;
|
||||||
while (!s_global_locker.compare_exchange(expected, tid))
|
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,
|
.master_tls_module_count = master_tls.module_count,
|
||||||
.dynamic_tls = s_dynamic_tls,
|
.dynamic_tls = s_dynamic_tls,
|
||||||
.cleanup_stack = nullptr,
|
.cleanup_stack = nullptr,
|
||||||
.id = static_cast<pthread_t>(syscall(SYS_PTHREAD_SELF)),
|
.id = static_cast<pthread_t>(syscall(SYS_THREAD_GETID)),
|
||||||
.errno_ = 0,
|
.errno_ = 0,
|
||||||
.cancel_type = PTHREAD_CANCEL_DEFERRED,
|
.cancel_type = PTHREAD_CANCEL_DEFERRED,
|
||||||
.cancel_state = PTHREAD_CANCEL_ENABLE,
|
.cancel_state = PTHREAD_CANCEL_ENABLE,
|
||||||
|
|||||||
Reference in New Issue
Block a user