Skip to content

0.14.1 --- a copy of the space can start a thread - #29

Merged
Sunrisepeak merged 4 commits into
mainfrom
openkal-0.16.0-fork
Oct 25, 2026
Merged

Sunrisepeak merged 4 commits into
mainfrom
openkal-0.16.0-fork

Conversation

@Sunrisepeak

@Sunrisepeak Sunrisepeak commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Fixes mcpplibs/openkal-musl#55.

kal_space_start made the copy with the duplication primitive alone. This system's fork does more: around the primitive each part of the system library takes its locks, and in the copy each makes its state true of the new process. Without that, the copy's first thread failed with EAGAIN.

Measured on macOS 14.8.9, in a program that duplicated itself with the primitive and asked the copy for a thread:

preparations the copy's pthread_create
none EAGAIN
the two child preparations alone the copy was killed
and the thread library's locks around the primitive 0
the whole set, in the library's order, five rounds of twenty threads 0

The routine fork itself calls (_libSystem_atfork_*) is not exported; the parts it calls are (dyld, malloc, libc, the thread library, and _mach_fork_child). They are called in the library's order, under names no C library in this ecosystem defines, so they are reachable beneath one. port/libSystem.tbd lists them for a link made elsewhere, and -U leaves them for the loader in a native link, whose development stub need not list them. Handlers a program registers with pthread_atfork are not run: a copy made here is not a fork the program asked for.

tests/conformance_space_task.cpp starts a context, joins it and starts one with a least stack size in a copy, three times, and reads the outcome through kal_process_wait.

kal_space_start made the copy with the duplication primitive alone, and this
system's fork does more: around the primitive the system library takes its
locks and, in the copy, makes its state true of the new process. Without that
the copy's first thread failed with EAGAIN (mcpplibs/openkal-musl#55). The
three preparations fork itself calls are now called around the primitive,
under names no C library defines, and port/libSystem.tbd names them.
tests/conformance_space_task.cpp starts a context, joins it and starts one
with a least stack size in a copy, three times.
The system library exports them and its development stub does not list them,
because they are not part of the interface it documents. -U leaves each for the
loader to find among the loaded images; port/libSystem.tbd lists them for a link
made elsewhere.
_libSystem_atfork_* are not exported on macOS 14.8.9; the parts they call are.
Measured in a program that duplicated itself with the primitive and asked the
copy for a thread: nothing, EAGAIN; the two child preparations alone, the copy
was killed; with the thread library's locks around the primitive, 0; the whole
set in the library's order, five rounds of twenty threads, 0. The whole set is
called, in that order, and named in port/libSystem.tbd and in -U flags.
@Sunrisepeak
Sunrisepeak merged commit 513b28a into main Oct 25, 2026
2 checks passed
@Sunrisepeak
Sunrisepeak deleted the openkal-0.16.0-fork branch October 25, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: pthread_create returns EAGAIN in a program duplicated by fork

1 participant