Skip to content

Commit 3f0bcdc

Browse files
committed
Improve cancellations, randomness, and time
- Exhaustively document cancellation points - Rename SIGCANCEL to SIGTHR just like BSDs - Further improve POSIX thread cancellations - Ensure asynchronous cancellations work correctly - Elevate the quality of getrandom() and getentropy() - Make futexes cancel correctly on OpenBSD 6.x and 7.x - Add reboot.com and shutdown.com to examples directory - Remove underscore prefix from awesome timespec_*() APIs - Create assertions that help verify our cancellation points - Remove bad timespec APIs (cmp generalizes eq/ne/gt/gte/lt/lte)
1 parent 0d7c265 commit 3f0bcdc

File tree

173 files changed

+1598
-781
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+1598
-781
lines changed

examples/clock.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ int main(int argc, char *argv[]) {
2020
volatile unsigned long x;
2121
struct timespec now, start, next, interval;
2222
printf("hammering the cpu...\n");
23-
next = start = _timespec_mono();
24-
interval = _timespec_frommillis(500);
25-
next = _timespec_add(next, interval);
23+
next = start = timespec_mono();
24+
interval = timespec_frommillis(500);
25+
next = timespec_add(next, interval);
2626
for (;;) {
2727
for (i = 0;; ++i) {
2828
x *= 7;
2929
if (!(i % 256)) {
30-
now = _timespec_mono();
31-
if (_timespec_gte(now, next)) {
30+
now = timespec_mono();
31+
if (timespec_cmp(now, next) >= 0) {
3232
break;
3333
}
3434
}
3535
}
36-
next = _timespec_add(next, interval);
36+
next = timespec_add(next, interval);
3737
printf("consumed %10g seconds monotonic time and %10g seconds cpu time\n",
38-
_timespec_tonanos(_timespec_sub(now, start)) / 1000000000.,
38+
timespec_tonanos(timespec_sub(now, start)) / 1000000000.,
3939
(double)clock() / CLOCKS_PER_SEC);
4040
}
4141
}

examples/clock_getres.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void show(int clock) {
2828
}
2929
shown[n++] = clock;
3030
if (clock_getres(clock, &ts) != -1) {
31-
kprintf("%s %'ld ns\n", DescribeClockName(clock), _timespec_tonanos(ts));
31+
kprintf("%s %'ld ns\n", DescribeClockName(clock), timespec_tonanos(ts));
3232
} else {
3333
kprintf("%s %s\n", DescribeClockName(clock), _strerrno(errno));
3434
}

examples/reboot.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#if 0
2+
/*─────────────────────────────────────────────────────────────────╗
3+
│ To the extent possible under law, Justine Tunney has waived │
4+
│ all copyright and related or neighboring rights to this file, │
5+
│ as it is written in the following disclaimers: │
6+
│ • http://unlicense.org/ │
7+
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
8+
╚─────────────────────────────────────────────────────────────────*/
9+
#endif
10+
#include "libc/calls/calls.h"
11+
#include "libc/runtime/runtime.h"
12+
#include "libc/stdio/stdio.h"
13+
#include "libc/str/str.h"
14+
#include "libc/sysv/consts/reboot.h"
15+
16+
int main(int argc, char *argv[]) {
17+
char line[8] = {0};
18+
if (argc > 1 && !strcmp(argv[1], "-y")) {
19+
line[0] = 'y';
20+
} else {
21+
printf("reboot your computer? yes or no [no] ");
22+
fflush(stdout);
23+
fgets(line, sizeof(line), stdin);
24+
}
25+
if (line[0] == 'y' || line[0] == 'Y') {
26+
if (reboot(RB_AUTOBOOT)) {
27+
printf("system is rebooting...\n");
28+
exit(0);
29+
} else {
30+
perror("reboot");
31+
exit(1);
32+
}
33+
} else if (line[0] == 'n' || line[0] == 'N') {
34+
exit(0);
35+
} else {
36+
printf("error: unrecognized response\n");
37+
exit(2);
38+
}
39+
}

examples/shutdown.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#if 0
2+
/*─────────────────────────────────────────────────────────────────╗
3+
│ To the extent possible under law, Justine Tunney has waived │
4+
│ all copyright and related or neighboring rights to this file, │
5+
│ as it is written in the following disclaimers: │
6+
│ • http://unlicense.org/ │
7+
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
8+
╚─────────────────────────────────────────────────────────────────*/
9+
#endif
10+
#include "libc/calls/calls.h"
11+
#include "libc/runtime/runtime.h"
12+
#include "libc/stdio/stdio.h"
13+
#include "libc/str/str.h"
14+
#include "libc/sysv/consts/reboot.h"
15+
16+
int main(int argc, char *argv[]) {
17+
char line[8] = {0};
18+
if (argc > 1 && !strcmp(argv[1], "-y")) {
19+
line[0] = 'y';
20+
} else {
21+
printf("shutdown your computer? yes or no [no] ");
22+
fflush(stdout);
23+
fgets(line, sizeof(line), stdin);
24+
}
25+
if (line[0] == 'y' || line[0] == 'Y') {
26+
if (reboot(RB_POWER_OFF)) {
27+
printf("system is shutting down...\n");
28+
exit(0);
29+
} else {
30+
perror("reboot");
31+
exit(1);
32+
}
33+
} else if (line[0] == 'n' || line[0] == 'N') {
34+
exit(0);
35+
} else {
36+
printf("error: unrecognized response\n");
37+
exit(2);
38+
}
39+
}

libc/calls/_timespec_eq.c

Lines changed: 0 additions & 26 deletions
This file was deleted.

libc/calls/_timespec_gt.c

Lines changed: 0 additions & 34 deletions
This file was deleted.

libc/calls/_timespec_gte.c

Lines changed: 0 additions & 28 deletions
This file was deleted.

libc/calls/_timeval_eq.c

Lines changed: 0 additions & 26 deletions
This file was deleted.

libc/calls/_timeval_gte.c

Lines changed: 0 additions & 28 deletions
This file was deleted.

libc/calls/blockcancel.internal.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ COSMOPOLITAN_C_START_
77
#define BLOCK_CANCELLATIONS \
88
do { \
99
int _CancelState; \
10-
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_CancelState)
10+
_CancelState = _pthread_block_cancellations()
1111

12-
#define ALLOW_CANCELLATIONS \
13-
pthread_setcancelstate(_CancelState, 0); \
14-
} \
12+
#define ALLOW_CANCELLATIONS \
13+
_pthread_allow_cancellations(_CancelState); \
14+
} \
1515
while (0)
1616

17+
int _pthread_block_cancellations(void);
18+
void _pthread_allow_cancellations(int);
19+
1720
COSMOPOLITAN_C_END_
1821
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
1922
#endif /* COSMOPOLITAN_LIBC_CALLS_BLOCKCANCEL_H_ */

0 commit comments

Comments
 (0)