raz / site / public / advanced / linkstatic.html

128 lines · 8.9 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>Threads without C, and keeping the two apart - More Raz</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="Threads without C, and keeping the two apart - More Raz">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'>
22<li><a href="fmt.html">Output and formatting</a></li>
23<li><a href="bytes.html">Strings and bytes</a></li>
24<li><a href="collections.html">Lists, arrays, maps and sorting</a></li>
25<li><a href="files.html">Files and the command line</a></li>
26<li><a href="processes.html">Time, randomness and other programs</a></li>
27<li><a href="allocators.html">Writing an allocator</a></li>
28</ol>
29<h3>Standing alone</h3><ol start='7'>
30<li><a href="static.html">A program with no run time</a></li>
31<li><a href="linkstatic.html" aria-current="page">Threads without C, and keeping the two apart</a></li>
32<li><a href="lifecycle.html">Starting, ending, and signals</a></li>
33<li><a href="targets.html">One module, one file per target</a></li>
34</ol>
35<h3>The machine</h3><ol start='11'>
36<li><a href="asm.html">Procedures in assembly</a></li>
37<li><a href="syscalls.html">System calls</a></li>
38<li><a href="listing.html">What the compiler makes</a></li>
39</ol>
40<h3>Shared memory</h3><ol start='14'>
41<li><a href="volatile.html">Volatile</a></li>
42<li><a href="atomics.html">Atomics</a></li>
43<li><a href="ordering.html">Ordering and publication</a></li>
44<li><a href="lockfree.html">A lock-free stack</a></li>
45<li><a href="ring.html">A ring buffer between threads</a></li>
46</ol></aside><article><p class="kicker">Standing alone &middot; lesson 8 of 18</p><h1>Threads without C, and keeping the two apart</h1>
47<p>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. <code>Thread</code> creates threads directly with the <code>clone</code> system call and gives each a stack of its own.</p>
48<p>A module like that says so, with <code>link static;</code> 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." <code>static</code> isn't a reserved word; it is only meaningful after <code>link</code>.</p>
49<h2>What <code>Thread</code> does</h2>
50<p><code>Thread</code> is a module of raz, in the library, that says <code>link static;</code> and makes the <code>clone</code>, <code>mmap</code> and <code>futex</code> system calls itself. Here is its beginning, printed from the library:</p>
51<pre class="output term"><code><span class="cmd">$ sed -n &#x27;1,22p&#x27; $RAZLIB/Thread.linux-x86_64.raz</span>
52(* Threads, made with the clone system call in a program without C.
53 Each runs on a stack of its own, mmaped, with a page below it that
54 can&#x27;t be touched, so that running off the stack stops the program
55 rather than spoiling what lies below; above it, a little stack for
56 Rt&#x27;s signal handlers, so that they can say so. join waits on the
57 thread&#x27;s id, which the kernel clears, and wakes it for, when the
58 thread ends; a Mutex sleeps on a futex.
59
60 A program that uses C makes them with C&#x27;s pthread_create instead,
61 since C keeps things of each thread&#x27;s own, errno and its buffers,
62 that a thread clone makes wouldn&#x27;t have. The compiler&#x27;s entry point
63 finds cCreate and cJoin by name and stores C&#x27;s calls in them, as it
64 stores C&#x27;s exit in Rt.cExit; so this module uses no C itself, and
65 serves both kinds of program. C&#x27;s thread has a stack and a guard
66 page of C&#x27;s; the mapping here holds only the handlers&#x27; stack and
67 the Thread. Its test uses Check. *)
68module Thread;
69import Sys, Rt, Check;
70import SYSTEM;
71
72const
73 Failed*: ERROR = &quot;can&#x27;t start a thread&quot;;</code></pre>
74<p>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.</p>
75<div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello;
76<span class="kw">import</span> Out, Thread;
77<span class="kw">import</span> SYSTEM;
78
79<span class="kw">var</span> done: <span class="ty">INT</span>;
80
81<span class="kw">proc</span> greet(arg: ^<span class="ty">U8</span>);
82<span class="kw">begin</span>
83 Out.str(<span class="string">&quot;hello from thread &quot;</span>); Out.int(<span class="ty">INT</span>(arg)); Out.ln;
84 _ := <span class="ty">XADD</span>(done, <span class="number">1</span>);
85<span class="kw">end</span>;
86
87<span class="kw">proc</span> main*;
88<span class="kw">var</span> t: ^Thread.Thread; err: <span class="ty">ERROR</span>;
89<span class="kw">begin</span>
90 t, err := Thread.spawn(greet, ^<span class="ty">U8</span>(<span class="number">7</span>));
91 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
92 Thread.join(t);
93 Out.str(<span class="string">&quot;joined; done = &quot;</span>); Out.int(done); Out.ln;
94<span class="kw">end</span>;
95
96<span class="kw">end</span> Hello.</code></pre>
97<pre class="output term"><code><span class="cmd">$ raz Hello.raz</span>
98<span class="cmd">$ ./Hello</span>
99hello from thread 7
100joined; done = 1
101<span class="cmd">$ file Hello | sed &#x27;s/,.*statically/, statically/&#x27;</span>
102Hello: ELF 64-bit LSB executable, statically linked, with debug_info, not stripped</code></pre>
103<h2>When you mix them</h2>
104<p>C's <code>pthread_create</code> and <code>Thread</code> can't share a process, and the compiler won't let you try. A program that imports a module that says <code>link static;</code> and another that uses C is an error, and the error names both:</p>
105<div class="file"><span>Mixed.raz</span></div><pre class="code"><code><span class="kw">module</span> Mixed;
106<span class="kw">import</span> Out, Thread, C;
107
108<span class="kw">proc</span> main*;
109<span class="kw">begin</span>
110 Out.str(<span class="ty">MSG</span>(Thread.Failed)); Out.ln;
111 _ := C.printf(<span class="string">&quot;%d\n&quot;</span>, <span class="ty">I32</span>(<span class="number">1</span>));
112 Out.str(<span class="string">&quot;and Out\n&quot;</span>);
113<span class="kw">end</span>;
114
115<span class="kw">end</span> Mixed.</code></pre>
116<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span>
117Mixed.raz:7:19: error: a string as a ^U8 needs import SYSTEM
118 7 | _ := C.printf(&quot;%d\n&quot;, I32(1));
119 | ^^^^^^
120help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
121raz: 1 error</code></pre>
122<p>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, <code>C.pthread_create</code>, because the library may assume them. A program that talks only to the kernel uses <code>Thread</code>, and is a static executable.</p>
123<h2>The same promise for your own modules</h2>
124<p>The rule isn't special to <code>Thread</code>. A module of yours that makes <code>SYSCALL</code>s directly, or relies on being the only owner of signal handlers, or sets up its own stacks, says <code>link static;</code> 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.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="static.html">&larr; A program with no run time</a><a href="index.html">Contents</a><a class="next" href="lifecycle.html">Starting, ending, and signals &rarr;</a></div></article></div>
125</main>
126<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
127</body>
128</html>