mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-17 16:10:29 +00:00
This changes *NSYNC to allocate waiters on the stack so our locks don't need to depend on dynamic memory. This make our runtiem simpler, and it also fixes bugs with thread cancellation support.
90 lines
3.8 KiB
C
90 lines
3.8 KiB
C
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set et ft=c ts=8 tw=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 "third_party/nsync/mu_semaphore.h"
|
|
#include "libc/calls/cp.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "third_party/nsync/mu_semaphore.internal.h"
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
*NSYNC (Apache 2.0)\\n\
|
|
Copyright 2016 Google, Inc.\\n\
|
|
https://github.com/google/nsync\"");
|
|
// clang-format off
|
|
|
|
/* Initialize *s; the initial value is 0. */
|
|
void nsync_mu_semaphore_init (nsync_semaphore *s) {
|
|
if (IsXnuSilicon ()) {
|
|
return nsync_mu_semaphore_init_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_init_sem (s);
|
|
} else {
|
|
return nsync_mu_semaphore_init_futex (s);
|
|
}
|
|
}
|
|
|
|
/* Releases system resources associated with *s. */
|
|
void nsync_mu_semaphore_destroy (nsync_semaphore *s) {
|
|
if (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. */
|
|
errno_t nsync_mu_semaphore_p (nsync_semaphore *s) {
|
|
errno_t err;
|
|
BEGIN_CANCELLATION_POINT;
|
|
if (IsXnuSilicon ()) {
|
|
err = nsync_mu_semaphore_p_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
err = nsync_mu_semaphore_p_sem (s);
|
|
} else {
|
|
err = nsync_mu_semaphore_p_futex (s);
|
|
}
|
|
END_CANCELLATION_POINT;
|
|
return err;
|
|
}
|
|
|
|
/* 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, nsync_time abs_deadline) {
|
|
errno_t err;
|
|
BEGIN_CANCELLATION_POINT;
|
|
if (IsXnuSilicon ()) {
|
|
err = nsync_mu_semaphore_p_with_deadline_gcd (s, abs_deadline);
|
|
} else if (IsNetbsd ()) {
|
|
err = nsync_mu_semaphore_p_with_deadline_sem (s, abs_deadline);
|
|
} else {
|
|
err = nsync_mu_semaphore_p_with_deadline_futex (s, abs_deadline);
|
|
}
|
|
END_CANCELLATION_POINT;
|
|
return err;
|
|
}
|
|
|
|
/* Ensure that the count of *s is at least 1. */
|
|
void nsync_mu_semaphore_v (nsync_semaphore *s) {
|
|
if (IsXnuSilicon ()) {
|
|
return nsync_mu_semaphore_v_gcd (s);
|
|
} else if (IsNetbsd ()) {
|
|
return nsync_mu_semaphore_v_sem (s);
|
|
} else {
|
|
return nsync_mu_semaphore_v_futex (s);
|
|
}
|
|
}
|