Skip to content

Commit e5fc329

Browse files
committed
Design doc feedback from #26621. NFC
1 parent ecb9e39 commit e5fc329

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

docs/design/01-precise-futex-wakeups.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CPU wakeups and increased latency for events.
2424
## Non-Goals
2525
- **Main Browser Thread**: Changes to the busy-wait loop in `futex_wait_main_browser_thread` are out of scope.
2626
- **Direct Atomics Usage**: Threads that call `atomic.wait` directly (bypassing `emscripten_futex_wait`) will remain un-interruptible.
27-
- **Wasm Workers**: Wasm Worker do not have a `pthread` structure, they are not covered by this design.
27+
- **Wasm Workers**: Wasm Workers do not have a `pthread` structure, so they are not covered by this design.
2828

2929
## Proposed Design
3030

@@ -85,7 +85,7 @@ When a thread needs to wake another thread for a side-channel event (e.g., in `p
8585
3. If `target_addr` is not NULL:
8686
- Read `start_c = target->wait_counter` (`SEQ_CST`).
8787
- Enter a loop:
88-
- Call `emscripten_futex_wake(target_addr, 1)`.
88+
- Call `emscripten_futex_wake(target_addr, INT_MAX)`.
8989
- Exit loop if `target->wait_counter != start_c` OR `target->waiting_on_address != target_addr`.
9090
- **Yield**: Call `sched_yield()` (or a small sleep) to allow the target thread to proceed if it is currently being scheduled.
9191
@@ -106,7 +106,7 @@ counter.
106106
### 5. Overlapping and Spurious Wakeups
107107
The design must handle cases where "real" wakeups (triggered by the application) and "side-channel" wakeups (cancellation/mailbox) occur simultaneously.
108108
109-
1. **Spurious Wakeups for Other Threads**: If multiple threads are waiting on the same address (e.g., a shared mutex), a side-channel `atomic_wake(addr, 1)` targeted at Thread A might be delivered by the kernel to Thread B.
109+
1. **Spurious Wakeups for Other Threads**: If multiple threads are waiting on the same address (e.g., a shared mutex), a side-channel `atomic_wake(addr, 1)` targeting Thread A could also wake Thread B.
110110
- **Thread B's response**: It will wake up, increment its `wait_counter`, see that its `wait_reasons` are empty, and return `0` to its caller.
111111
- **Thread C (the waker)**: It will see that Thread A's `wait_counter` has *not* changed and `waiting_on_address` is still `addr`. It will therefore continue its loop and call `atomic_wake` again until Thread A is finally woken.
112112
- **Result**: Thread B experiences a "spurious" wakeup. This is acceptable and expected behavior for futex-based synchronization.
@@ -118,7 +118,8 @@ The `wait_counter` is a `uint32_t` and will wrap around to zero after $2^{32}$ w
118118
1. **Impossibility of Racing**: For the waker to "miss" a wake-up due to wrap-around, the waiter would have to wake up and re-enter a sleep state exactly $2^{32}$ times in the very brief window between the waker's `atomic_wake` and its subsequent check of `wait_counter`. Even at extreme wakeup frequencies (e.g., 1 million per second), this would take over an hour.
119119
2. **Address Change Check**: The waker loop also checks `target->waiting_on_address != target_addr`. If the waiter wakes up and either stops waiting or starts waiting on a *different* address, the waker will exit the loop regardless of the counter value.
120120
121-
### 6. Benefits
121+
## Benefits
122+
122123
- **Lower Power Consumption**: Threads can sleep indefinitely (or for the full duration of a user-requested timeout) without periodic wakeups.
123124
- **Lower Latency**: Mailbox events and cancellation requests are processed immediately rather than waiting for the next 1ms or 100ms tick.
124125
- **Simpler Loop**: The complex logic for calculating remaining timeout slices in `emscripten_futex_wait` is removed.

docs/design/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@
33
This directory contains design documents for emscripten features and major
44
changes/refactors.
55

6-
We are experimenting with keeping these document here under source control with
6+
We are experimenting with keeping these documents here under source control with
77
the hope that this will increase understandability of the codebase. This has
8-
some advantages over doing all planning in Google Docs or GitHub issues. For
9-
example, it allows us to track the history of designs and it allows them to be
10-
searchable using standard tools like `git grep`.
8+
some advantages over doing all our planning in Google Docs or GitHub issues.
9+
For example, it allows us to track the history of designs and it allows them to
10+
be searchable using standard tools like `git grep`.
11+
12+
## Document Format
13+
14+
Each document in this directory should be a markdown file. At the top of each
15+
document should be a `Status` which can be either `Draft`, `Accepted`,
16+
`Completed`.
17+
18+
When a document is marked as `Completed` it should also be updated such that
19+
it is clear the work has been done, and is now in the past. For example,
20+
phrases such as "The current behavior" should be replaced with "The previous
21+
behavior".

0 commit comments

Comments
 (0)