cosmopolitan/third_party/nsync/mu_semaphore.h
Justine Tunney 98c5847727
Fix fork waiter leak in nsync
This change fixes a bug where nsync waiter objects would leak. It'd mean
that long-running programs like runitd would run out of file descriptors
on NetBSD where waiter objects have ksem file descriptors. On other OSes
this bug is mostly harmless since the worst that can happen with a futex
is to leak a little bit of ram. The bug was caused because tib_nsync was
sneaking back in after the finalization code had cleared it. This change
refactors the thread exiting code to handle nsync teardown appropriately
and in making this change I found another issue, which is that user code
which is buggy, and tries to exit without joining joinable threads which
haven't been detached, would result in a deadlock. That doesn't sound so
bad, except the main thread is a joinable thread. So this deadlock would
be triggered in ways that put libc at fault. So we now auto-join threads
and libc will log a warning to --strace when that happens for any thread
2024-12-31 01:30:13 -08:00

29 lines
905 B
C

#ifndef NSYNC_SEM_H_
#define NSYNC_SEM_H_
#include "third_party/nsync/time.h"
COSMOPOLITAN_C_START_
typedef struct nsync_semaphore_s_ {
void *sem_space[3];
} nsync_semaphore;
/* Initialize *s; the initial value is 0. */
bool nsync_mu_semaphore_init(nsync_semaphore *s);
/* Destroy *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);
/* Wait until one of: the count of *s is non-zero, in which case
decrement *s and return 0; or abs_deadline expires, in which case
return ETIMEDOUT. */
errno_t nsync_mu_semaphore_p_with_deadline(nsync_semaphore *s, int clock,
nsync_time abs_deadline);
/* Ensure that the count of *s is at least 1. */
void nsync_mu_semaphore_v(nsync_semaphore *s);
COSMOPOLITAN_C_END_
#endif /* NSYNC_SEM_H_ */