mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-24 06:12:27 +00:00
Clean up some sleep code
This commit is contained in:
parent
9849b4c7ba
commit
672ccda37c
35 changed files with 310 additions and 598 deletions
46
third_party/nsync/testing/array.c
vendored
46
third_party/nsync/testing/array.c
vendored
|
@ -1,46 +0,0 @@
|
|||
/*-*- 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 "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/testing/array.h"
|
||||
// clang-format off
|
||||
|
||||
void a_ensure_ (void *v, int delta, int sz) {
|
||||
typedef A_TYPE (void *) a_void_ptr;
|
||||
a_void_ptr *a = (a_void_ptr *) v;
|
||||
int omax = a->h_.max_;
|
||||
if (omax < 0) {
|
||||
omax = -omax;
|
||||
}
|
||||
if (a->h_.len_ + delta > omax) {
|
||||
int nmax = a->h_.len_ + delta;
|
||||
void *na;
|
||||
if (nmax < omax * 2) {
|
||||
nmax = omax * 2;
|
||||
}
|
||||
if (a->h_.max_ <= 0) {
|
||||
na = malloc (nmax * sz);
|
||||
memcpy (na, a->a_, omax*sz);
|
||||
} else {
|
||||
na = realloc (a->a_, nmax*sz);
|
||||
}
|
||||
memset (omax *sz + (char *)na, 0, (nmax - omax) * sz);
|
||||
a->a_ = (void **) na;
|
||||
a->h_.max_ = nmax;
|
||||
}
|
||||
}
|
61
third_party/nsync/testing/array.h
vendored
61
third_party/nsync/testing/array.h
vendored
|
@ -1,61 +0,0 @@
|
|||
#ifndef NSYNC_TESTING_ARRAY_H_
|
||||
#define NSYNC_TESTING_ARRAY_H_
|
||||
/* clang-format off */
|
||||
|
||||
/* Return the number of elements in a C array a.
|
||||
A constant expression if a is a constant expression. */
|
||||
#define NELEM(a) ((int) (sizeof (a) / sizeof (a[0])))
|
||||
|
||||
|
||||
/* A dynamic array */
|
||||
|
||||
/* internal routines */
|
||||
void a_ensure_ (void *v, int delta, int sz);
|
||||
|
||||
/* The following struct prefixes all array values. */
|
||||
struct a_hdr_ {
|
||||
int len_; /* The number of elements considererf to be part of the array. */
|
||||
int max_; /* abs(max_) is the number of elements that have been set aside
|
||||
to store elements of the array. If max_ < 0, the space
|
||||
has been allocated statically. If max_ > 0, the space
|
||||
has been allocated dynamically. */
|
||||
};
|
||||
|
||||
/* A type declaration for an array of "type". Example: typedef A_TYPE (int) array_of_int; */
|
||||
#define A_TYPE(type) struct { struct a_hdr_ h_; type *a_; }
|
||||
|
||||
/* The empty array initializer. Empty arrays can also be initialized by zeroing. */
|
||||
#define A_EMPTY { { 0, 0 }, NULL }
|
||||
|
||||
/* A static initializer using the contents of C array "data".
|
||||
Example:
|
||||
static int some_ints[] = { 7, 11, 13 };
|
||||
static array_of_int x = A_INIT (some_ints); */
|
||||
#define A_INIT(data) { { NELEM (data), -NELEM (data) }, data }
|
||||
|
||||
/* Element i of the array *a (l-value or r-value) */
|
||||
#define A(a,i) ((a)->a_[i])
|
||||
|
||||
/* Append an entry to array *a, and yield it as an l-value. */
|
||||
#define A_PUSH(a) (*(a_ensure_ ((a), 1, sizeof ((a)->a_[0])), &(a)->a_[(a)->h_.len_++]))
|
||||
|
||||
/* Return the length of array *a. */
|
||||
#define A_LEN(a) ((a)->h_.len_)
|
||||
|
||||
/* Set the length of array *a to l. Requires that 0 <= l <= A_LEN (a). */
|
||||
#define A_SET_LEN(a, l) do { if (0 <= (l) && (l) <= (a)->h_.len_) { \
|
||||
(a)->h_.len_ = (l); } else { \
|
||||
*(volatile int *)0 = 0; } } while (0)
|
||||
|
||||
/* Reduce the length of array *a by n. Requires that 0 <= n <= A_LEN (a). */
|
||||
#define A_DISCARD(a, n) do { if (0 <= (n) && (n) <= (a)->h_.len_) { \
|
||||
(a)->h_.len_ -= (n); } else { \
|
||||
*(volatile int *)0 = 0; } } while (0)
|
||||
|
||||
/* Deallocate and disassociate any storage associated with *a, and make *a
|
||||
empty. */
|
||||
#define A_FREE(a) do { if ((a)->h_.max_ > 0) { free ((void *) (a)->a_); } \
|
||||
(a)->a_ = NULL; (a)->h_.max_ = 0; (a)->h_.len_ = 0; \
|
||||
} while (0)
|
||||
|
||||
#endif /*NSYNC_TESTING_ARRAY_H_*/
|
|
@ -17,11 +17,11 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/heap.internal.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/testing/array.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/heap.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
|
|
2
third_party/nsync/testing/dll_test.c
vendored
2
third_party/nsync/testing/dll_test.c
vendored
|
@ -18,8 +18,8 @@
|
|||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/dll.h"
|
||||
#include "third_party/nsync/testing/array.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
// clang-format off
|
||||
|
|
60
third_party/nsync/testing/heap.h
vendored
60
third_party/nsync/testing/heap.h
vendored
|
@ -1,60 +0,0 @@
|
|||
#ifndef NSYNC_TESTING_HEAP_H_
|
||||
#define NSYNC_TESTING_HEAP_H_
|
||||
/* clang-format off */
|
||||
|
||||
/* A heap.
|
||||
Optionally, elements may have storage for the index to allow deletions from
|
||||
arbitrary elements. A "set" operation sets the field. Use heap_no_set when
|
||||
no field is available.
|
||||
|
||||
Let:
|
||||
set (e,i) sets the index field of the element e to i
|
||||
lt (e0, e1) returns whether element e0 < e1
|
||||
|
||||
If
|
||||
"a" is an array,
|
||||
"n" is is its length,
|
||||
then
|
||||
To add an element e:
|
||||
ensure there are n+1 elements in a[]
|
||||
heap_add (a, n, lt, set, e); // modifies n
|
||||
To remove element i:
|
||||
heap_remove (a, n, lt, set, i); // modifies n
|
||||
To replace element i with element e:
|
||||
heap_adjust (a, n, lt, set, i, e);
|
||||
*/
|
||||
|
||||
|
||||
#define h_up_(i) (((i)-1) >> 1)
|
||||
#define h_down_(i) (((i)<<1) + 1)
|
||||
|
||||
#define h_updownall_(up,a,n,i,lt,set,v,s) \
|
||||
do { \
|
||||
int i_ = (i); \
|
||||
int n_ = (n); \
|
||||
int j_; \
|
||||
if (up) { \
|
||||
for (; i_!=0 && ((j_ = h_up_ (i_)), lt ((v), (a)[j_])); i_ = j_) { \
|
||||
(a)[i_] = (a)[j_]; \
|
||||
set ((a)[i_], i_); \
|
||||
} \
|
||||
} else { \
|
||||
for (; (j_ = h_down_ (i_)) < n_ && ((j_ += (j_+1 < n_ && \
|
||||
lt ((a)[j_+1], (a)[j_]))), lt ((a)[j_], (v))); i_ = j_) { \
|
||||
(a)[i_] = (a)[j_]; \
|
||||
set ((a)[i_], i_); \
|
||||
} \
|
||||
} \
|
||||
s; \
|
||||
} while (0)
|
||||
|
||||
#define heap_no_set(a,b) ((void)0)
|
||||
|
||||
#define heap_add(a,n,lt,set,v) h_updownall_ (1, (a), 0, \
|
||||
(n), lt, set, (v), ((a)[i_]=(v), set ((a)[i_], i_), (n)++))
|
||||
#define heap_remove(a,n,lt,set,i) h_updownall_ (lt ((a)[n_], (a)[i_]), (a), --(n), \
|
||||
(i), lt, set, (a)[n_], ((a)[i_]=(a)[n_], set ((a)[i_], i_)))
|
||||
#define heap_adjust(a,n,lt,set,i,v) h_updownall_ (lt ((v), (a)[i_]), (a), (n), \
|
||||
(i), lt, set, (v), ((a)[i_]=(v), set ((a)[i_], i_)))
|
||||
|
||||
#endif /*NSYNC_TESTING_HEAP_H_*/
|
|
@ -17,11 +17,11 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/heap.internal.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#include "third_party/nsync/mu_wait.h"
|
||||
#include "third_party/nsync/testing/array.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/heap.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
#include "third_party/nsync/testing/time_extra.h"
|
||||
|
|
12
third_party/nsync/testing/testing.mk
vendored
12
third_party/nsync/testing/testing.mk
vendored
|
@ -12,8 +12,8 @@ THIRD_PARTY_NSYNC_TESTING_SRCS_TEST = $(filter %_test.c,$(THIRD_PARTY_NSYNC_TEST
|
|||
THIRD_PARTY_NSYNC_TESTING_OBJS = $(THIRD_PARTY_NSYNC_TESTING_SRCS:%.c=o/$(MODE)/%.o)
|
||||
THIRD_PARTY_NSYNC_TESTING_COMS = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.com)
|
||||
THIRD_PARTY_NSYNC_TESTING_BINS = $(THIRD_PARTY_NSYNC_TESTING_COMS) $(THIRD_PARTY_NSYNC_TESTING_COMS:%=%.dbg)
|
||||
# THIRD_PARTY_NSYNC_TESTING_TESTS = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
# THIRD_PARTY_NSYNC_TESTING_CHECKS = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
THIRD_PARTY_NSYNC_TESTING_TESTS_ = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
THIRD_PARTY_NSYNC_TESTING_CHECKS_ = $(THIRD_PARTY_NSYNC_TESTING_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
THIRD_PARTY_NSYNC_TESTING_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
|
@ -57,10 +57,10 @@ o/$(MODE)/third_party/nsync/testing/mu_test.com.runs: private QUOTA = -C64
|
|||
|
||||
.PHONY: o/$(MODE)/third_party/nsync/testing
|
||||
o/$(MODE)/third_party/nsync/testing: \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_BINS)
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS_) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_BINS_)
|
||||
|
||||
.PHONY: o/$(MODE)/third_party/nsync/test
|
||||
o/$(MODE)/third_party/nsync/test: \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_TESTS)
|
||||
$(THIRD_PARTY_NSYNC_TESTING_CHECKS_) \
|
||||
$(THIRD_PARTY_NSYNC_TESTING_TESTS_)
|
||||
|
|
14
third_party/nsync/testing/time_extra.c
vendored
14
third_party/nsync/testing/time_extra.c
vendored
|
@ -41,20 +41,6 @@ char *nsync_time_str (nsync_time t, int decimals) {
|
|||
return (smprintf ("%.*f%s", decimals, s/scale[i].multiplier, scale[i].suffix));
|
||||
}
|
||||
|
||||
int nsync_time_sleep_until (nsync_time abs_deadline) {
|
||||
int result = 0;
|
||||
nsync_time now;
|
||||
now = nsync_time_now ();
|
||||
if (nsync_time_cmp (abs_deadline, now) > 0) {
|
||||
nsync_time remaining;
|
||||
remaining = nsync_time_sleep (nsync_time_sub (abs_deadline, now));
|
||||
if (nsync_time_cmp (remaining, nsync_time_zero) > 0) {
|
||||
result = EINTR;
|
||||
}
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
|
||||
double nsync_time_to_dbl (nsync_time t) {
|
||||
return (((double) NSYNC_TIME_SEC (t)) + ((double) NSYNC_TIME_NSEC (t) * 1e-9));
|
||||
}
|
||||
|
|
4
third_party/nsync/testing/time_extra.h
vendored
4
third_party/nsync/testing/time_extra.h
vendored
|
@ -6,10 +6,6 @@
|
|||
"decimals" decimal places. */
|
||||
char *nsync_time_str(nsync_time t, int decimals);
|
||||
|
||||
/* Sleep until the specified time. Returns 0 on success, and EINTR
|
||||
if the call was interrupted. */
|
||||
int nsync_time_sleep_until(nsync_time abs_deadline);
|
||||
|
||||
/* Return t as a double. */
|
||||
double nsync_time_to_dbl(nsync_time t);
|
||||
|
||||
|
|
2
third_party/nsync/testing/wait_test.c
vendored
2
third_party/nsync/testing/wait_test.c
vendored
|
@ -16,9 +16,9 @@
|
|||
│ limitations under the License. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/str/str.h"
|
||||
#include "third_party/nsync/array.internal.h"
|
||||
#include "third_party/nsync/counter.h"
|
||||
#include "third_party/nsync/note.h"
|
||||
#include "third_party/nsync/testing/array.h"
|
||||
#include "third_party/nsync/testing/closure.h"
|
||||
#include "third_party/nsync/testing/smprintf.h"
|
||||
#include "third_party/nsync/testing/testing.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue