From 1515283c7f4f048ae4675dba993866ac3dd7a1ec Mon Sep 17 00:00:00 2001 From: Lubos Dolezel Date: Wed, 18 Jul 2018 22:10:01 +0200 Subject: [PATCH] Short circuit the most common pthread_canceled() call for singlethreaded programs --- src/kernel/emulation/linux/bsdthread/bsdthread_create.c | 8 ++++++++ src/kernel/emulation/linux/bsdthread/bsdthread_create.h | 3 +++ src/kernel/emulation/linux/bsdthread/pthread_canceled.c | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/src/kernel/emulation/linux/bsdthread/bsdthread_create.c b/src/kernel/emulation/linux/bsdthread/bsdthread_create.c index 582c01a88..fd674e37b 100644 --- a/src/kernel/emulation/linux/bsdthread/bsdthread_create.c +++ b/src/kernel/emulation/linux/bsdthread/bsdthread_create.c @@ -22,6 +22,8 @@ extern void *memset(void *s, int c, size_t n); #define STACK_GUARD_SIZE 4096 +static bool _uses_threads = false; + // http://www.tldp.org/FAQ/Threads-FAQ/clone.c long sys_bsdthread_create(void* thread_start, void* arg, @@ -31,6 +33,8 @@ long sys_bsdthread_create(void* thread_start, void* arg, int ret; unsigned long stacksize = 0; + _uses_threads = true; + #ifndef BSDTHREAD_WRAP_LINUX_PTHREAD if (!(flags & PTHREAD_START_CUSTOM)) { @@ -123,4 +127,8 @@ int darling_thread_create(void** stack, void* entry_point, uintptr_t arg3, } #endif +bool uses_threads(void) +{ + return _uses_threads; +} diff --git a/src/kernel/emulation/linux/bsdthread/bsdthread_create.h b/src/kernel/emulation/linux/bsdthread/bsdthread_create.h index 82a72e362..24f85c4bb 100644 --- a/src/kernel/emulation/linux/bsdthread/bsdthread_create.h +++ b/src/kernel/emulation/linux/bsdthread/bsdthread_create.h @@ -1,6 +1,7 @@ #ifndef BSDTHREAD_CREATE_H #define BSDTHREAD_CREATE_H #include +#include long sys_bsdthread_create(void* thread_start, void* arg, void** stack, void* pthread, uint32_t flags); @@ -10,6 +11,8 @@ int darling_thread_create(void** stack, void* entry_point, uintptr_t arg3, void* thread_stack_allocate(unsigned long stacksize); +bool uses_threads(void); + #define LINUX_CLONE_VM 0x00000100 #define LINUX_CLONE_FS 0x00000200 #define LINUX_CLONE_FILES 0x00000400 diff --git a/src/kernel/emulation/linux/bsdthread/pthread_canceled.c b/src/kernel/emulation/linux/bsdthread/pthread_canceled.c index 673394839..3902441b6 100644 --- a/src/kernel/emulation/linux/bsdthread/pthread_canceled.c +++ b/src/kernel/emulation/linux/bsdthread/pthread_canceled.c @@ -5,9 +5,14 @@ #include #include "../mach/lkm.h" #include "../../../../lkm/api.h" +#include "bsdthread_create.h" +#include long sys_pthread_canceled(int action) { + if (action == 0 && !uses_threads()) + return -EINVAL; + int ret = lkm_call(NR_pthread_canceled, (void*)(long) action); if (ret < 0) ret = errno_linux_to_bsd(ret);