Skip to content

Commit

Permalink
Iterate more on recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Nov 11, 2023
1 parent 7138399 commit d0ad269
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 158 deletions.
2 changes: 2 additions & 0 deletions libc/intrin/describeframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ static const char *GetFrameName(int x) {
return "stack";
} else if (IsGfdsFrame(x)) {
return "g_fds";
} else if (IsNsyncFrame(x)) {
return "nsync";
} else if (IsZiposFrame(x)) {
return "zipos";
} else if (IsMemtrackFrame(x)) {
Expand Down
12 changes: 7 additions & 5 deletions libc/intrin/extend.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ static void *_mapframe_impl(void *p, int f) {
if (!rc) {
return p;
} else {
unassert(errno == ENOMEM);
return 0;
}
} else {
unassert(errno == ENOMEM);
return 0;
}
}
Expand Down Expand Up @@ -86,12 +84,16 @@ static void *_mapframe(void *p, int f) {
*/
void *_extend(void *p, size_t n, void *e, int f, intptr_t h) {
char *q;
unassert(!((uintptr_t)SHADOW(p) & (G - 1)));
unassert((uintptr_t)p + (G << kAsanScale) <= h);
#ifndef NDEBUG
if ((uintptr_t)SHADOW(p) & (G - 1)) notpossible;
if ((uintptr_t)p + (G << kAsanScale) > h) notpossible;
#endif
// TODO(jart): Make this spin less in non-ASAN mode.
for (q = e; q < ((char *)p + n); q += 8) {
if (!((uintptr_t)q & (G - 1))) {
unassert(q + G <= (char *)h);
#ifndef NDEBUG
if (q + G > (char *)h) notpossible;
#endif
if (!_mapframe(q, f)) return 0;
if (IsAsan()) {
if (!((uintptr_t)SHADOW(q) & (G - 1))) {
Expand Down
2 changes: 2 additions & 0 deletions libc/intrin/mmi_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "libc/runtime/memtrack.internal.h"
#include "libc/thread/thread.h"

// nsync depends on this non-nsync lock

extern pthread_mutex_t __mmi_lock_obj;

void __mmi_lock(void) {
Expand Down
2 changes: 2 additions & 0 deletions libc/intrin/wsarecv.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/describentoverlapped.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/internal.h"

__msabi extern typeof(WSARecv) *const __imp_WSARecv;
Expand Down
1 change: 1 addition & 0 deletions libc/intrin/wsarecvfrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/describentoverlapped.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/strace.internal.h"
#include "libc/nt/thunk/msabi.h"
#include "libc/nt/winsock.h"
Expand Down
6 changes: 6 additions & 0 deletions libc/nexgen32e/x86compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
#define _X86_CC_AVX2 0
#endif

#ifdef __F16C__
#define _X86_CC_F16C 1
#else
#define _X86_CC_F16C 0
#endif

#ifdef __AVXVNNI__
#define _X86_CC_AVXVNNI 1
#else
Expand Down
2 changes: 1 addition & 1 deletion libc/nexgen32e/x86feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#define X86_DTES64 1H, ECX, 2, 0
#define X86_ERMS 7H, EBX, 9, 0 /* broaadwell c. 2014 */
#define X86_EST 1H, ECX, 7, 0
#define X86_F16C 1H, ECX, 29, 0
#define X86_F16C 1H, ECX, 29, _X86_CC_F16C
#define X86_FDP_EXCPTN_ONLY 7H, EBX, 6, 0
#define X86_FLUSH_L1D 7H, EDX, 28, 0
#define X86_FMA 1H, ECX, 12, _X86_CC_FMA /* haswell c. 2013 */
Expand Down
32 changes: 17 additions & 15 deletions libc/proc/posix_spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@ static textwindows errno_t posix_spawn_nt_impl(
char errno_buf[30];
char oflags_buf[128];
char openmode_buf[15];
(void)errno_buf;
(void)oflags_buf;
(void)openmode_buf;
switch (a->action) {
case _POSIX_SPAWN_CLOSE:
err = spawnfds_close(&fds, a->fildes);
Expand Down Expand Up @@ -331,13 +328,12 @@ static textwindows errno_t posix_spawn_nt_impl(

// figure out flags
uint32_t dwCreationFlags = 0;
if (attrp && *attrp) {
if ((*attrp)->flags & POSIX_SPAWN_SETSID) {
dwCreationFlags |= kNtDetachedProcess;
}
if ((*attrp)->flags & POSIX_SPAWN_SETPGROUP) {
dwCreationFlags |= kNtCreateNewProcessGroup;
}
short flags = attrp && *attrp ? (*attrp)->flags : 0;
if (flags & POSIX_SPAWN_SETSID) {
dwCreationFlags |= kNtDetachedProcess;
}
if (flags & POSIX_SPAWN_SETPGROUP) {
dwCreationFlags |= kNtCreateNewProcessGroup;
}

// create process startinfo
Expand All @@ -361,8 +357,14 @@ static textwindows errno_t posix_spawn_nt_impl(
}

// inherit signal mask
sigset_t childmask;
char maskvar[6 + 21];
FormatUint64(stpcpy(maskvar, "_MASK="), sigmask);
if (flags & POSIX_SPAWN_SETSIGMASK) {
childmask = (*attrp)->sigmask;
} else {
childmask = sigmask;
}
FormatUint64(stpcpy(maskvar, "_MASK="), childmask);

// launch process
int rc = -1;
Expand Down Expand Up @@ -490,7 +492,7 @@ errno_t posix_spawn(int *pid, const char *path,
}
if (!(child = vfork())) {
can_clobber = true;
sigset_t *childmask;
sigset_t childmask;
bool lost_cloexec = 0;
struct sigaction dfl = {0};
short flags = attrp && *attrp ? (*attrp)->flags : 0;
Expand Down Expand Up @@ -599,11 +601,11 @@ errno_t posix_spawn(int *pid, const char *path,
fcntl(pfds[1], F_SETFD, FD_CLOEXEC);
}
if (flags & POSIX_SPAWN_SETSIGMASK) {
childmask = &(*attrp)->sigmask;
childmask = (*attrp)->sigmask;
} else {
childmask = &oldmask;
childmask = oldmask;
}
sigprocmask(SIG_SETMASK, childmask, 0);
sigprocmask(SIG_SETMASK, &childmask, 0);
if (!envp) envp = environ;
execve(path, argv, envp);
ChildFailed:
Expand Down
8 changes: 6 additions & 2 deletions libc/proc/wait4-nt.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ static textwindows int __proc_wait(int pid, int *wstatus, int options,

// check if killed or win32 error
if (wi) {
if (pr && !--pr->waiters && pr->status == PROC_UNDEAD) {
__proc_free(pr);
if (pr) {
if (!--pr->waiters && pr->status == PROC_UNDEAD) {
__proc_free(pr);
}
} else {
--__proc.waiters;
}
__proc_unlock();
if (wi == 1) {
Expand Down
7 changes: 7 additions & 0 deletions libc/runtime/memtrack.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ COSMOPOLITAN_C_START_
#define kMemtrackSize (0x1ffffffc0000 - kMemtrackStart)
#define kFixedmapStart 0x300000040000
#define kFixedmapSize (0x400000040000 - kFixedmapStart)
#define kMemtrackNsyncStart 0x6fc000040000
#define kMemtrackNsyncSize (0x6fcffffc0000 - kMemtrackNsyncStart)
#define kMemtrackFdsStart 0x6fe000040000
#define kMemtrackFdsSize (0x6feffffc0000 - kMemtrackFdsStart)
#define kMemtrackZiposStart 0x6fd000040000
Expand Down Expand Up @@ -114,6 +116,11 @@ forceinline pureconst bool IsGfdsFrame(int x) {
x <= (int)((kMemtrackFdsStart + kMemtrackFdsSize - 1) >> 16);
}

forceinline pureconst bool IsNsyncFrame(int x) {
return (int)(kMemtrackNsyncStart >> 16) <= x &&
x <= (int)((kMemtrackNsyncStart + kMemtrackNsyncSize - 1) >> 16);
}

forceinline pureconst bool IsZiposFrame(int x) {
return (int)(kMemtrackZiposStart >> 16) <= x &&
x <= (int)((kMemtrackZiposStart + kMemtrackZiposSize - 1) >> 16);
Expand Down
2 changes: 1 addition & 1 deletion libc/runtime/memtrack64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@
6f900000-6f9fffff 64gb free
6fa00000-6fafffff 64gb free
6fb00000-6fbfffff 64gb free
6fc00004-6fcffffb 64gb free
6fc00004-6fcffffb 64gb nsync
6fd00004-6fdffffb 64gb zipos
6fe00004-6feffffb 64gb g_fds
6ff00004-70000003 64gb free
Expand Down
3 changes: 3 additions & 0 deletions test/libc/calls/signal_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/ucontext.h"
#include "libc/dce.h"
#include "libc/log/check.h"
Expand All @@ -33,6 +34,8 @@ void OnUsr1(int sig) {
}

void SetUpOnce(void) {
sigset_t ss;
sigprocmask(SIG_SETMASK, 0, &ss);
ASSERT_SYS(0, 0, pledge("stdio proc", 0));
}

Expand Down
69 changes: 0 additions & 69 deletions test/libc/thread/mu_semaphore_sem_test.c

This file was deleted.

39 changes: 18 additions & 21 deletions third_party/nsync/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
nsync_panic_ ("out of memory\n");
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;
}
} 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");
}
malloc.e = _extend (malloc.p, malloc.used + size, malloc.e, MAP_PRIVATE,
kMemtrackNsyncStart + kMemtrackNsyncSize);
if (!malloc.e) {
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;
}

Expand Down
2 changes: 1 addition & 1 deletion third_party/nsync/common.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/* ---------- */

Expand Down
9 changes: 0 additions & 9 deletions third_party/nsync/mu_semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions third_party/nsync/mu_semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading

0 comments on commit d0ad269

Please sign in to comment.