mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 06:53:33 +00:00
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
145 lines
5.9 KiB
C
145 lines
5.9 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set noet ft=c ts=8 sw=8 fenc=utf-8 :vi │
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2016 Google Inc. │
|
|
│ │
|
|
│ Licensed under the Apache License, Version 2.0 (the "License"); │
|
|
│ you may not use this file except in compliance with the License. │
|
|
│ You may obtain a copy of the License at │
|
|
│ │
|
|
│ http://www.apache.org/licenses/LICENSE-2.0 │
|
|
│ │
|
|
│ Unless required by applicable law or agreed to in writing, software │
|
|
│ distributed under the License is distributed on an "AS IS" BASIS, │
|
|
│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
|
|
│ See the License for the specific language governing permissions and │
|
|
│ limitations under the License. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/assert.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/str/str.h"
|
|
#include "libc/thread/thread.h"
|
|
#include "third_party/nsync/atomic.h"
|
|
#include "third_party/nsync/atomic.internal.h"
|
|
#include "libc/cosmo.h"
|
|
#include "libc/calls/struct/timespec.h"
|
|
#include "libc/cosmo.h"
|
|
#include "third_party/nsync/mu_semaphore.internal.h"
|
|
|
|
/**
|
|
* @fileoverview Semaphores w/ Linux Futexes API.
|
|
*/
|
|
|
|
#define ASSERT(x) unassert(x)
|
|
|
|
/* Check that atomic operations on nsync_atomic_uint32_ can be applied to int. */
|
|
static const int assert_int_size = 1 /
|
|
(sizeof (assert_int_size) == sizeof (uint32_t) &&
|
|
sizeof (nsync_atomic_uint32_) == sizeof (uint32_t));
|
|
|
|
struct futex {
|
|
int i; /* lo half=count; hi half=waiter count */
|
|
};
|
|
|
|
static nsync_semaphore *sem_big_enough_for_futex = (nsync_semaphore *) (uintptr_t)(1 /
|
|
(sizeof (struct futex) <= sizeof (*sem_big_enough_for_futex)));
|
|
|
|
/* Initialize *s; the initial value is 0. */
|
|
bool nsync_mu_semaphore_init_futex (nsync_semaphore *s) {
|
|
struct futex *f = (struct futex *) s;
|
|
f->i = 0;
|
|
return true;
|
|
}
|
|
|
|
void nsync_mu_semaphore_destroy_futex (nsync_semaphore *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,
|
|
cancellation will occur by unwinding cleanup handlers pushed to the stack. */
|
|
errno_t nsync_mu_semaphore_p_futex (nsync_semaphore *s) {
|
|
struct futex *f = (struct futex *) s;
|
|
int i;
|
|
errno_t result = 0;
|
|
do {
|
|
i = ATM_LOAD ((nsync_atomic_uint32_ *) &f->i);
|
|
if (i == 0) {
|
|
int futex_result;
|
|
futex_result = -cosmo_futex_wait (
|
|
(atomic_int *)&f->i, i,
|
|
PTHREAD_PROCESS_PRIVATE, 0, 0);
|
|
ASSERT (futex_result == 0 ||
|
|
futex_result == EINTR ||
|
|
futex_result == EAGAIN ||
|
|
futex_result == ECANCELED ||
|
|
futex_result == EWOULDBLOCK);
|
|
if (futex_result == ECANCELED) {
|
|
result = ECANCELED;
|
|
}
|
|
}
|
|
} while (result == 0 && (i == 0 ||
|
|
!atomic_compare_exchange_weak_explicit (
|
|
(nsync_atomic_uint32_ *) &f->i, &i, i-1,
|
|
memory_order_acquire, memory_order_relaxed)));
|
|
return result;
|
|
}
|
|
|
|
/* Like nsync_mu_semaphore_p() this waits for the count of *s to exceed 0,
|
|
while additionally supporting a time parameter specifying at what point
|
|
in the future ETIMEDOUT should be returned, if neither cancellation, or
|
|
semaphore release happens. */
|
|
errno_t nsync_mu_semaphore_p_with_deadline_futex (nsync_semaphore *s, int clock, nsync_time abs_deadline) {
|
|
struct futex *f = (struct futex *)s;
|
|
int i;
|
|
int result = 0;
|
|
do {
|
|
i = ATM_LOAD ((nsync_atomic_uint32_ *) &f->i);
|
|
if (i == 0) {
|
|
int futex_result;
|
|
struct timespec ts_buf;
|
|
const struct timespec *ts = NULL;
|
|
if (nsync_time_cmp (abs_deadline, nsync_time_no_deadline) != 0) {
|
|
bzero (&ts_buf, sizeof (ts_buf));
|
|
ts_buf.tv_sec = NSYNC_TIME_SEC (abs_deadline);
|
|
ts_buf.tv_nsec = NSYNC_TIME_NSEC (abs_deadline);
|
|
ts = &ts_buf;
|
|
}
|
|
futex_result = cosmo_futex_wait ((atomic_int *)&f->i, i,
|
|
PTHREAD_PROCESS_PRIVATE,
|
|
clock, ts);
|
|
ASSERT (futex_result == 0 ||
|
|
futex_result == -EINTR ||
|
|
futex_result == -EAGAIN ||
|
|
futex_result == -ECANCELED ||
|
|
futex_result == -ETIMEDOUT ||
|
|
futex_result == -EWOULDBLOCK);
|
|
/* Some systems don't wait as long as they are told. */
|
|
if (futex_result == -ETIMEDOUT) {
|
|
nsync_time now;
|
|
if (clock_gettime (clock, &now))
|
|
result = EINVAL;
|
|
if (nsync_time_cmp (now, abs_deadline) >= 0)
|
|
result = ETIMEDOUT;
|
|
}
|
|
if (futex_result == -ECANCELED) {
|
|
result = ECANCELED;
|
|
}
|
|
}
|
|
} while (result == 0 && (i == 0 ||
|
|
!atomic_compare_exchange_weak_explicit (
|
|
(nsync_atomic_uint32_ *) &f->i, &i, i-1,
|
|
memory_order_acquire, memory_order_relaxed)));
|
|
return (result);
|
|
}
|
|
|
|
/* Ensure that the count of *s is at least 1. */
|
|
void nsync_mu_semaphore_v_futex (nsync_semaphore *s) {
|
|
struct futex *f = (struct futex *) s;
|
|
uint32_t old_value = ATM_LOAD ((nsync_atomic_uint32_ *) &f->i);
|
|
while (!atomic_compare_exchange_weak_explicit (
|
|
(nsync_atomic_uint32_ *) &f->i, &old_value, old_value+1,
|
|
memory_order_release, memory_order_relaxed)) {
|
|
}
|
|
ASSERT (cosmo_futex_wake ((atomic_int *)&f->i, 1, PTHREAD_PROCESS_PRIVATE) >= 0);
|
|
}
|