Landin library reference discuss source

compiler/tests/fixtures/runtime/thread-lock-one-thread/main.ldn

1--  The lock and the started flag on the one thread a program starts
2--  with.  Nothing here can contend, so every take finds the lock free or
3--  is a take_if_free that does not wait; a take that waits, and the give
4--  that wakes it, need a second thread.
5import hosted/thread/lock
6
7mut shared: lock.lock = zeroed
8
9--  Each check returns its own status, so a failure says which one.
10cycle: (held: ptr mut lock.lock, first: i32) -> (status: i32) =
11    status = first
12    return when not lock.take_if_free(held)
13    status = first + 1
14    return when lock.take_if_free(held)
15    lock.give(held)
16    status = first + 2
17    lock.take(held)
18    return when lock.take_if_free(held)
19    lock.give(held)
20    status = first + 3
21    return when not lock.take_if_free(held)
22    lock.give(held)
23    status = 0
24end cycle
25
26public main: () -> (code: i32) =
27    code = 2
28    return when lock.threads_started()
29    code = cycle(addr shared, 10)
30    return when code <> 0
31    mut local: lock.lock = lock.new()
32    code = cycle(addr local, 20)
33    return when code <> 0
34    lock.note_start()
35    code = 3
36    return when not lock.threads_started()
37    lock.note_start()
38    return when not lock.threads_started()
39    code = cycle(addr shared, 30)
40    return when code <> 0
41    code = 42
42end main