From d164dd13f8ad16c1bab95d5ebc8f4192efc503f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20S=C5=82omi=C5=84ski?= Date: Sun, 23 Feb 2020 19:13:25 +0100 Subject: [PATCH] options/posix: implement pthread_self --- meson.build | 1 + options/internal/include/mlibc/thread.hpp | 10 ++++++++++ options/internal/x86_64/thread.cpp | 13 +++++++++++++ options/posix/generic/pthread-stubs.cpp | 6 ++++-- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 options/internal/include/mlibc/thread.hpp create mode 100644 options/internal/x86_64/thread.cpp diff --git a/meson.build b/meson.build index 31727511..ef181e10 100644 --- a/meson.build +++ b/meson.build @@ -135,6 +135,7 @@ internal_sources = [ 'options/internal/gcc/initfini.cpp', 'options/internal/gcc-extra/cxxabi.cpp', 'options/internal/x86_64/setjmp.S', + 'options/internal/x86_64/thread.cpp', ] if not static diff --git a/options/internal/include/mlibc/thread.hpp b/options/internal/include/mlibc/thread.hpp new file mode 100644 index 00000000..dad28def --- /dev/null +++ b/options/internal/include/mlibc/thread.hpp @@ -0,0 +1,10 @@ +#ifndef MLIBC_THREAD_HPP +#define MLIBC_THREAD_HPP + +namespace mlibc { + +void *get_current_tcb(); + +} + +#endif // MLIBC_THREAD_HPP diff --git a/options/internal/x86_64/thread.cpp b/options/internal/x86_64/thread.cpp new file mode 100644 index 00000000..64efb11c --- /dev/null +++ b/options/internal/x86_64/thread.cpp @@ -0,0 +1,13 @@ +#include + +#include + +namespace mlibc { + +void *get_current_tcb() { + uintptr_t ptr; + asm ("movq %%fs:0, %0" : "=r"(ptr)); + return reinterpret_cast(ptr); +} + +} diff --git a/options/posix/generic/pthread-stubs.cpp b/options/posix/generic/pthread-stubs.cpp index 451a14da..bb9d2a8a 100644 --- a/options/posix/generic/pthread-stubs.cpp +++ b/options/posix/generic/pthread-stubs.cpp @@ -10,6 +10,7 @@ #include #include #include +#include static bool enableTrace = false; @@ -100,10 +101,11 @@ int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, __ensure(!"Not implemented"); __builtin_unreachable(); } + pthread_t pthread_self(void) { - __ensure(!"Not implemented"); - __builtin_unreachable(); + return reinterpret_cast(mlibc::get_current_tcb()); } + int pthread_equal(pthread_t, pthread_t) { __ensure(!"Not implemented"); __builtin_unreachable();