// A builder past the send walk's copy limit (4 KiB), captured by spawned // actors, is promoted to shared: its count is negative and changed // atomically by each holder. Each actor builds and appends to it in a // loop, so the `unique` tests of `std/bytes`' builder (builder.rs: // `reserve`, `tl_bytes_build`, `tl_bytes_build_string`) read a count other // threads change. Under TSan (scripts/tsan.sh), a plain read there is a // data race; it must be an atomic one (`heap::rc`), as `array.rs`'s is. import std/bytes import std/io import std/process.{type proc} import std/string fn grow :: b: bytes.Builder n: Int -> bytes.Builder { if n == 0 { b } else { grow (bytes.append_byte b 65) (n - 1) } } fn work :: b: bytes.Builder n: Int acc: Int -> Int { if n == 0 { acc } else { let built = bytes.build b let s = bytes.build_string b let c = bytes.append_byte b 66 work b (n - 1) ( acc + bytes.length built + string.length s + bytes.length (bytes.build c) ) } } pub fn main :: -> () / { let b = grow (bytes.builder ()) 8000 let done = process.subject () let _ = { use <- process.spawn process.send done (work b 300 0) } let _ = { use <- process.spawn process.send done (work b 300 0) } let _ = { use <- process.spawn process.send done (work b 300 0) } let _ = { use <- process.spawn process.send done (work b 300 0) } let t = process.receive_forever done + process.receive_forever done + process.receive_forever done + process.receive_forever done io.println (show t) } //> 28801200