Implement pthread_atfork()

If threads are being used, then fork() will now acquire and release and
runtime locks so that fork() may be safely used from threads. This also
makes vfork() thread safe, because pthread mutexes will do nothing when
the process is a child of vfork(). More torture tests have been written
to confirm this all works like a charm. Additionally:

- Invent hexpcpy() api
- Rename nsync_malloc_() to kmalloc()
- Complete posix named semaphore implementation
- Make pthread_create() asynchronous signal safe
- Add rm, rmdir, and touch to command interpreter builtins
- Invent sigisprecious() and modify sigset functions to use it
- Add unit tests for posix_spawn() attributes and fix its bugs

One unresolved problem is the reclaiming of *NSYNC waiter memory in the
forked child processes, within apps which have threads waiting on locks
This commit is contained in:
Justine Tunney 2022-10-16 12:05:08 -07:00
parent 64c284003d
commit 60cb435cb4
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
124 changed files with 2169 additions and 718 deletions

19
third_party/nsync/README.cosmo vendored Normal file
View file

@ -0,0 +1,19 @@
DESCRIPTION
*NSYNC is a synchronization primitives library.
LICENSE
Apache 2.0
ORIGIN
git@github.com:google/nsync
commit ac5489682760393fe21bd2a8e038b528442412a7
Author: Mike Burrows <m3b@google.com>
Date: Wed Jun 1 16:47:52 2022 -0700
LOCAL CHANGES
- nsync_malloc_() is implemented as kmalloc()
- nsync_mu_semaphore uses Cosmopolitan Futexes

View file

@ -15,6 +15,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/intrin/kmalloc.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"
@ -23,7 +24,6 @@
#include "third_party/nsync/atomic.internal.h"
#include "third_party/nsync/common.internal.h"
#include "third_party/nsync/dll.h"
#include "third_party/nsync/malloc.internal.h"
#include "third_party/nsync/mu_semaphore.h"
#include "third_party/nsync/races.internal.h"
#include "third_party/nsync/wait_s.internal.h"
@ -188,7 +188,7 @@ waiter *nsync_waiter_new_ (void) {
}
ATM_STORE_REL (&free_waiters_mu, 0); /* release store */
if (w == NULL) { /* If free list was empty, allocate an item. */
w = (waiter *) nsync_malloc_ (sizeof (*w));
w = (waiter *) kmalloc (sizeof (*w));
w->tag = WAITER_TAG;
w->nw.tag = NSYNC_WAITER_TAG;
nsync_mu_semaphore_init (&w->sem);

View file

@ -13,7 +13,9 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#ifndef NSYNC_DEBUG
#ifdef MODE_DBG
#define NSYNC_DEBUG 1
#else
#define NSYNC_DEBUG 0
#endif

View file

@ -43,8 +43,6 @@
#include "third_party/nsync/time.h"
// clang-format off
/* futex() polyfill w/ sched_yield() fallback */
#define FUTEX_WAIT_BITS_ FUTEX_BITSET_MATCH_ANY
int _futex (atomic_int *, int, int, const struct timespec *, int *, int);
@ -161,6 +159,11 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare, struct timespec *
op |= FUTEX_PRIVATE_FLAG_;
}
LOCKTRACE ("futex(%t [%d], %s, %#x, %s) → ...",
w, atomic_load_explicit (w, memory_order_relaxed),
DescribeFutexOp (op), expect,
DescribeTimespec (0, timeout));
if (FUTEX_IS_SUPPORTED) {
if (IsWindows ()) {
// Windows 8 futexes don't support multiple processes :(
@ -199,7 +202,8 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare, struct timespec *
}
STRACE ("futex(%t [%d], %s, %#x, %s) → %s",
w, *w, DescribeFutexOp (op), expect,
w, atomic_load_explicit (w, memory_order_relaxed),
DescribeFutexOp (op), expect,
DescribeTimespec (0, timeout),
DescribeErrnoResult (rc));
@ -208,7 +212,7 @@ int nsync_futex_wait_ (atomic_int *w, int expect, char pshare, struct timespec *
int nsync_futex_wake_ (atomic_int *w, int count, char pshare) {
int e, rc, op, fop;
int wake (void *, int, int) asm ("_futex");
int wake (atomic_int *, int, int) asm ("_futex");
ASSERT (count == 1 || count == INT_MAX);
@ -245,8 +249,8 @@ int nsync_futex_wake_ (atomic_int *w, int count, char pshare) {
}
STRACE ("futex(%t [%d], %s, %d) → %s",
w, *w, DescribeFutexOp(op),
count, DescribeErrnoResult(rc));
w, atomic_load_explicit (w, memory_order_relaxed),
DescribeFutexOp(op), count, DescribeErrnoResult(rc));
return rc;
}

View file

@ -1,52 +0,0 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 tw=8 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.
*/
#include "libc/atomic.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/extend.internal.h"
#include "libc/macros.internal.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/sysv/consts/map.h"
#include "third_party/nsync/common.internal.h"
#include "third_party/nsync/malloc.internal.h"
// clang-format off
static char *nsync_malloc_endptr_;
static size_t nsync_malloc_total_;
static atomic_char nsync_malloc_lock_;
/* nsync_malloc_() is a malloc-like routine used by mutex and condition
variable code to allocate waiter structs. This allows *NSYNC mutexes
to be used by malloc(), by providing another, simpler allocator here.
The intent is that the implicit NULL value here can be overridden by
a client declaration that uses an initializer. */
void *nsync_malloc_ (size_t size) {
char *start;
size_t offset;
size = ROUNDUP (size, __BIGGEST_ALIGNMENT__);
while (atomic_exchange (&nsync_malloc_lock_, 1)) nsync_yield_ ();
offset = nsync_malloc_total_;
nsync_malloc_total_ += size;
start = (char *) kMemtrackNsyncStart;
if (!nsync_malloc_endptr_) nsync_malloc_endptr_ = start;
nsync_malloc_endptr_ =
_extend (start, nsync_malloc_total_, nsync_malloc_endptr_,
MAP_PRIVATE, kMemtrackNsyncStart + kMemtrackNsyncSize);
atomic_store_explicit (&nsync_malloc_lock_, 0, memory_order_relaxed);
return start + offset;
}

View file

@ -1,10 +0,0 @@
#ifndef NSYNC_MALLOC_INTERNAL_H_
#define NSYNC_MALLOC_INTERNAL_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
void *nsync_malloc_(size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* NSYNC_MALLOC_INTERNAL_H_ */