mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-03 09:48:29 +00:00
Allow user to override pthread mutex and cond
This commit is contained in:
parent
4705705548
commit
55b7aa1632
54 changed files with 216 additions and 102 deletions
|
@ -52,7 +52,7 @@ extern pthread_mutex_t __sig_worker_lock;
|
|||
|
||||
static void sys_execve_nt_abort(sigset_t sigmask) {
|
||||
_pthread_unlock();
|
||||
pthread_mutex_unlock(&__sig_worker_lock);
|
||||
_pthread_mutex_unlock(&__sig_worker_lock);
|
||||
__sig_unblock(sigmask);
|
||||
}
|
||||
|
||||
|
@ -61,8 +61,8 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|||
|
||||
// execve() needs to be @asyncsignalsafe
|
||||
sigset_t sigmask = __sig_block();
|
||||
pthread_mutex_lock(&__sig_worker_lock); // order matters
|
||||
_pthread_lock(); // order matters
|
||||
_pthread_mutex_lock(&__sig_worker_lock); // order matters
|
||||
_pthread_lock(); // order matters
|
||||
|
||||
// new process should be a child of our parent
|
||||
int64_t hParentProcess;
|
||||
|
|
|
@ -68,7 +68,6 @@
|
|||
#ifdef __x86_64__
|
||||
|
||||
extern long __klog_handle;
|
||||
void WipeKeystrokes(void);
|
||||
__msabi extern typeof(GetCurrentProcessId) *const __imp_GetCurrentProcessId;
|
||||
|
||||
static textwindows wontreturn void AbortFork(const char *func, void *addr) {
|
||||
|
@ -466,8 +465,6 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
// re-apply code morphing for function tracing
|
||||
if (ftrace_stackdigs)
|
||||
_weaken(__hook)(_weaken(ftrace_hook), _weaken(GetSymbolTable)());
|
||||
// reset core runtime services
|
||||
WipeKeystrokes();
|
||||
// notify pthread join
|
||||
atomic_store_explicit(&_pthread_static.ptid, GetCurrentThreadId(),
|
||||
memory_order_release);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/state.internal.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
|
@ -50,11 +51,14 @@
|
|||
|
||||
__msabi extern typeof(GetCurrentProcessId) *const __imp_GetCurrentProcessId;
|
||||
|
||||
extern pthread_mutex_t __rand64_lock_obj;
|
||||
extern pthread_mutex_t __pthread_lock_obj;
|
||||
extern pthread_mutex_t __cxa_lock_obj;
|
||||
extern pthread_mutex_t __dlopen_lock_obj;
|
||||
extern pthread_mutex_t __pthread_lock_obj;
|
||||
extern pthread_mutex_t __rand64_lock_obj;
|
||||
extern pthread_mutex_t __sig_worker_lock;
|
||||
|
||||
void __dlopen_lock(void);
|
||||
void __dlopen_unlock(void);
|
||||
void nsync_mu_semaphore_sem_fork_child(void);
|
||||
|
||||
// first and last and always
|
||||
|
@ -79,7 +83,7 @@ StartOver:
|
|||
f->forking = 1;
|
||||
__stdio_ref(f);
|
||||
__stdio_unlock();
|
||||
pthread_mutex_lock(&f->lock);
|
||||
_pthread_mutex_lock(&f->lock);
|
||||
__stdio_unref(f);
|
||||
goto StartOver;
|
||||
}
|
||||
|
@ -89,7 +93,7 @@ static void fork_parent_stdio(void) {
|
|||
struct Dll *e;
|
||||
for (e = dll_first(__stdio.files); e; e = dll_next(__stdio.files, e)) {
|
||||
FILE_CONTAINER(e)->forking = 0;
|
||||
pthread_mutex_unlock(&FILE_CONTAINER(e)->lock);
|
||||
_pthread_mutex_unlock(&FILE_CONTAINER(e)->lock);
|
||||
}
|
||||
__stdio_unlock();
|
||||
}
|
||||
|
@ -97,25 +101,26 @@ static void fork_parent_stdio(void) {
|
|||
static void fork_child_stdio(void) {
|
||||
struct Dll *e;
|
||||
for (e = dll_first(__stdio.files); e; e = dll_next(__stdio.files, e)) {
|
||||
pthread_mutex_wipe_np(&FILE_CONTAINER(e)->lock);
|
||||
_pthread_mutex_wipe_np(&FILE_CONTAINER(e)->lock);
|
||||
FILE_CONTAINER(e)->forking = 0;
|
||||
}
|
||||
pthread_mutex_wipe_np(&__stdio.lock);
|
||||
_pthread_mutex_wipe_np(&__stdio.lock);
|
||||
}
|
||||
|
||||
static void fork_prepare(void) {
|
||||
pthread_mutex_lock(&supreme_lock);
|
||||
_pthread_mutex_lock(&supreme_lock);
|
||||
if (_weaken(_pthread_onfork_prepare))
|
||||
_weaken(_pthread_onfork_prepare)();
|
||||
fork_prepare_stdio();
|
||||
__localtime_lock();
|
||||
__dlopen_lock();
|
||||
__cxa_lock();
|
||||
__gdtoa_lock1();
|
||||
__gdtoa_lock();
|
||||
_pthread_lock();
|
||||
dlmalloc_pre_fork();
|
||||
__fds_lock();
|
||||
pthread_mutex_lock(&__rand64_lock_obj);
|
||||
_pthread_mutex_lock(&__rand64_lock_obj);
|
||||
if (_weaken(cosmo_stack_lock))
|
||||
_weaken(cosmo_stack_lock)();
|
||||
__maps_lock();
|
||||
|
@ -126,45 +131,48 @@ static void fork_parent(void) {
|
|||
__maps_unlock();
|
||||
if (_weaken(cosmo_stack_unlock))
|
||||
_weaken(cosmo_stack_unlock)();
|
||||
pthread_mutex_unlock(&__rand64_lock_obj);
|
||||
_pthread_mutex_unlock(&__rand64_lock_obj);
|
||||
__fds_unlock();
|
||||
dlmalloc_post_fork_parent();
|
||||
_pthread_unlock();
|
||||
__gdtoa_unlock();
|
||||
__gdtoa_unlock1();
|
||||
__cxa_unlock();
|
||||
__dlopen_unlock();
|
||||
__localtime_unlock();
|
||||
fork_parent_stdio();
|
||||
if (_weaken(_pthread_onfork_parent))
|
||||
_weaken(_pthread_onfork_parent)();
|
||||
pthread_mutex_unlock(&supreme_lock);
|
||||
_pthread_mutex_unlock(&supreme_lock);
|
||||
}
|
||||
|
||||
static void fork_child(void) {
|
||||
nsync_mu_semaphore_sem_fork_child();
|
||||
if (_weaken(cosmo_stack_wipe))
|
||||
_weaken(cosmo_stack_wipe)();
|
||||
pthread_mutex_wipe_np(&__rand64_lock_obj);
|
||||
pthread_mutex_wipe_np(&__fds_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__dlopen_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__rand64_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__fds_lock_obj);
|
||||
dlmalloc_post_fork_child();
|
||||
pthread_mutex_wipe_np(&__gdtoa_lock_obj);
|
||||
pthread_mutex_wipe_np(&__gdtoa_lock1_obj);
|
||||
_pthread_mutex_wipe_np(&__gdtoa_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__gdtoa_lock1_obj);
|
||||
fork_child_stdio();
|
||||
pthread_mutex_wipe_np(&__pthread_lock_obj);
|
||||
pthread_mutex_wipe_np(&__cxa_lock_obj);
|
||||
pthread_mutex_wipe_np(&__localtime_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__pthread_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__cxa_lock_obj);
|
||||
_pthread_mutex_wipe_np(&__localtime_lock_obj);
|
||||
if (IsWindows()) {
|
||||
// we don't bother locking the proc/itimer/sig locks above since
|
||||
// their state is reset in the forked child. nothing to protect.
|
||||
sys_read_nt_wipe_keystrokes();
|
||||
__proc_wipe_and_reset();
|
||||
__itimer_wipe_and_reset();
|
||||
pthread_mutex_wipe_np(&__sig_worker_lock);
|
||||
_pthread_mutex_wipe_np(&__sig_worker_lock);
|
||||
if (_weaken(__sig_init))
|
||||
_weaken(__sig_init)();
|
||||
}
|
||||
if (_weaken(_pthread_onfork_child))
|
||||
_weaken(_pthread_onfork_child)();
|
||||
pthread_mutex_wipe_np(&supreme_lock);
|
||||
_pthread_mutex_wipe_np(&supreme_lock);
|
||||
}
|
||||
|
||||
int _fork(uint32_t dwCreationFlags) {
|
||||
|
|
|
@ -255,14 +255,14 @@ static textwindows void __proc_setup(void) {
|
|||
*/
|
||||
textwindows void __proc_lock(void) {
|
||||
cosmo_once(&__proc.once, __proc_setup);
|
||||
pthread_mutex_lock(&__proc.lock);
|
||||
_pthread_mutex_lock(&__proc.lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks process tracker.
|
||||
*/
|
||||
textwindows void __proc_unlock(void) {
|
||||
pthread_mutex_unlock(&__proc.lock);
|
||||
_pthread_mutex_unlock(&__proc.lock);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -273,7 +273,7 @@ textwindows void __proc_wipe_and_reset(void) {
|
|||
pthread_mutex_t lock = __proc.lock;
|
||||
bzero(&__proc, sizeof(__proc));
|
||||
__proc.lock = lock;
|
||||
pthread_mutex_wipe_np(&__proc.lock);
|
||||
_pthread_mutex_wipe_np(&__proc.lock);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue