Skip to content

Commit 6245732

Browse files
committed
Make threads faster and more reliable
This change doubles the performance of thread spawning. That's thanks to our new stack manager, which allows us to avoid zeroing stacks. It gives us 15µs spawns rather than 30µs spawns on Linux. Also, pthread_exit() is faster now, since it doesn't need to acquire the pthread GIL. On NetBSD, that helps us avoid allocating too many semaphores. Even if that happens we're now able to survive semaphores running out and even memory running out, when allocating *NSYNC waiter objects. I found a lot more rare bugs in the POSIX threads runtime that could cause things to crash, if you've got dozens of threads all spawning and joining dozens of threads. I want cosmo to be world class production worthy for 2025 so happy holidays all
1 parent 906bd06 commit 6245732

Some content is hidden

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

51 files changed

+1005
-320
lines changed

libc/calls/clock_nanosleep-openbsd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#include "libc/sysv/consts/clock.h"
2424
#include "libc/sysv/errfuns.h"
2525

26-
int sys_clock_nanosleep_openbsd(int clock, int flags,
27-
const struct timespec *req,
28-
struct timespec *rem) {
26+
relegated int sys_clock_nanosleep_openbsd(int clock, int flags,
27+
const struct timespec *req,
28+
struct timespec *rem) {
2929
int res;
3030
struct timespec start, relative, remainder;
3131
if (!flags) {

libc/calls/time.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/time.h"
20-
#include "libc/calls/struct/timeval.h"
21-
#include "libc/dce.h"
22-
#include "libc/sysv/errfuns.h"
19+
#include "libc/calls/calls.h"
20+
#include "libc/calls/struct/timespec.h"
21+
#include "libc/sysv/consts/clock.h"
2322

2423
/**
2524
* Returns time as seconds from UNIX epoch.
@@ -29,15 +28,11 @@
2928
* @asyncsignalsafe
3029
*/
3130
int64_t time(int64_t *opt_out_ret) {
32-
int64_t secs;
33-
struct timeval tv;
34-
if (gettimeofday(&tv, 0) != -1) {
35-
secs = tv.tv_sec;
36-
if (opt_out_ret) {
37-
*opt_out_ret = secs;
38-
}
39-
} else {
40-
secs = -1;
41-
}
31+
int64_t secs = -1;
32+
struct timespec ts;
33+
if (!clock_gettime(CLOCK_REALTIME, &ts))
34+
secs = ts.tv_sec;
35+
if (opt_out_ret)
36+
*opt_out_ret = secs;
4237
return secs;
4338
}

libc/cosmo.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@ errno_t cosmo_once(_COSMO_ATOMIC(unsigned) *, void (*)(void)) libcesque;
1313
int systemvpe(const char *, char *const[], char *const[]) libcesque;
1414
char *GetProgramExecutableName(void) libcesque;
1515
void unleaf(void) libcesque;
16+
bool32 IsLinuxModern(void) libcesque;
17+
1618
int __demangle(char *, const char *, size_t) libcesque;
1719
int __is_mangled(const char *) libcesque;
18-
bool32 IsLinuxModern(void) libcesque;
19-
int LoadZipArgs(int *, char ***) libcesque;
20+
2021
int cosmo_args(const char *, char ***) libcesque;
22+
int LoadZipArgs(int *, char ***) libcesque;
23+
2124
int cosmo_futex_wake(_COSMO_ATOMIC(int) *, int, char);
2225
int cosmo_futex_wait(_COSMO_ATOMIC(int) *, int, char, int,
2326
const struct timespec *);
2427

28+
errno_t cosmo_stack_alloc(size_t *, size_t *, void **) libcesque;
29+
errno_t cosmo_stack_free(void *, size_t, size_t) libcesque;
30+
void cosmo_stack_clear(void) libcesque;
31+
void cosmo_stack_setmaxstacks(int) libcesque;
32+
int cosmo_stack_getmaxstacks(void) libcesque;
33+
2534
int __deadlock_check(void *, int) libcesque;
2635
int __deadlock_tracked(void *) libcesque;
2736
void __deadlock_record(void *, int) libcesque;

libc/intrin/count.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2024 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/atomic.h"
20+
#include "libc/stdalign.h"
21+
#include "libc/thread/thread.h"
22+
23+
// this counter is important because pthread_exit() needs to know if
24+
// it's an orphan thread, without needing to acquire _pthread_lock()
25+
// which causes contention and a file descriptor explosion on netbsd
26+
alignas(64) atomic_uint _pthread_count = 1;

libc/intrin/itimer.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2024 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/thread/itimer.h"
20+
#include "libc/str/str.h"
21+
22+
struct IntervalTimer __itimer = {
23+
.lock = PTHREAD_MUTEX_INITIALIZER,
24+
.cond = PTHREAD_COND_INITIALIZER,
25+
};
26+
27+
textwindows void __itimer_lock(void) {
28+
pthread_mutex_lock(&__itimer.lock);
29+
}
30+
31+
textwindows void __itimer_unlock(void) {
32+
pthread_mutex_unlock(&__itimer.lock);
33+
}
34+
35+
textwindows void __itimer_wipe_and_reset(void) {
36+
// timers aren't inherited by forked subprocesses
37+
bzero(&__itimer.it, sizeof(__itimer.it));
38+
pthread_mutex_wipe_np(&__itimer.lock);
39+
pthread_cond_init(&__itimer.cond, 0);
40+
__itimer.thread = 0;
41+
__itimer.once = 0;
42+
}

libc/intrin/kisdangerous.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 et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2024 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/intrin/kprintf.h"
20+
#include "libc/intrin/maps.h"
21+
22+
privileged optimizesize bool32 kisdangerous(const void *addr) {
23+
bool32 res = true;
24+
__maps_lock();
25+
if (__maps.maps) {
26+
struct Map *map;
27+
if ((map = __maps_floor(addr)))
28+
if ((const char *)addr >= map->addr &&
29+
(const char *)addr < map->addr + map->size)
30+
res = false;
31+
} else {
32+
res = false;
33+
}
34+
__maps_unlock();
35+
return res;
36+
}

libc/intrin/kprintf.greg.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,6 @@ __funline bool kischarmisaligned(const char *p, signed char t) {
160160
return false;
161161
}
162162

163-
ABI bool32 kisdangerous(const void *addr) {
164-
bool32 res = true;
165-
__maps_lock();
166-
if (__maps.maps) {
167-
struct Map *map;
168-
if ((map = __maps_floor(addr)))
169-
if ((const char *)addr >= map->addr &&
170-
(const char *)addr < map->addr + map->size)
171-
res = false;
172-
} else {
173-
res = false;
174-
}
175-
__maps_unlock();
176-
return res;
177-
}
178-
179163
ABI static void klogclose(long fd) {
180164
#ifdef __x86_64__
181165
long ax = __NR_close;
File renamed without changes.

libc/intrin/pthread_orphan_np.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/assert.h"
20+
#include "libc/intrin/atomic.h"
1921
#include "libc/thread/posixthread.internal.h"
2022
#include "libc/thread/thread.h"
2123

@@ -28,5 +30,6 @@ int pthread_orphan_np(void) {
2830
res = _pthread_list == _pthread_list->prev &&
2931
_pthread_list == _pthread_list->next;
3032
_pthread_unlock();
33+
unassert(!res || atomic_load(&_pthread_count) <= 1);
3134
return res;
3235
}

libc/intrin/pthreadlock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/stdalign.h"
1920
#include "libc/thread/posixthread.internal.h"
2021

21-
pthread_mutex_t __pthread_lock_obj = PTHREAD_MUTEX_INITIALIZER;
22+
alignas(64) pthread_mutex_t __pthread_lock_obj = PTHREAD_MUTEX_INITIALIZER;
2223

2324
void _pthread_lock(void) {
2425
pthread_mutex_lock(&__pthread_lock_obj);

0 commit comments

Comments
 (0)