// wake carries "something changed, please draw a frame" signals across // threads without touching the GC heap. // // The Boehm collector only knows the threads V itself spawned. Callbacks that // arrive on foreign threads — libmpv's render update callback in particular — // execute V code just fine there, but a store they make into a GC-managed // object was observed to never become visible to the UI thread. A plain // counter in data-segment memory does not have that problem, and a POSIX // semaphore carries the wake: bump() raises the counter and posts the // semaphore, so the relay thread sleeps until mpv actually has a frame // instead of polling the counter on a timer. // // Sources live in fixed slots, one counter and one semaphore per slot, so two // players in one process do not wake each other: a slot is claimed at setup // and handed to the render callback as its user_data, and each relay watches // only its own counter, so another player's bump never trips its // cur != seen check. The semaphore posts and waits stay per-slot too — a // shared one would let a merge across players swallow another relay's wake. @[has_globals] module wake import sync const max_sources = 16 struct Sources { mut: seqs [max_sources]u64 sems [max_sources]&sync.Semaphore } __global sources = Sources{} fn init() { for i in 0 .. wake.max_sources { sources.sems[i] = sync.new_semaphore() } } // claim takes an unused slot and returns its index, or -1 when every slot is // taken. Called from setup on the UI thread, so no lock: slot allocation is // done once per player while the loop cannot be running relays yet. pub fn claim() int { for i in 0 .. wake.max_sources { if sources.seqs[i] == 0 { // Reserve the slot before handing it out: a zero would read as // "no bump yet" to a relay, so claim at one. sources.seqs[i] = 1 return i } } return -1 } // bump records that something changed and wakes the slot's relay thread. Safe // from any thread, foreign ones included: the counter store happens before the // post, and sem_post's full barrier makes it visible to whoever the post // wakes. Concurrent bumps may merge, which is fine — a redraw ask is a redraw // ask. pub fn bump(source int) { if source < 0 || source >= wake.max_sources { return } sources.seqs[source] = sources.seqs[source] + 1 sources.sems[source].post() } // current reports a slot's counter value, for the relay that remembers the // last value it saw. pub fn current(source int) u64 { if source < 0 || source >= wake.max_sources { return 0 } return sources.seqs[source] } // wait blocks until at least one bump happened on this slot. Only the slot's // relay thread waits; posts that queue up while it is busy drain through // repeated non-sleeping waits, and the counter merges the backlog into one // redraw ask. pub fn wait(source int) { if source < 0 || source >= wake.max_sources { return } sources.sems[source].wait() } // release marks a slot unused again so another player can take it. The relay // has already been joined by then, so no bump can land after the zeroing. pub fn release(source int) { if source < 0 || source >= wake.max_sources { return } sources.seqs[source] = 0 }