2022-10-14 15:25:47 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/assert.h"
|
2023-06-04 15:19:45 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/errno.h"
|
2022-11-06 02:49:41 +00:00
|
|
|
#include "libc/intrin/atomic.h"
|
2023-07-06 13:57:28 +00:00
|
|
|
#include "libc/intrin/dll.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/intrin/kmalloc.h"
|
2022-11-09 11:58:57 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-11-02 05:36:03 +00:00
|
|
|
#include "libc/runtime/memtrack.internal.h"
|
|
|
|
#include "libc/str/str.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/thread/posixthread.internal.h"
|
2022-10-14 15:25:47 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2022-10-14 15:25:47 +00:00
|
|
|
|
2022-10-16 19:05:08 +00:00
|
|
|
static struct AtForks {
|
2022-11-02 05:36:03 +00:00
|
|
|
pthread_spinlock_t lock;
|
2022-10-16 19:05:08 +00:00
|
|
|
struct AtFork {
|
|
|
|
struct AtFork *p[2];
|
|
|
|
atfork_f f[3];
|
|
|
|
} * list;
|
|
|
|
} _atforks;
|
|
|
|
|
2022-11-09 11:58:57 +00:00
|
|
|
static void _pthread_purge(void) {
|
2023-07-06 13:57:28 +00:00
|
|
|
struct Dll *e;
|
|
|
|
while ((e = dll_first(_pthread_list))) {
|
|
|
|
dll_remove(&_pthread_list, e);
|
|
|
|
_pthread_free(POSIXTHREAD_CONTAINER(e));
|
2022-11-09 11:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 19:05:08 +00:00
|
|
|
static void _pthread_onfork(int i) {
|
|
|
|
struct AtFork *a;
|
2023-07-26 20:54:49 +00:00
|
|
|
unassert(0 <= i && i <= 2);
|
2022-11-02 05:36:03 +00:00
|
|
|
if (!i) pthread_spin_lock(&_atforks.lock);
|
2022-10-16 19:05:08 +00:00
|
|
|
for (a = _atforks.list; a; a = a->p[!i]) {
|
|
|
|
if (a->f[i]) a->f[i]();
|
|
|
|
_atforks.list = a;
|
|
|
|
}
|
2022-11-02 05:36:03 +00:00
|
|
|
if (i) pthread_spin_unlock(&_atforks.lock);
|
2022-10-16 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _pthread_onfork_prepare(void) {
|
|
|
|
_pthread_onfork(0);
|
2022-11-09 11:58:57 +00:00
|
|
|
pthread_spin_lock(&_pthread_lock);
|
2023-06-04 15:19:45 +00:00
|
|
|
__fds_lock();
|
2022-11-02 05:36:03 +00:00
|
|
|
__mmi_lock();
|
2023-06-04 15:19:45 +00:00
|
|
|
__kmalloc_lock();
|
2022-10-16 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _pthread_onfork_parent(void) {
|
2022-11-02 05:36:03 +00:00
|
|
|
__kmalloc_unlock();
|
2023-06-04 15:19:45 +00:00
|
|
|
__mmi_unlock();
|
|
|
|
__fds_unlock();
|
2022-11-09 11:58:57 +00:00
|
|
|
pthread_spin_unlock(&_pthread_lock);
|
2022-10-16 19:05:08 +00:00
|
|
|
_pthread_onfork(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _pthread_onfork_child(void) {
|
2022-11-06 02:49:41 +00:00
|
|
|
struct CosmoTib *tib;
|
|
|
|
struct PosixThread *pt;
|
2022-11-05 01:19:05 +00:00
|
|
|
pthread_mutexattr_t attr;
|
2022-11-02 05:36:03 +00:00
|
|
|
extern pthread_mutex_t __mmi_lock_obj;
|
2022-11-06 02:49:41 +00:00
|
|
|
tib = __get_tls();
|
|
|
|
pt = (struct PosixThread *)tib->tib_pthread;
|
2022-11-09 11:58:57 +00:00
|
|
|
|
|
|
|
// let's choose to let the new process live.
|
|
|
|
// even though it's unclear what to do with this kind of race.
|
2022-11-06 02:49:41 +00:00
|
|
|
atomic_store_explicit(&pt->cancelled, false, memory_order_relaxed);
|
2022-11-09 11:58:57 +00:00
|
|
|
|
|
|
|
// wipe core runtime locks
|
2023-06-04 15:19:45 +00:00
|
|
|
__kmalloc_unlock();
|
2022-11-05 01:19:05 +00:00
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init(&__mmi_lock_obj, &attr);
|
2023-06-04 15:19:45 +00:00
|
|
|
pthread_mutex_init(&__fds_lock_obj, &attr);
|
2023-09-02 03:49:13 +00:00
|
|
|
(void)pthread_spin_init(&_pthread_lock, 0);
|
2022-11-09 11:58:57 +00:00
|
|
|
|
|
|
|
// call user-supplied forked child callbacks
|
2022-10-16 19:05:08 +00:00
|
|
|
_pthread_onfork(2);
|
2022-11-09 11:58:57 +00:00
|
|
|
|
|
|
|
// delete other threads that existed before forking
|
|
|
|
// this must come after onfork, since it calls free
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_remove(&_pthread_list, &pt->list);
|
2022-11-09 11:58:57 +00:00
|
|
|
_pthread_purge();
|
2023-07-06 13:57:28 +00:00
|
|
|
dll_make_first(&_pthread_list, &pt->list);
|
2022-10-16 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int _pthread_atfork(atfork_f prepare, atfork_f parent, atfork_f child) {
|
|
|
|
int rc;
|
|
|
|
struct AtFork *a;
|
2022-11-02 05:36:03 +00:00
|
|
|
if (!(a = kmalloc(sizeof(struct AtFork)))) return ENOMEM;
|
2022-10-16 19:05:08 +00:00
|
|
|
a->f[0] = prepare;
|
|
|
|
a->f[1] = parent;
|
|
|
|
a->f[2] = child;
|
2022-11-02 05:36:03 +00:00
|
|
|
pthread_spin_lock(&_atforks.lock);
|
2022-10-16 19:05:08 +00:00
|
|
|
a->p[0] = 0;
|
|
|
|
a->p[1] = _atforks.list;
|
|
|
|
if (_atforks.list) _atforks.list->p[0] = a;
|
|
|
|
_atforks.list = a;
|
2022-11-02 05:36:03 +00:00
|
|
|
pthread_spin_unlock(&_atforks.lock);
|
2022-10-16 19:05:08 +00:00
|
|
|
rc = 0;
|
|
|
|
return rc;
|
2022-10-14 15:25:47 +00:00
|
|
|
}
|