| 1 | title: Threads without C, and keeping the two apart |
| 2 | part: Standing alone |
| 3 | summary: link static, what a module that makes its own system calls promises, and the error when it breaks. |
| 4 | --- |
| 5 | A 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 | |
| 7 | A 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 | |
| 17 | A 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 |
| 20 | module Hello; |
| 21 | import Out, Thread; |
| 22 | import SYSTEM; |
| 23 | |
| 24 | var done: INT; |
| 25 | |
| 26 | proc greet(arg: ^U8); |
| 27 | begin |
| 28 | Out.str("hello from thread "); Out.int(INT(arg)); Out.ln; |
| 29 | _ := XADD(done, 1); |
| 30 | end; |
| 31 | |
| 32 | proc main*; |
| 33 | var t: ^Thread.Thread; err: ERROR; |
| 34 | begin |
| 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; |
| 39 | end; |
| 40 | |
| 41 | end 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 | |
| 52 | C'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 |
| 55 | module Mixed; |
| 56 | import Out, Thread, C; |
| 57 | |
| 58 | proc main*; |
| 59 | begin |
| 60 | Out.str(MSG(Thread.Failed)); Out.ln; |
| 61 | _ := C.printf("%d\n", I32(1)); |
| 62 | Out.str("and Out\n"); |
| 63 | end; |
| 64 | |
| 65 | end Mixed. |
| 66 | ``` |
| 67 | |
| 68 | ```sh |
| 69 | $ raz Mixed.raz |
| 70 | ``` |
| 71 | |
| 72 | Which 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 | |
| 76 | The 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. |