-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: multithreading and thread safety #478
Open
kainino0x
wants to merge
4
commits into
webgpu-native:main
Choose a base branch
from
kainino0x:doc-multithreading
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Multithreading {#Multithreading} | ||
|
||
`webgpu.h` implementations are allowed to require that all returned objects, except for `WGPUInstance`, can only be used from the thread they were created on, causing undefined behavior otherwise. | ||
|
||
Where multithreading is supported: | ||
|
||
- The implementation must provide the documented @ref ThreadSafety guarantees. | ||
- All objects must be freely usable on any thread at any time, except: | ||
- Where APIs are specified as being non-thread-safe, they must mutexed. | ||
- The exceptions for Wasm, below. | ||
- State must be thread-global, except where otherwise defined. | ||
- For example, buffer map state is thread-global, and mapped memory ranges may be used from any thread. | ||
- Note: Error scope state is thread-local. See @ref ErrorScopes. | ||
|
||
Native (non-Wasm) implementations **should** support multithreading. | ||
|
||
## In WebAssembly | ||
|
||
At the time of this writing, the JS WebGPU API is not multithreading-capable. | ||
Initial `webgpu.h` implementations will not be multithreading-capable. | ||
|
||
Wasm-on-JS implementations *may* implement multithreading by proxying calls to a single JS "thread", as long as the C API behavior is conformant. | ||
|
||
Once the JS API is multithreading-capable, there are still expected to be some thread-local behaviors: | ||
|
||
- Buffer mapping state may be thread-local. | ||
- Implementations *should* make this state thread-global if feasible (e.g. by proxying all mapping calls to a single JS thread). | ||
- Any object which is non-shareable or non-transferable in JS may also be fully or partially non-shareable or non-transferable in Wasm (e.g. encoder objects may be thread-locked, similar to how they are not thread safe in `webgpu.h` in general). (In Rust terms, shareable = `Send`+`Sync`, transferable = `Send`.) | ||
- Implementations *may* make them shareable. (For example, proxying all encoder methods to a single thread would make them fully shareable but be inefficient; alternatively, encoder commands can be buffered, which might enable some multithreaded use-cases even if `Finish()` still must happen on the thread that created the encoder.) | ||
|
||
## Thread Safety {#ThreadSafety} | ||
|
||
The `webgpu.h` API is thread-safe (when multithreading is supported). That is, its functions are reentrant and may be called during the execution of other functions, with the following exceptions: | ||
|
||
- Encoder objects (@ref WGPUCommandEncoder, @ref WGPUComputePassEncoder, @ref WGPURenderPassEncoder, and @ref WGPURenderBundleEncoder) are not thread-safe. Note these objects appear only in their own methods. | ||
- Additionally, in Wasm, note these objects may be thread-locked as discussed above. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. can probably drop the "note" |
||
- API calls may not be made during @ref WGPUCallbackMode_AllowSpontaneous callbacks. See @ref CallbackReentrancy. | ||
|
||
The following _are_ thread safe: | ||
|
||
- @ref wgpuInstanceWaitAny() and @ref wgpuInstanceProcessEvents | ||
- @ref wgpuDeviceDestroy() | ||
- @ref wgpuBufferDestroy(), @ref wgpuTextureDestroy(), and @ref wgpuQuerySetDestroy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
- \subpage SentinelValues | ||
- \subpage Strings | ||
- \subpage StructChaining | ||
- \subpage Multithreading |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. Could this be a @ref also?