| 1 | // wake carries "something changed, please draw a frame" signals across |
| 2 | // threads without touching the GC heap. |
| 3 | // |
| 4 | // The Boehm collector only knows the threads V itself spawned. Callbacks that |
| 5 | // arrive on foreign threads — libmpv's render update callback in particular — |
| 6 | // execute V code just fine there, but a store they make into a GC-managed |
| 7 | // object was observed to never become visible to the UI thread. A plain |
| 8 | // counter in data-segment memory does not have that problem, and a POSIX |
| 9 | // semaphore carries the wake: bump() raises the counter and posts the |
| 10 | // semaphore, so the relay thread sleeps until mpv actually has a frame |
| 11 | // instead of polling the counter on a timer. |
| 12 | // |
| 13 | // Sources live in fixed slots, one counter and one semaphore per slot, so two |
| 14 | // players in one process do not wake each other: a slot is claimed at setup |
| 15 | // and handed to the render callback as its user_data, and each relay watches |
| 16 | // only its own counter, so another player's bump never trips its |
| 17 | // cur != seen check. The semaphore posts and waits stay per-slot too — a |
| 18 | // shared one would let a merge across players swallow another relay's wake. |
| 19 | @[has_globals] |
| 20 | module wake |
| 21 | |
| 22 | import sync |
| 23 | |
| 24 | const max_sources = 16 |
| 25 | |
| 26 | struct Sources { |
| 27 | mut: |
| 28 | seqs [max_sources]u64 |
| 29 | sems [max_sources]&sync.Semaphore |
| 30 | } |
| 31 | |
| 32 | __global sources = Sources{} |
| 33 | |
| 34 | fn init() { |
| 35 | for i in 0 .. wake.max_sources { |
| 36 | sources.sems[i] = sync.new_semaphore() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // claim takes an unused slot and returns its index, or -1 when every slot is |
| 41 | // taken. Called from setup on the UI thread, so no lock: slot allocation is |
| 42 | // done once per player while the loop cannot be running relays yet. |
| 43 | pub fn claim() int { |
| 44 | for i in 0 .. wake.max_sources { |
| 45 | if sources.seqs[i] == 0 { |
| 46 | // Reserve the slot before handing it out: a zero would read as |
| 47 | // "no bump yet" to a relay, so claim at one. |
| 48 | sources.seqs[i] = 1 |
| 49 | return i |
| 50 | } |
| 51 | } |
| 52 | return -1 |
| 53 | } |
| 54 | |
| 55 | // bump records that something changed and wakes the slot's relay thread. Safe |
| 56 | // from any thread, foreign ones included: the counter store happens before the |
| 57 | // post, and sem_post's full barrier makes it visible to whoever the post |
| 58 | // wakes. Concurrent bumps may merge, which is fine — a redraw ask is a redraw |
| 59 | // ask. |
| 60 | pub fn bump(source int) { |
| 61 | if source < 0 || source >= wake.max_sources { |
| 62 | return |
| 63 | } |
| 64 | sources.seqs[source] = sources.seqs[source] + 1 |
| 65 | sources.sems[source].post() |
| 66 | } |
| 67 | |
| 68 | // current reports a slot's counter value, for the relay that remembers the |
| 69 | // last value it saw. |
| 70 | pub fn current(source int) u64 { |
| 71 | if source < 0 || source >= wake.max_sources { |
| 72 | return 0 |
| 73 | } |
| 74 | return sources.seqs[source] |
| 75 | } |
| 76 | |
| 77 | // wait blocks until at least one bump happened on this slot. Only the slot's |
| 78 | // relay thread waits; posts that queue up while it is busy drain through |
| 79 | // repeated non-sleeping waits, and the counter merges the backlog into one |
| 80 | // redraw ask. |
| 81 | pub fn wait(source int) { |
| 82 | if source < 0 || source >= wake.max_sources { |
| 83 | return |
| 84 | } |
| 85 | sources.sems[source].wait() |
| 86 | } |
| 87 | |
| 88 | // release marks a slot unused again so another player can take it. The relay |
| 89 | // has already been joined by then, so no bump can land after the zeroing. |
| 90 | pub fn release(source int) { |
| 91 | if source < 0 || source >= wake.max_sources { |
| 92 | return |
| 93 | } |
| 94 | sources.seqs[source] = 0 |
| 95 | } |
| 96 | |