ludic/examples/library/threads.ludic
Orkuncakilkaya 8cf4b3fed6 fix(lang): the action drain is the program's; --check runs every check a build makes; a registry key named count is refused; name lookups are tables
- the function that calls every reducer (and the action queue) is written in the program's own
  file: in the first action's file it belonged to that module, depended on every module with a
  reducer, and joined a game's modules into one 69-module cycle (examples/actions/modules and a
  ludic deps case hold it); the state instances, the queue and the reducers make no deps edges
- ludicc --check / ludic build --check lower the program too and write nothing, so the code
  writer's refusals are in it: a bind to a function that is gone, an unknown name (and the checker
  now refuses fn <missing> itself); rejects bind_missing_fn, unknown_name, registry_count_key
- def R count is refused: its constant would be PREFIX_COUNT, the registry's size
- a file's module, package, trust and numbers-float are tables, and from the check on the lookups
  of functions, enums, records, globals and externs are too (tagged enums kept as a list): Maroon
  Lake's check-only build went from about 20 s to 7 s including lowering, its IR from about 2
  minutes to under 10 s; duplicate declarations are found by table, not a pair of loops
- threads.ludic's pool check gives each call a little work, so a busy machine cannot run them all
  on the caller before a worker wakes (it failed one run in three under load)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 23:34:43 +03:00

62 lines
2.4 KiB
Text

# threads.ludic — Job.parallel_for on real OS threads, with Sync.* made safe to share.
# Each assertion that holds prints its number, so a full run prints:
# 1 2 3 4 5 6 7
# A worker is a top-level function taking (i: int, ctx: pointer-like) that computes on what ctx
# points at; `fn name` passes it. Workers never touch the world or a list another thread can see.
program Threads {
const N: int = 20000
state ThreadsState {
calls: int = 0 # an atomic handle, made on the main thread before any work
lock: int = 0 # a mutex handle
total: long = 0 # guarded by `lock`
}
# out[i] = i * i: every index written exactly once, by whichever thread claimed it
function square(i: int, out: words) -> void { out[i] = i * i }
# count the call atomically, and add i to a shared total under the mutex
function tally(threads_st: mut ThreadsState, i: int, ctx: pointer) -> void {
Sync.add(threads_st.calls, 1)
Sync.lock(threads_st.lock)
threads_st.total = threads_st.total + i
Sync.unlock(threads_st.lock)
}
# remember which calls ran on a pool thread - each with a little work in it, so on a busy machine
# the calling thread cannot claim every index before a worker wakes
function placed(i: int, out: words) -> void {
var spin = 0
for k in 0 .. 2000 { spin = spin + k % 7 }
if Job.is_worker() { out[i] = 1 + spin * 0 } else { out[i] = spin * 0 }
}
entry (threads_st: mut ThreadsState) {
if Sync.cpu_count() >= 1 { print(1) }
if not Job.is_worker() { print(2) }
let sq = words(N)
Job.parallel_for(N, fn square, sq)
var right = true
for i in 0 .. N { if sq[i] != i * i { right = false } }
if right { print(3) }
threads_st.calls = Sync.atomic()
threads_st.lock = Sync.mutex()
Job.parallel_for(N, fn tally, null)
if Sync.get(threads_st.calls) == N { print(4) }
let want: long = N * (N - 1) / 2
if threads_st.total == want { print(5) }
let ran = words(N)
Job.parallel_for(N, fn placed, ran)
var pooled = 0
for i in 0 .. N { pooled += ran[i] }
# with more than one core some calls ran on workers; with one, all ran here
if (Sync.cpu_count() > 1 and pooled > 0) or (Sync.cpu_count() == 1 and pooled == 0) { print(6) }
Job.parallel_for(0, fn square, sq) # nothing to do: returns at once
let fn = 7 # `fn` is still an ordinary name, before `and` too
if 0 < fn and fn < 8 { print(fn) }
}
}