1 //go:build none
2 //:build linux
3 //
4 // The //go:build none above is the upstream marker for a file that Go's
5 // toolchain must not compile on its own; these are included manually for
6 // Linux targets. Stage4 selects files with //:build, so without this it
7 // compiles them for every target - and they include pthread headers.
8 //
9 // A Moxie domain is a THREAD, not a process. The runtime's whole concurrency
10 // model is cooperative - one thread of execution per domain, no shared-memory
11 // window between them, an event loop that observes its breaker between tasks -
12 // and a thread is exactly that: a second execution context in the same address
13 // space with its own TLS (domain root, arenas, package globals) and its own
14 // stack. Forking instead gave each domain a PID it never needed, made
15 // termination a signal problem (pdeathsig, waitpid, a reaper, a pid table) and
16 // let a wedged domain outlive its owner as a stray process.
17 //
18 // The thread gets its own TLS block, so InitCShared runs again inside it: fresh
19 // domain root, fresh arenas, and the package initialisers, so the child's
20 // globals are its own.
21 22 #include <pthread.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 26 // Domain stack sizes are per role, not one constant. A worker domain that runs
27 // a package's initialisers and a deep codec needs a large stack; an event-loop
28 // domain that dispatches short handlers does not, and 64 MB per domain was
29 // simply committed address space times 9-15 threads. The default stays
30 // conservative and a program chooses the per-role size with MOXIE_STACK_BYTES:
31 //
32 // MOXIE_STACK_BYTES=16777216 normgr
33 //
34 // or, for a finer split, MOXIE_STACK_BYTES_MAIN (the main domain) and
35 // MOXIE_STACK_BYTES_WORKER (every spawned domain). A value below the minimum or
36 // above the cap is refused, not clamped: a stack the runtime cannot honour is a
37 // crash waiting for the frame that needs it.
38 #define MOXIE_STACK_MIN (256UL * 1024UL)
39 #define MOXIE_STACK_MAX (512UL * 1024UL * 1024UL)
40 #define MOXIE_STACK_DEFAULT (16UL * 1024UL * 1024UL)
41 42 static uint32_t moxie_stack_bytes_from(const char *name) {
43 const char *v = getenv(name);
44 if (v == NULL || *v == '\0') {
45 return 0;
46 }
47 char *end = NULL;
48 unsigned long long n = strtoull(v, &end, 10);
49 if (end == v || (end != NULL && *end != '\0')) {
50 return 0;
51 }
52 if (n < MOXIE_STACK_MIN || n > MOXIE_STACK_MAX) {
53 return 0;
54 }
55 return (uint32_t)n;
56 }
57 58 // moxie_domain_stack_bytes is the stack a spawned domain gets.
59 uint32_t moxie_domain_stack_bytes(void) {
60 uint32_t n = moxie_stack_bytes_from("MOXIE_STACK_BYTES");
61 if (n == 0) {
62 n = moxie_stack_bytes_from("MOXIE_STACK_BYTES_WORKER");
63 }
64 if (n == 0) {
65 n = (uint32_t)MOXIE_STACK_DEFAULT;
66 }
67 return n;
68 }
69 70 // moxie_runtime_thread_entry is the Moxie side of a spawn: it initialises the
71 // domain (InitCShared), binds the breaker and the channel rings, then calls the
72 // target function. Exported from src/runtime/spawn.mx.
73 extern void moxie_runtime_thread_entry(void *arg);
74 75 static void *moxie_thread_tramp(void *arg) {
76 moxie_runtime_thread_entry(arg);
77 return NULL;
78 }
79 80 // moxie_thread_spawn starts one domain thread. The stack matches the compiler's
81 // worker threads: a package's type graph and IR are deep, and a domain's frames
82 // live on this stack for the life of the domain - but the size is the
83 // program's choice (moxie_domain_stack_bytes), not a constant in the runtime.
84 int32_t moxie_thread_spawn(void *arg) {
85 pthread_attr_t attr;
86 if (pthread_attr_init(&attr) != 0) {
87 return -1;
88 }
89 pthread_attr_setstacksize(&attr, (size_t)moxie_domain_stack_bytes());
90 pthread_t tid;
91 int err = pthread_create(&tid, &attr, moxie_thread_tramp, arg);
92 pthread_attr_destroy(&attr);
93 return err == 0 ? 0 : -1;
94 }
95