Standing alone · lesson 8 of 18

Threads without C, and keeping the two apart

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.

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.

What Thread does

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:

$ sed -n '1,22p' $RAZLIB/Thread.linux-x86_64.raz
(* Threads for programs without C, made with the clone system call.
   Each runs on a stack of its own, mmaped, with a page below it that
   can't be touched, so that running off the stack stops the program
   rather than spoiling what lies below; above it, a little stack for
   Rt's signal handlers, so that they can say so. join waits on the
   thread's id, which the kernel clears, and wakes it for, when the
   thread ends; a Mutex sleeps on a futex. A program with C threads
   uses C's, through pthread_create, and so can't use these. *)
module Thread;
import Sys, Rt;
link static;

const
    Failed*: ERROR = "can't start a thread";
    Stack = 1 shl 20;
    Alt = 16384;     (* the signal handlers' stack *)
    Page = 4096;
    (* clone: share memory, files, signal handlers, the thread group;
       write the id to tid, and clear it at the end *)
    Flags = 350F00H;
    FutexWait = 0;
    FutexWake = 1;

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.

Hello.raz
module Hello;
import Out, Thread;

var done: INT;

proc greet(arg: ^U8);
begin
    Out.str("hello from thread "); Out.int(INT(arg)); Out.ln;
    _ := XADD(ADR(done), 1);
end;

proc main*;
var t: ^Thread.Thread; err: ERROR;
begin
    t, err := Thread.spawn(greet, ^U8(7));
    if err # nil then Out.str(MSG(err)); Out.ln; return; end;
    Thread.join(t);
    Out.str("joined; done = "); Out.int(done); Out.ln;
end;

end Hello.
$ raz Hello.raz
$ ./Hello
hello from thread 7
joined; done = 1
$ file Hello | sed 's/,.*statically/, statically/'
Hello: ELF 64-bit LSB executable, statically linked, with debug_info, not stripped

When you mix them

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:

Mixed.raz
module Mixed;
import Out, Thread, C;

proc main*;
begin
    Out.str(MSG(Thread.Failed)); Out.ln;
    _ := C.printf("%d\n", I32(1));
    Out.str("and Out\n");
end;

end Mixed.
$ raz Mixed.raz
raz: Thread says link static, but C uses C

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.

The same promise for your own modules

The rule isn't special to Thread. A module of yours that makes SYSCALLs 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.

Save a program under its module’s name and run it with raz run Name.raz.