raz / site / content / advanced / 08-linkstatic.txt

76 lines · 2.9 KiB · raw · history · last change a5145f64

1title: Threads without C, and keeping the two apart
2part: Standing alone
3summary: link static, what a module that makes its own system calls promises, and the error when it breaks.
4---
5A program without C can do things a program with C can't. The most useful is to make threads of its own: C's thread functions rely on C's own set-up, which a program that never started C has not done. `Thread` creates threads directly with the `clone` system call and gives each a stack of its own.
6
7A module like that says so, with `link static;` after its imports. It means: "I make my own system calls or my own threads, which the C library wouldn't know about; a program that includes me must not use C." `static` isn't a reserved word; it is only meaningful after `link`.
8
9## What `Thread` does
10
11`Thread` is a module of raz, in the library, that says `link static;` and makes the `clone`, `mmap` and `futex` system calls itself. Here is its beginning, printed from the library:
12
13```sh
14$ sed -n '1,22p' $RAZLIB/Thread.linux-x86_64.raz
15```
16
17A program that imports it is therefore a static executable, and every thread it spawns is a Linux thread with a stack of its own, mapped with a page below it that can't be touched, so that running off the stack ends the program with a message and not with corruption.
18
19```raz Hello
20module Hello;
21import Out, Thread;
22import SYSTEM;
23
24var done: INT;
25
26proc greet(arg: ^U8);
27begin
28 Out.str("hello from thread "); Out.int(INT(arg)); Out.ln;
29 _ := XADD(done, 1);
30end;
31
32proc main*;
33var t: ^Thread.Thread; err: ERROR;
34begin
35 t, err := Thread.spawn(greet, ^U8(7));
36 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
37 Thread.join(t);
38 Out.str("joined; done = "); Out.int(done); Out.ln;
39end;
40
41end Hello.
42```
43
44```sh
45$ raz Hello.raz
46$ ./Hello
47$ file Hello | sed 's/,.*statically/, statically/'
48```
49
50## When you mix them
51
52C's `pthread_create` and `Thread` can't share a process, and the compiler won't let you try. A program that imports a module that says `link static;` and another that uses C is an error, and the error names both:
53
54```raz Mixed
55module Mixed;
56import Out, Thread, C;
57
58proc main*;
59begin
60 Out.str(MSG(Thread.Failed)); Out.ln;
61 _ := C.printf("%d\n", I32(1));
62 Out.str("and Out\n");
63end;
64
65end Mixed.
66```
67
68```sh
69$ raz Mixed.raz
70```
71
72Which one you want depends on what the program is for. A program that calls a C library (a window system, a database client) uses C's threads, `C.pthread_create`, because the library may assume them. A program that talks only to the kernel uses `Thread`, and is a static executable.
73
74## The same promise for your own modules
75
76The rule isn't special to `Thread`. A module of yours that makes `SYSCALL`s directly, or relies on being the only owner of signal handlers, or sets up its own stacks, says `link static;` too, and then the compiler keeps C away from it for you. The module is documenting a fact about itself, and the compiler checks it where the program is assembled.