raz / site / public / advanced / volatile.html

157 lines · 11.1 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>Volatile - 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="Volatile - 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">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" aria-current="page">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">Shared memory &middot; lesson 14 of 18</p><h1>Volatile</h1>
47<p>Most memory is the program's own: only it reads and writes it, so the compiler may keep a variable in a register, leave out a write that nothing reads, and change the order of accesses, as long as the program does what the report says. Some memory is not: a device's registers, memory another processor or thread is changing, a page the kernel fills. There, each access matters, and its order.</p>
48<p>A variable reached through a <code>^VOLATILE T</code> is volatile, and so is any field or element of it. Each read or write of one of a basic, enumeration, set or pointer type is exactly one memory access, of exactly its size, and volatile accesses happen in the order the program writes them.</p>
49<h2>A device laid over memory</h2>
50<p>The classic use is a device's registers: a record that says what is at which offset, laid over the address the hardware answers at. The record's layout is C's, so it is the same as the datasheet's struct. Here a page of the program's own stands in for the device.</p>
51<div class="file"><span>Uart.raz</span></div><pre class="code"><code><span class="kw">module</span> Uart;
52<span class="kw">import</span> Out;
53<span class="kw">import</span> SYSTEM;
54
55<span class="kw">type</span>
56 Regs = <span class="kw">record</span>
57 data: <span class="ty">U32</span>; <span class="comment">(* offset 0 *)</span>
58 status: <span class="ty">U32</span>; <span class="comment">(* offset 4 *)</span>
59 control: <span class="ty">U8</span>; <span class="comment">(* offset 8 *)</span>
60 <span class="kw">end</span>;
61
62<span class="kw">var</span> page: [<span class="number">64</span>]<span class="ty">U8</span>; <span class="comment">(* where the device would be *)</span>
63
64<span class="kw">proc</span> main*;
65<span class="kw">var</span> dev: ^<span class="kw">VOLATILE</span> Regs; i: <span class="ty">INT</span>;
66<span class="kw">begin</span>
67 dev := ^<span class="kw">VOLATILE</span> Regs(SYSTEM.<span class="ty">ADR</span>(page)); <span class="comment">(* the conversion is written *)</span>
68 dev^.control := <span class="number">1</span>;
69 dev^.status := <span class="number">0FFH</span>;
70 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
71 dev^.data := <span class="ty">U32</span>(<span class="string">&#x27;a&#x27;</span> + i);
72 Out.char(<span class="ty">U8</span>(dev^.data));
73 <span class="kw">end</span>;
74 Out.ln;
75 FENCE;
76 Out.int(<span class="ty">INT</span>(page[<span class="number">8</span>])); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(<span class="ty">INT</span>(dev^.status)); Out.ln;
77<span class="kw">end</span>;
78
79<span class="kw">end</span> Uart.</code></pre>
80<pre class="output"><code>abc
811 255
82</code></pre>
83<p>On real hardware the address would be a constant, <code>^VOLATILE Regs(0FEC00000H)</code>, which is a legal conversion since an integer constant converts to a pointer. The program is otherwise unchanged.</p>
84<h2>What each access is</h2>
85<p>The listing shows what the guarantee means: each field access is one instruction of the field's size, and so is a write to a field that is never read afterwards, which an optimizer would be allowed to drop from an ordinary variable.</p>
86<pre class="output term"><code><span class="cmd">$ raz -S Uart.raz | grep -n &quot;dword\|byte&quot; | head -2</span>
8715: 34 mov byte [rbx + 8], 1
8817: 38 mov dword [rbx + 4], 255</code></pre>
89<p>The <code>U8</code> control byte is written with a <code>byte</code> store, and the <code>U32</code> status with a <code>dword</code> one. Never a wider access that touches the neighbour, never two narrower ones.</p>
90<h2>Waiting for something else to change</h2>
91<p>Where a program waits for another thread or a device to change a value, the loop must read the value each time. A volatile read does. This "device" is a thread that sets a bit in a status word after a delay; the main thread polls.</p>
92<div class="file"><span>Poll.raz</span></div><pre class="code"><code><span class="kw">module</span> Poll;
93<span class="kw">import</span> Out, Sys, Thread;
94<span class="kw">import</span> SYSTEM;
95
96<span class="kw">type</span> Dev = <span class="kw">record</span> status: <span class="ty">INT</span>; data: <span class="ty">INT</span>; <span class="kw">end</span>;
97
98<span class="kw">var</span> dev: Dev;
99
100<span class="kw">proc</span> device(_arg: ^<span class="ty">U8</span>);
101<span class="kw">var</span> d: ^<span class="kw">VOLATILE</span> Dev;
102<span class="kw">begin</span>
103 d := ^<span class="kw">VOLATILE</span> Dev(SYSTEM.<span class="ty">ADR</span>(dev));
104 Sys.sleep(<span class="number">20</span>);
105 d^.data := <span class="number">42</span>;
106 FENCE; <span class="comment">(* data before the flag that announces it *)</span>
107 d^.status := <span class="number">1</span>;
108<span class="kw">end</span>;
109
110<span class="kw">proc</span> main*;
111<span class="kw">var</span> t: ^Thread.Thread; err: <span class="ty">ERROR</span>; d: ^<span class="kw">VOLATILE</span> Dev; spins: <span class="ty">INT</span>;
112<span class="kw">begin</span>
113 d := ^<span class="kw">VOLATILE</span> Dev(SYSTEM.<span class="ty">ADR</span>(dev));
114 t, err := Thread.spawn(device, <span class="ty">nil</span>);
115 <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>;
116 <span class="kw">while</span> d^.status = <span class="number">0</span> <span class="kw">do</span> <span class="ty">INC</span>(spins); Sys.yield; <span class="kw">end</span>;
117 Out.int(d^.data); Out.ln;
118 <span class="kw">if</span> spins &gt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;it waited for the device\n&quot;</span>); <span class="kw">end</span>;
119 Thread.join(t);
120<span class="kw">end</span>;
121
122<span class="kw">end</span> Poll.</code></pre>
123<pre class="output"><code>42
124it waited for the device
125</code></pre>
126<p>The <code>FENCE</code> between the two writes matters. It is a full memory barrier, ordering the program's ordinary accesses against those of other processors and devices. Without it, volatile accesses are ordered among themselves, but nothing says another processor sees <code>data</code> before <code>status</code>. The next lessons are about this.</p>
127<h2>What volatile is not</h2>
128<ul><li><strong>Not atomic across a read-modify-write.</strong> <code>INC(d^.count)</code> reads and then writes: two accesses, even through a volatile pointer. <code>XADD</code> is the atomic one.</li><li><strong>Not a lock.</strong> It says each access happens as written; it says nothing about what happens between them.</li><li><strong>Not for whole records or arrays.</strong> Copying a whole record through a volatile pointer isn't allowed, since it can't be one access. A field of a packed record can't be reached volatilely at all, since a misaligned access can't be promised as one.</li><li><strong>Not silently dropped.</strong> A <code>^VOLATILE T</code> doesn't convert to a <code>^T</code> without a written conversion, so the volatile can't be lost by accident.</li></ul>
129<p>The compiler holds the line on each of them:</p>
130<div class="file"><span>Wrong.raz</span></div><pre class="code"><code><span class="kw">module</span> Wrong;
131<span class="kw">import</span> SYSTEM;
132
133<span class="kw">type</span> Pair = <span class="kw">record</span> a, b: <span class="ty">U32</span>; <span class="kw">end</span>;
134
135<span class="kw">var</span> cell: Pair;
136
137<span class="kw">proc</span> main*;
138<span class="kw">var</span> v: ^<span class="kw">VOLATILE</span> Pair; p: ^Pair; copy: Pair;
139<span class="kw">begin</span>
140 v := ^<span class="kw">VOLATILE</span> Pair(SYSTEM.<span class="ty">ADR</span>(cell));
141 p := v;
142 copy := v^;
143<span class="kw">end</span>;
144
145<span class="kw">end</span> Wrong.</code></pre>
146<pre class="output term"><code><span class="cmd">$ raz Wrong.raz</span>
147Wrong.raz:12:10: error: can&#x27;t use ^VOLATILE Pair as ^Pair
148 12 | p := v;
149 | ^
150Wrong.raz:13:13: error: can&#x27;t copy through a volatile pointer
151 13 | copy := v^;
152 | ^^
153raz: 2 errors</code></pre><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="listing.html">&larr; What the compiler makes</a><a href="index.html">Contents</a><a class="next" href="atomics.html">Atomics &rarr;</a></div></article></div>
154</main>
155<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
156</body>
157</html>