.\" Made by website/plain/build.mjs from website/words/lock.md; change that file. .TH SG-LOCK 7 2026-11-28 "SG" "SG reference" .SH NAME lock \- a short block that alone touches a locked value; it never waits, so locks cannot deadlock .SH SYNOPSIS .TP \fBlock v as name { ... }\fR takes a shared locked value; name is a ref to what it holds, for the block .TP \fBlock a as x, b as y { ... }\fR takes several values together, in a fixed order .TP \fBlock v as name when condition { ... }\fR waits, with the lock let go, until the condition holds .TP \fBawait lock v as name when condition { ... }\fR the same in an async function .TP \fBlock Name as name { ... }\fR takes a static locked value .SH DESCRIPTION \fBlock v as name\fR takes a \fBshared locked\fR value: only one task is inside a \fBlock\fR block of the value at a time, and \fBname\fR is a \fBref\fR to what it holds until the closing \fB}\fR\&. Whatever way the block is left, its end, \fBreturn\fR, \fBbreak\fR, \fBcontinue\fR or \fBtry\fR, the lock is let go there. .PP A \fBlock\fR block never waits. Inside it there is no other \fBlock\fR, no \fBwait\fR, no \fBawait\fR, no send, receive or \fBselect\fR on a channel, and no call of a function that does one of these, followed through the calls. A task that holds a lock therefore always lets it go, and locks cannot deadlock. Values needed together are locked in one block: \fBlock a as x, b as y\fR takes them in the order of their addresses, whatever order it names them in, so two tasks never hold one each. A task may be started in a \fBlock\fR block; since it is not waited for there, it goes into a list of tasks or a \fBTaskGroup\fR made before the block, and is waited for after it. .PP \fBwhen\fR waits before the block, not in it. The condition is computed under the lock, with each name lent to read; while it fails the lock is let go and the task sleeps. Each \fBlock\fR block on the value that ends wakes those that wait to look again, so no task wakes another by hand. The condition looks only at the locked values, since a change elsewhere would wake nobody. That no more will come is said by a field of the value, set in a \fBlock\fR block. .PP In the interpreter one task runs at a time, so the output is the same on every run. Compiled code runs the tasks at the same time, and \fBlock\fR takes a mutex of the system. .SH EXAMPLES .SS "Several tasks, one value" .RS 4 .nf void countWords(shared locked HashMap counts, string line) { for word in line.split(" ") { lock counts as c { var slot = c.getOrAdd(word, 0) slot += 1 } } } void main() { var counts = new shared locked HashMap() var a = Task.run(countWords, share counts, "the cat and the dog") var b = Task.run(countWords, share counts, "the bird and the cat") a.wait() b.wait() lock counts as c { for word in "the cat dog bird and".split(" ") { print($"{word} {c[word]}") } } } .fi .RE .PP .RS 4 .nf the 4 cat 2 dog 1 bird 1 and 2 .fi .RE .PP The split runs in parallel; only the additions take turns. \fBslot\fR borrows a count inside the map, which lasts as long as the block. .SS "Leaving the block early" .RS 4 .nf struct Seats { List taken int size } bool book(shared locked Seats seats, string name) { lock seats as s { if s.taken.length() >= s.size { return false } s.taken.add(name) } return true } void main() { var seats = new shared locked Seats { taken = List(), size = 2 } var asks = List>() for name in "Ada Pasha Linus Grace".split(" ") { asks.add(Task.run(book, share seats, name.clone())) } var booked = 0 for ok in Task.whenAll(asks).wait() { if ok { booked += 1 } } lock seats as s { print($"{booked} booked, {s.taken.length()} seats taken of {s.size}") } } .fi .RE .PP .RS 4 .nf 2 booked, 2 seats taken of 2 .fi .RE .PP The test and the change are made under one lock, so no two tasks take the last seat. \fBreturn false\fR lets the lock go on its way out; which two names got seats changes from run to run, how many does not. .SS "Two values together" .RS 4 .nf struct Account { string name long balance } void transfer(shared locked Account from, shared locked Account to, long amount) { lock from as a, to as b { a.balance -= amount b.balance += amount } } void main() { var alice = new shared locked Account { name = "alice", balance = 100 } var bob = new shared locked Account { name = "bob", balance = 100 } var tasks = List>() for _ in 0..10 { tasks.add(Task.run(transfer, share alice, share bob, 5)) tasks.add(Task.run(transfer, share bob, share alice, 3)) } Task.whenAll(tasks).wait() lock alice as a, bob as b { print($"{a.name} {a.balance}, {b.name} {b.balance}") } } .fi .RE .PP .RS 4 .nf alice 80, bob 120 .fi .RE .PP Half the transfers name \fBalice\fR first and half \fBbob\fR, yet each takes the two in the same order, so no two of them wait for each other. .SS "Waiting for a condition" .RS 4 .nf struct Jobs { List waiting bool done } long work(shared locked Jobs jobs) { long sum = 0 while true { int? job = null lock jobs as j when j.waiting.length() > 0 || j.done { job = j.waiting.removeLast() } if job is null { return sum } sum += job * job } } void main() { var jobs = new shared locked Jobs { waiting = List(), done = false } var workers = List>() for _ in 0..3 { workers.add(Task.run(work, share jobs)) } for n in 1..11 { lock jobs as j { j.waiting.add(n) } } lock jobs as j { j.done = true } long total = 0 for sum in Task.whenAll(workers).wait() { total += sum } print(total) } .fi .RE .PP .RS 4 .nf 385 .fi .RE .PP Each worker sleeps until there is a job or \fBdone\fR is set. Every \fBlock\fR block in \fBmain\fR wakes them to look again; nothing calls a \fBnotify\fR\&. Once the list is empty and \fBdone\fR is set, \fBremoveLast\fR gives \fBnull\fR and the worker ends. .SS "In an async function" .RS 4 .nf struct Mailbox { List letters bool closed } async int collect(shared locked Mailbox box) { var count = 0 while true { var done = false await lock box as b when b.letters.length() > 0 || b.closed { if b.letters.length() == 0 { done = true } else { _ = b.letters.removeLast() count += 1 } } if done { return count } } } async void main() { var box = new shared locked Mailbox { letters = List(), closed = false } var reader = collect(share box) for i in 0..5 { await Task.delay(1) lock box as b { b.letters.add($"letter {i}".toString()) } } lock box as b { b.closed = true } print($"{await reader} letters read") } .fi .RE .PP .RS 4 .nf 5 letters read .fi .RE .PP \fBawait lock\fR lets the thread go on with other tasks while this one waits. A \fBlock\fR without \fBwhen\fR is written as it is: it never waits, so there is nothing to await. .SH "WHAT STOPS THE PROGRAM" .SS "One value under two names" .RS 4 .nf struct Account { long balance } void transfer(shared locked Account from, shared locked Account to, long amount) { lock from as a, to as b { a.balance -= amount b.balance += amount } } void main() { var alice = new shared locked Account { balance = 100 } var bob = share alice transfer(share alice, share bob, 10) } .fi .RE .PP .RS 4 .nf panic: `a` and `b` are one value, which a `lock` takes once --> example.sg:6:21 6 | lock from as a, to as b { | ^^ .fi .RE .PP \fBalice\fR and \fBbob\fR are two owners of one account, so the compiler cannot tell. Two names would change one value at once; a transfer to oneself is tested for before the \fBlock\fR\&. .SS "Waiting for what nobody can change" .RS 4 .nf struct Queue { List items } void main() { var queue = new shared locked Queue { items = List() } lock queue as q when q.items.length() > 0 { print(q.items.removeLast() ?? 0) } } .fi .RE .PP .RS 4 .nf panic: this task waits until `q.items.length() > 0`, and no other task could make it hold: it is the only owner of `queue` --> example.sg:7:26 7 | lock queue as q when q.items.length() > 0 { | ^^^^^^^^^^^^^^^^^^^^ hint: a task with no more to give says so inside the value before it lets it go, in a field such as `done`: `lock queue as q { q.done = true }`, and the condition looks at it: `when ... || q.done` .fi .RE .PP In the interpreter and in compiled code alike, a task that would wait for ever is stopped with the line it waits on. .SH "WHAT THE COMPILER REFUSES" .SS "A lock inside a lock" .RS 4 .nf struct Account { long balance } void main() { var alice = new shared locked Account { balance = 100 } var bob = new shared locked Account { balance = 50 } lock alice as a { lock bob as b { a.balance -= 10 b.balance += 10 } } } .fi .RE .PP .RS 4 .nf error: a `lock` block does not take another lock a task that holds a lock never waits, so it always lets the lock go: locks and waits cannot deadlock --> example.sg:9:9 9 | lock bob as b { | ^^^^^^^^^^^^^^^ hint: lock both in one block, which takes them in a fixed order: `lock a as x, b as y { ... }` .fi .RE .SS "Waiting for a task" .RS 4 .nf int answer() { return 42 } void main() { var total = new shared locked 0 var task = Task.run(answer) lock total as t { t += task.wait() } } .fi .RE .PP .RS 4 .nf error: a `lock` block does not wait for a task a task that holds a lock never waits, so it always lets the lock go: locks and waits cannot deadlock --> example.sg:9:19 9 | t += task.wait() | ^^^^ hint: wait for the task before the block or after it .fi .RE .SS "A call that waits" .RS 4 .nf int answer() { return 42 } int ask() { var task = Task.run(answer) return task.wait() } void main() { var total = new shared locked 0 lock total as t { t += ask() } } .fi .RE .PP .RS 4 .nf error: `ask` waits, and a `lock` block does not wait a task that holds a lock never waits, so it always lets the lock go: locks and waits cannot deadlock --> example.sg:13:14 13 | t += ask() | ^^^ `ask` waits for a task here: --> example.sg:7:17 7 | return task.wait() | ^^^^ hint: call `ask` before the block or after it .fi .RE .PP The compiler follows the calls, and shows where the wait is. .SS "await inside" .RS 4 .nf async int answer() { await Task.delay(5) return 42 } async void main() { var total = new shared locked 0 lock total as t { t += await answer() } } .fi .RE .PP .RS 4 .nf error: a `lock` block does not await a task a task that holds a lock never waits, so it always lets the lock go: locks and waits cannot deadlock --> example.sg:9:14 9 | t += await answer() | ^^^^^^^^^^^^^^ hint: await the task before the block or after it .fi .RE .SS "A send on a channel" .RS 4 .nf void main() { var total = new shared locked 0 var numbers = Channel(1) var out = numbers.sender() lock total as t { t += 1 _ = out.send(t) } } .fi .RE .PP .RS 4 .nf error: `send` waits on a channel, and a `lock` block does not wait a task that holds a lock never waits, so it always lets the lock go: locks and waits cannot deadlock --> example.sg:7:17 7 | _ = out.send(t) | ^^^^ hint: send before the block or after it .fi .RE .SS "A condition that changes the value" .RS 4 .nf struct Queue { List items } void main() { var queue = new shared locked Queue { items = List { 1 } } lock queue as q when q.items.removeLast() is not null { print("took one") } } .fi .RE .PP .RS 4 .nf error: `removeLast` changes the list, but it is reached through a `read` borrow --> example.sg:7:26 7 | lock queue as q when q.items.removeLast() is not null { | ^^^^^^^ hint: the condition only looks at what is locked; change it in the block .fi .RE .PP The condition may be computed many times; the change is made once, in the block. .SS "A condition that looks elsewhere" .RS 4 .nf struct Queue { List items } static atomic bool Stop = atomic false void main() { var queue = new shared locked Queue { items = List() } lock queue as q when q.items.length() > 0 || Stop.load() { print("ready") } } .fi .RE .PP .RS 4 .nf error: the condition of `when` reads an `atomic`, and a task waiting for it is woken only when a `lock` block on the same value ends a change outside the locked value would not wake it, and it would wait for ever --> example.sg:9:50 9 | lock queue as q when q.items.length() > 0 || Stop.load() { | ^^^^ hint: keep what the condition looks at inside the locked value, and change it in a `lock` block .fi .RE .PP A \fBshared atomic\fR given to the task is refused there too, as is any \fBatomic\fR\&. The clock, random bytes and input or output are refused there for the same reason. .SS "when in an async function, without await" .RS 4 .nf struct Queue { List items } async void main() { var queue = new shared locked Queue { items = List { 1 } } lock queue as q when q.items.length() > 0 { print(q.items.removeLast() ?? 0) } } .fi .RE .PP .RS 4 .nf error: `lock ... when` waits, and an async function awaits what waits: its task waits, and its thread goes on --> example.sg:7:5 7 | lock queue as q when q.items.length() > 0 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: write `await lock ... when ...` .fi .RE .SS "Giving the value away inside its block" .RS 4 .nf struct Jobs { List waiting } void keep(shared locked Jobs jobs) { } void main() { var jobs = new shared locked Jobs { waiting = List() } lock jobs as j { j.waiting.add(1) keep(jobs) } } .fi .RE .PP .RS 4 .nf error: `jobs` cannot be moved here: `j` borrows from it and is used again later the block of `j` holds what it opened until the block ends, and lets go of it there --> example.sg:12:14 12 | keep(jobs) | ^^^^ `j` borrowed it here: --> example.sg:10:10 10 | lock jobs as j { | ^^^^ hint: move or change it after the block .fi .RE .PP The block lets go of the lock at its \fB}\fR, so the value must still be there: it is not given away or written over inside. \fBkeep(share jobs)\fR gives one more owner and keeps this one. .SS "A value that is not locked" .RS 4 .nf void main() { var total = new shared checked 0 lock total as t { t += 1 } } .fi .RE .PP .RS 4 .nf error: `lock` is for a `shared locked` value, and this is `shared checked int`, which one task changes --> example.sg:3:10 3 | lock total as t { | ^^^^^ hint: `ref total as name { ... }` changes it for a block .fi .RE .PP A value with one owner needs no lock: it is changed where it is. .SH "SEE ALSO" .BR sg-locked (7), .BR sg-as (7), .BR sg-shared (7), .BR sg-atomic (7), .BR sg-await (7) .PP https://sg\-lang.org/reference/lock.html