.\" Made by website/plain/build.mjs from website/words/locked.md; change that file. .TH SG-LOCKED 7 2026-11-28 "SG" "SG reference" .SH NAME locked \- shared locked T: a value that tasks reach only inside a lock .SH SYNOPSIS .TP \fBnew shared locked x\fR puts x on the heap behind a lock, owned by this task .TP \fBshared locked T\fR the type of an owner .TP \fBshare v\fR one more owner, to give to another task .TP \fBweak locked T\fR a look that does not keep the value alive .TP \fBstatic locked T Name = e\fR one for the whole program, with no owners to count .TP \fBlock v as name { ... }\fR the only place where what it holds is reached .SH DESCRIPTION A \fBshared locked T\fR is a value that several tasks own and change. What it holds is reached only inside a \fBlock\fR block, and only one task is inside a \fBlock\fR block of the value at a time. Two tasks therefore never change it at once, nor read it while another changes it: the compiler rules the data race out, and nothing is left to check while the program runs. .PP Outside a \fBlock\fR, what it holds is not reached at all, not even read: a field, a method, \fBfor\fR, \fBprint\fR, \fB==\fR and \fBread x\fR are errors. What does not touch it works: \fBshare\fR, \fBweak\fR, \fBsame\fR and \fB\&.clone()\fR, which also gives one more owner, not a copy. .PP \fBlocked\fR is written after \fBshared\fR or \fBweak\fR, or in a \fBstatic\fR\&. What it holds follows the rule of what goes to another task: it owns all it holds, so no borrow, no \fBshared checked\fR value and no \fBTask\fR is inside. .PP The cost is a mutex of the system in front of the value, 64 bytes on macOS and 48 on Linux, taken and let go by each \fBlock\fR\&. A number or a \fBbool\fR that tasks change one step at a time is cheaper as an \fBatomic\fR\&. A value that one task changes through several owners is a \fBshared checked T\fR\&. Work handed from task to task, rather than kept in one place, goes through a \fBChannel\fR\&. .SH EXAMPLES .SS "One value, reached in a lock" .RS 4 .nf void main() { var hits = new shared locked 0 lock hits as h { h += 1 } lock hits as h { h += 1 print(h) } } .fi .RE .PP .RS 4 .nf 2 .fi .RE .PP \fBh\fR is a \fBref int\fR for the block: the number itself, lent to change. .SS "Changed by several tasks" .RS 4 .nf void collect(shared locked List found, int from, int to) { for n in from..to { if n % 7 == 0 { lock found as list { list.add(n) } } } } void main() { var found = new shared locked List() var low = Task.run(collect, share found, 0, 50) var high = Task.run(collect, share found, 50, 100) low.wait() high.wait() lock found as list { list.sort() print(list) } } .fi .RE .PP .RS 4 .nf [0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98] .fi .RE .PP \fBshare found\fR gives each task one more owner of the same list. The tasks add in any order, so the list is sorted before it is printed. .SS "A field of a struct" .RS 4 .nf struct Stats { int requests long bytes } struct Server { string name shared locked Stats stats } void serve(Server server, int count) { for _ in 0..count { lock server.stats as s { s.requests += 1 s.bytes += 100 } } } void main() { var stats = new shared locked Stats { requests = 0, bytes = 0 } var api = Task.run(serve, Server { name = "api", stats = share stats }, 300) var admin = Task.run(serve, Server { name = "admin", stats = share stats }, 200) api.wait() admin.wait() lock stats as s { print($"{s.requests} requests, {s.bytes} bytes") } } .fi .RE .PP .RS 4 .nf 500 requests, 50000 bytes .fi .RE .PP A struct that several tasks change in part keeps that part in a \fBshared locked\fR field. Each server goes to its own task, and both point to one \fBStats\fR\&. .SS "One for the whole program" .RS 4 .nf static locked List Log = List() void note(string line) { lock Log as log { log.add(line) } } void work(int id) { note($"task {id} done".toString()) } void main() { var a = Task.run(work, 1) var b = Task.run(work, 2) a.wait() b.wait() lock Log as log { log.sort() for line in log { print(line) } } } .fi .RE .PP .RS 4 .nf task 1 done task 2 done .fi .RE .PP A \fBstatic locked\fR value lives until the program ends, so no owner is counted and none is given to the tasks. It is never assigned; it is changed inside \fBlock\fR, as any locked value. .SS "A look that does not keep it alive" .RS 4 .nf struct Cache { List names } void remember(read weak locked Cache look, string name) { var owner = share look if owner is null { print($"{name}: the cache is gone") return } lock owner as c { c.names.add(name.clone()) print($"{name}: {c.names.length()} kept") } } void main() { var cache = new shared locked Cache { names = List() } var look = weak cache remember(read look, "tea") remember(read look, "bread") cache = new shared locked Cache { names = List() } remember(read look, "milk") } .fi .RE .PP .RS 4 .nf tea: 1 kept bread: 2 kept milk: the cache is gone .fi .RE .PP A \fBweak locked\fR value is not locked itself: \fBshare look\fR asks for an owner first, which is \fBnull\fR once the value is gone. .SH "WHAT THE COMPILER REFUSES" .SS "Reading it outside a lock" .RS 4 .nf struct Stats { int requests } void main() { var stats = new shared locked Stats { requests = 0 } print(stats.requests) } .fi .RE .PP .RS 4 .nf error: what a `shared locked` value holds is reached only inside a `lock` block, where no other task reaches it --> example.sg:7:11 7 | print(stats.requests) | ^^^^^ hint: `lock stats as name { ... }` .fi .RE .PP Even a read could see a change half made by another task. .SS "locked alone" .RS 4 .nf struct Server { string name locked int requests } void main() { } .fi .RE .PP .RS 4 .nf error: `locked` is written after `shared` or `weak`: several tasks own the value it holds --> example.sg:3:5 3 | locked int requests | ^^^^^^ hint: `shared locked T` for an owner, `weak locked T` for a look at one .fi .RE .SS "new locked" .RS 4 .nf void main() { var requests = new locked 0 } .fi .RE .PP .RS 4 .nf error: `locked` is written after `shared`: `new shared locked ...` --> example.sg:2:24 2 | var requests = new locked 0 | ^^^^^^ .fi .RE .SS "Something only one task may hold" .RS 4 .nf struct Board { shared checked List scores } void main() { var board = new shared locked Board { scores = new shared checked List() } } .fi .RE .PP .RS 4 .nf error: a `locked` value cannot hold a `Board`, which holds a `shared checked List` (the field `scores` of `Board`): a `shared checked List` is checked within one task; one that several tasks change is `shared locked` what goes to another task, comes back from one or is held by a `locked` value owns all it holds: no borrow, no `shared checked` value, no `Task` --> example.sg:6:35 6 | var board = new shared locked Board { scores = new shared checked List() } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP The checks of a \fBshared checked\fR value are not made for several tasks. The scores are kept in the \fBBoard\fR itself, which the lock already guards. .SS "checked and locked together" .RS 4 .nf void count(shared checked locked int total) { } void main() { } .fi .RE .PP .RS 4 .nf error: a value with several owners is `checked` or `locked`, not both `checked` lets one task change it, each access checked; `locked` lets several tasks change it, inside a `lock` block --> example.sg:1:27 1 | void count(shared checked locked int total) { | ^^^^^^ .fi .RE .SH "SEE ALSO" .BR sg-lock (7), .BR sg-shared (7), .BR sg-atomic (7), .BR sg-checked (7), .BR sg-share (7), .BR sg-weak (7), .BR sg-static (7) .PP https://sg\-lang.org/reference/locked.html