raz / site / content / advanced / 14-volatile.txt

127 lines · 4.7 KiB · raw · history · last change a5145f64

1title: Volatile
2part: Shared memory
3summary: ^VOLATILE T: memory that something else may change, laid over with a record.
4---
5Most 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.
6
7A variable reached through a `^VOLATILE T` 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.
8
9## A device laid over memory
10
11The 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.
12
13```raz Uart run
14module Uart;
15import Out;
16import SYSTEM;
17
18type
19 Regs = record
20 data: U32; (* offset 0 *)
21 status: U32; (* offset 4 *)
22 control: U8; (* offset 8 *)
23 end;
24
25var page: [64]U8; (* where the device would be *)
26
27proc main*;
28var dev: ^VOLATILE Regs; i: INT;
29begin
30 dev := ^VOLATILE Regs(SYSTEM.ADR(page)); (* the conversion is written *)
31 dev^.control := 1;
32 dev^.status := 0FFH;
33 for i := 0 to 2 do
34 dev^.data := U32('a' + i);
35 Out.char(U8(dev^.data));
36 end;
37 Out.ln;
38 FENCE;
39 Out.int(INT(page[8])); Out.char(' '); Out.int(INT(dev^.status)); Out.ln;
40end;
41
42end Uart.
43```
44
45On real hardware the address would be a constant, `^VOLATILE Regs(0FEC00000H)`, which is a legal conversion since an integer constant converts to a pointer. The program is otherwise unchanged.
46
47## What each access is
48
49The 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.
50
51```sh
52$ raz -S Uart.raz | grep -n "dword\|byte" | head -2
53```
54
55The `U8` control byte is written with a `byte` store, and the `U32` status with a `dword` one. Never a wider access that touches the neighbour, never two narrower ones.
56
57## Waiting for something else to change
58
59Where 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.
60
61```raz Poll run
62module Poll;
63import Out, Sys, Thread;
64import SYSTEM;
65
66type Dev = record status: INT; data: INT; end;
67
68var dev: Dev;
69
70proc device(_arg: ^U8);
71var d: ^VOLATILE Dev;
72begin
73 d := ^VOLATILE Dev(SYSTEM.ADR(dev));
74 Sys.sleep(20);
75 d^.data := 42;
76 FENCE; (* data before the flag that announces it *)
77 d^.status := 1;
78end;
79
80proc main*;
81var t: ^Thread.Thread; err: ERROR; d: ^VOLATILE Dev; spins: INT;
82begin
83 d := ^VOLATILE Dev(SYSTEM.ADR(dev));
84 t, err := Thread.spawn(device, nil);
85 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
86 while d^.status = 0 do INC(spins); Sys.yield; end;
87 Out.int(d^.data); Out.ln;
88 if spins > 0 then Out.str("it waited for the device\n"); end;
89 Thread.join(t);
90end;
91
92end Poll.
93```
94
95The `FENCE` 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 `data` before `status`. The next lessons are about this.
96
97## What volatile is not
98
99- **Not atomic across a read-modify-write.** `INC(d^.count)` reads and then writes: two accesses, even through a volatile pointer. `XADD` is the atomic one.
100- **Not a lock.** It says each access happens as written; it says nothing about what happens between them.
101- **Not for whole records or arrays.** 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.
102- **Not silently dropped.** A `^VOLATILE T` doesn't convert to a `^T` without a written conversion, so the volatile can't be lost by accident.
103
104The compiler holds the line on each of them:
105
106```raz Wrong
107module Wrong;
108import SYSTEM;
109
110type Pair = record a, b: U32; end;
111
112var cell: Pair;
113
114proc main*;
115var v: ^VOLATILE Pair; p: ^Pair; copy: Pair;
116begin
117 v := ^VOLATILE Pair(SYSTEM.ADR(cell));
118 p := v;
119 copy := v^;
120end;
121
122end Wrong.
123```
124
125```sh
126$ raz Wrong.raz
127```