mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-03 01:38:30 +00:00
Iterate more on recent changes
This commit is contained in:
parent
7138399f96
commit
d0ad2694ed
22 changed files with 90 additions and 158 deletions
39
third_party/nsync/common.c
vendored
39
third_party/nsync/common.c
vendored
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
2
third_party/nsync/common.internal.h
vendored
2
third_party/nsync/common.internal.h
vendored
|
@ -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;
|
||||
|
||||
/* ---------- */
|
||||
|
||||
|
|
9
third_party/nsync/mu_semaphore.c
vendored
9
third_party/nsync/mu_semaphore.c
vendored
|
@ -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,
|
||||
|
|
3
third_party/nsync/mu_semaphore.h
vendored
3
third_party/nsync/mu_semaphore.h
vendored
|
@ -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);
|
||||
|
||||
|
|
2
third_party/nsync/mu_semaphore.internal.h
vendored
2
third_party/nsync/mu_semaphore.internal.h
vendored
|
@ -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 *);
|
||||
|
|
5
third_party/nsync/mu_semaphore_gcd.c
vendored
5
third_party/nsync/mu_semaphore_gcd.c
vendored
|
@ -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,
|
||||
|
|
35
third_party/nsync/mu_semaphore_sem.c
vendored
35
third_party/nsync/mu_semaphore_sem.c
vendored
|
@ -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,
|
||||
|
|
2
third_party/nsync/panic.c
vendored
2
third_party/nsync/panic.c
vendored
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue