Skip to content

Commit 4a6fd3d

Browse files
committed
Make more improvements to threading support
- fix rare thread exit race condition on openbsd - pthread_getattr_np() now supplies detached status - child threads may now pthread_join() the main thread - introduce sigandset(), sigorset(), and sigisemptyset() - introduce pthread_cleanup_push() and pthread_cleanup_pop()
1 parent 38df0a4 commit 4a6fd3d

Some content is hidden

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

52 files changed

+586
-241
lines changed

libc/assert.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define COSMOPOLITAN_LIBC_ASSERT_H_
33
#if !(__ASSEMBLER__ + __LINKER__ + 0)
44
COSMOPOLITAN_C_START_
5+
#include "libc/intrin/kprintf.h"
56

67
extern bool __assert_disable;
78
void __assert_fail(const char *, const char *, int) hidden relegated;
@@ -23,11 +24,12 @@ void __assert_fail(const char *, const char *, int) hidden relegated;
2324
} \
2425
} while (0)
2526

26-
#define _npassert(x) \
27-
do { \
28-
if (__builtin_expect(!(x), 0)) { \
29-
notpossible; \
30-
} \
27+
#define _npassert(x) \
28+
do { \
29+
if (__builtin_expect(!(x), 0)) { \
30+
kprintf("%s:%d: oh no!\n", __FILE__, __LINE__); \
31+
notpossible; \
32+
} \
3133
} while (0)
3234

3335
COSMOPOLITAN_C_END_

libc/calls/struct/sigset.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ int sigaddset(sigset_t *, int) paramsnonnull();
1111
int sigdelset(sigset_t *, int) paramsnonnull();
1212
int sigemptyset(sigset_t *) paramsnonnull();
1313
int sigfillset(sigset_t *) paramsnonnull();
14+
int sigandset(sigset_t *, const sigset_t *, const sigset_t *) paramsnonnull();
15+
int sigorset(sigset_t *, const sigset_t *, const sigset_t *) paramsnonnull();
16+
int sigisemptyset(const sigset_t *) paramsnonnull();
1417
int sigismember(const sigset_t *, int) paramsnonnull() nosideeffect;
1518
int sigprocmask(int, const sigset_t *, sigset_t *);
1619
int sigsuspend(const sigset_t *);

libc/intrin/exit1.greg.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/intrin/strace.internal.h"
2019
#include "libc/dce.h"
2120
#include "libc/intrin/asmflag.h"
2221
#include "libc/intrin/promises.internal.h"
22+
#include "libc/intrin/strace.internal.h"
2323
#include "libc/nt/thread.h"
2424
#include "libc/runtime/runtime.h"
2525
#include "libc/sysv/consts/nr.h"
26+
#include "libc/thread/tls.h"
2627

2728
__msabi extern typeof(ExitThread) *const __imp_ExitThread;
2829

2930
/**
3031
* Terminates thread with raw system call.
3132
*
33+
* The function you want is pthread_exit(). If you call this function
34+
* whilst using the pthreads then your joiners might not get woken up
35+
* on non-Linux platforms where we zero __get_tls()->tib_tid manually
36+
*
3237
* If this is the main thread, or an orphaned child thread, then this
3338
* function is equivalent to exiting the process; however, `rc` shall
3439
* only be reported to the parent process on Linux, FreeBSD & Windows

libc/intrin/sigandset.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2020 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/struct/sigset.h"
20+
#include "libc/macros.internal.h"
21+
#include "libc/str/str.h"
22+
23+
/**
24+
* Bitwise ANDs two signal sets.
25+
*
26+
* @return 0 on success, or -1 w/ errno
27+
* @asyncsignalsafe
28+
* @vforksafe
29+
*/
30+
int sigandset(sigset_t *set, const sigset_t *x, const sigset_t *y) {
31+
int i;
32+
for (i = 0; i < ARRAYLEN(set->__bits); ++i) {
33+
set->__bits[i] = x->__bits[i] & y->__bits[i];
34+
}
35+
return 0;
36+
}

libc/intrin/sigisemptyset.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2020 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/struct/sigset.h"
20+
#include "libc/macros.internal.h"
21+
#include "libc/str/str.h"
22+
23+
/**
24+
* Determines if signal set is empty.
25+
*
26+
* @return 1 if empty, 0 if non-empty, or -1 w/ errno
27+
* @asyncsignalsafe
28+
* @vforksafe
29+
*/
30+
int sigisemptyset(const sigset_t *set) {
31+
int i;
32+
for (i = 0; i < ARRAYLEN(set->__bits); ++i) {
33+
if (set->__bits[i]) {
34+
return 0;
35+
}
36+
}
37+
return 1;
38+
}

libc/intrin/sigorset.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2020 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/struct/sigset.h"
20+
#include "libc/macros.internal.h"
21+
#include "libc/str/str.h"
22+
23+
/**
24+
* Bitwise ORs two signal sets.
25+
*
26+
* @return 0 on success, or -1 w/ errno
27+
* @asyncsignalsafe
28+
* @vforksafe
29+
*/
30+
int sigorset(sigset_t *set, const sigset_t *x, const sigset_t *y) {
31+
int i;
32+
for (i = 0; i < ARRAYLEN(set->__bits); ++i) {
33+
set->__bits[i] = x->__bits[i] | y->__bits[i];
34+
}
35+
return 0;
36+
}

libc/log/checkfail.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/intrin/safemacros.internal.h"
2019
#include "libc/calls/calls.h"
21-
#include "libc/intrin/strace.internal.h"
2220
#include "libc/errno.h"
2321
#include "libc/fmt/fmt.h"
2422
#include "libc/intrin/kprintf.h"
23+
#include "libc/intrin/safemacros.internal.h"
24+
#include "libc/intrin/strace.internal.h"
2525
#include "libc/log/check.h"
2626
#include "libc/log/color.internal.h"
2727
#include "libc/log/internal.h"
@@ -71,9 +71,6 @@ relegated void __check_fail(const char *suffix, const char *opstr,
7171
kprintf(" %s", __argv[i]);
7272
}
7373
kprintf("%s\n", RESET);
74-
if (!IsTiny() && e == ENOMEM) {
75-
__print_maps();
76-
}
7774
__die();
7875
unreachable;
7976
}

libc/log/memlog.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "libc/mem/mem.h"
2727
#include "libc/runtime/symbols.internal.h"
2828
#include "libc/sysv/consts/o.h"
29+
#include "libc/thread/thread.h"
2930
#include "third_party/dlmalloc/dlmalloc.h"
3031

3132
/**

libc/log/oncrash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ relegated void ShowCrashReport(int err, int sig, struct siginfo *si,
223223
host, getpid(), gettid(), program_invocation_name, names.sysname,
224224
names.version, names.nodename, names.release);
225225
if (ctx) {
226-
kprintf("\n");
227-
ShowFunctionCalls(ctx);
228226
ShowGeneralRegisters(ctx);
229227
ShowSseRegisters(ctx);
228+
kprintf("\n");
229+
ShowFunctionCalls(ctx);
230230
}
231231
kprintf("\n");
232232
__print_maps();

libc/nexgen32e/gc.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ __gc: mov %fs:0,%rcx # __get_tls()
5757
leave
5858
ret
5959
9: ud2
60+
nop
6061
.endfn __gc,globl,hidden

0 commit comments

Comments
 (0)