Iterate more on recent changes

This commit is contained in:
Justine Tunney 2023-11-10 22:55:33 -08:00
parent 7138399f96
commit d0ad2694ed
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
22 changed files with 90 additions and 158 deletions

View file

@ -19,7 +19,12 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/extend.internal.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/nt/memory.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
#include "libc/thread/tls.h"
@ -149,36 +154,28 @@ waiter *nsync_dll_waiter_samecond_ (struct Dll *e) {
/* -------------------------------- */
#define kMallocBlockSize 16384
static struct {
nsync_atomic_uint32_ mu;
size_t used;
char *block;
char *p, *e;
} malloc;
static void *nsync_malloc (size_t size) {
void *res;
ASSERT (size <= kMallocBlockSize);
if (IsWindows ()) {
res = HeapAlloc (GetProcessHeap (), 0, size);
if (!res) {
void *res = 0;
nsync_spin_test_and_set_ (&malloc.mu, 1, 1, 0);
if (malloc.p + malloc.used + size > malloc.e) {
if (!malloc.p) {
malloc.p = malloc.e = (char *)kMemtrackNsyncStart;
}
malloc.e = _extend (malloc.p, malloc.used + size, malloc.e, MAP_PRIVATE,
kMemtrackNsyncStart + kMemtrackNsyncSize);
if (!malloc.e) {
nsync_panic_ ("out of memory\n");
}
} else {
nsync_spin_test_and_set_ (&malloc.mu, 1, 1, 0);
if (!malloc.block || malloc.used + size > kMallocBlockSize) {
malloc.used = 0;
malloc.block = __sys_mmap (0, kMallocBlockSize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0, 0);
if (malloc.block == MAP_FAILED) {
nsync_panic_ ("out of memory\n");
}
}
res = malloc.block + malloc.used;
malloc.used = (malloc.used + size + 15) & -16;
ATM_STORE_REL (&malloc.mu, 0);
}
res = malloc.p + malloc.used;
malloc.used = (malloc.used + size + 15) & -16;
ATM_STORE_REL (&malloc.mu, 0);
return res;
}

View file

@ -40,7 +40,7 @@ uint32_t nsync_spin_test_and_set_(nsync_atomic_uint32_ *w, uint32_t test,
uint32_t set, uint32_t clear);
/* Abort after printing the nul-temrinated string s[]. */
void nsync_panic_(const char *s);
void nsync_panic_(const char *s) wontreturn;
/* ---------- */

View file

@ -42,15 +42,6 @@ void nsync_mu_semaphore_init (nsync_semaphore *s) {
}
}
/* Releases system resources associated with *s. */
void nsync_mu_semaphore_destroy (nsync_semaphore *s) {
if (PREFER_GCD_OVER_ULOCK && IsXnuSilicon ()) {
return nsync_mu_semaphore_destroy_gcd (s);
} else if (IsNetbsd ()) {
return nsync_mu_semaphore_destroy_sem (s);
}
}
/* Wait until the count of *s exceeds 0, and decrement it. If POSIX cancellations
are currently disabled by the thread, then this function always succeeds. When
they're enabled in MASKED mode, this function may return ECANCELED. Otherwise,

View file

@ -11,9 +11,6 @@ typedef struct nsync_semaphore_s_ {
/* Initialize *s; the initial value is 0. */
void nsync_mu_semaphore_init(nsync_semaphore *s);
/* Releases system resources associated with *s. */
void nsync_mu_semaphore_destroy(nsync_semaphore *s);
/* Wait until the count of *s exceeds 0, and decrement it. */
errno_t nsync_mu_semaphore_p(nsync_semaphore *s);

View file

@ -11,13 +11,11 @@ errno_t nsync_mu_semaphore_p_with_deadline_futex(nsync_semaphore *, nsync_time);
void nsync_mu_semaphore_v_futex(nsync_semaphore *);
void nsync_mu_semaphore_init_sem(nsync_semaphore *);
void nsync_mu_semaphore_destroy_sem(nsync_semaphore *);
errno_t nsync_mu_semaphore_p_sem(nsync_semaphore *);
errno_t nsync_mu_semaphore_p_with_deadline_sem(nsync_semaphore *, nsync_time);
void nsync_mu_semaphore_v_sem(nsync_semaphore *);
void nsync_mu_semaphore_init_gcd(nsync_semaphore *);
void nsync_mu_semaphore_destroy_gcd(nsync_semaphore *);
errno_t nsync_mu_semaphore_p_gcd(nsync_semaphore *);
errno_t nsync_mu_semaphore_p_with_deadline_gcd(nsync_semaphore *, nsync_time);
void nsync_mu_semaphore_v_gcd(nsync_semaphore *);

View file

@ -90,11 +90,6 @@ void nsync_mu_semaphore_init_gcd (nsync_semaphore *s) {
*(dispatch_semaphore_t *)s = dispatch_semaphore_create (0);
}
/* Releases system resources associated with *s. */
void nsync_mu_semaphore_destroy_gcd (nsync_semaphore *s) {
dispatch_release (*(dispatch_semaphore_t *)s);
}
/* Wait until the count of *s exceeds 0, and decrement it. If POSIX cancellations
are currently disabled by the thread, then this function always succeeds. When
they're enabled in MASKED mode, this function may return ECANCELED. Otherwise,

View file

@ -55,12 +55,24 @@ static struct {
static nsync_semaphore *sem_big_enough_for_sem = (nsync_semaphore *) (uintptr_t)(1 /
(sizeof (struct sem) <= sizeof (*sem_big_enough_for_sem)));
static void nsync_mu_semaphore_sem_create (struct sem *f) {
int lol;
f->id = 0;
ASSERT (!sys_sem_init (0, &f->id));
if ((lol = __sys_fcntl (f->id, F_DUPFD_CLOEXEC, 50)) >= 50) {
sys_close (f->id);
f->id = lol;
}
}
static void nsync_mu_semaphore_sem_fork_child (void) {
struct Dll *e;
struct sem *f;
for (e = dll_first (g_sems.list); e; e = dll_next (g_sems.list, e)) {
sys_close (SEM_CONTAINER (e)->id);
f = SEM_CONTAINER (e);
sys_close (f->id);
nsync_mu_semaphore_sem_create (f);
}
g_sems.list = 0; /* list memory is on dead thread stacks */
(void) pthread_spin_init (&g_sems.lock, 0);
}
@ -70,14 +82,8 @@ static void nsync_mu_semaphore_sem_init (void) {
/* Initialize *s; the initial value is 0. */
void nsync_mu_semaphore_init_sem (nsync_semaphore *s) {
int lol;
struct sem *f = (struct sem *) s;
f->id = 0;
ASSERT (!sys_sem_init (0, &f->id));
if ((lol = __sys_fcntl (f->id, F_DUPFD_CLOEXEC, 50)) >= 50) {
sys_close (f->id);
f->id = lol;
}
nsync_mu_semaphore_sem_create (f);
cosmo_once (&g_sems.once, nsync_mu_semaphore_sem_init);
pthread_spin_lock (&g_sems.lock);
dll_init (&f->list);
@ -86,17 +92,6 @@ void nsync_mu_semaphore_init_sem (nsync_semaphore *s) {
STRACE ("sem_init(0, [%ld]) → 0", f->id);
}
/* Releases system resources associated with *s. */
void nsync_mu_semaphore_destroy_sem (nsync_semaphore *s) {
int rc;
struct sem *f = (struct sem *) s;
ASSERT (!(rc = sys_sem_destroy (f->id)));
pthread_spin_lock (&g_sems.lock);
dll_remove (&g_sems.list, &f->list);
pthread_spin_unlock (&g_sems.lock);
STRACE ("sem_destroy(%ld) → %d", f->id, rc);
}
/* Wait until the count of *s exceeds 0, and decrement it. If POSIX cancellations
are currently disabled by the thread, then this function always succeeds. When
they're enabled in MASKED mode, this function may return ECANCELED. Otherwise,

View file

@ -25,7 +25,7 @@
/* Aborts after printing the nul-terminated string s[]. */
void nsync_panic_ (const char *s) {
tinyprint(2, "error: nsync panic: ", s, "\n",
tinyprint(2, "error: nsync panic: ", s,
"cosmoaddr2line ", program_invocation_name, " ",
DescribeBacktrace (__builtin_frame_address (0)), "\n",
NULL);