Skip to content

Commit f203624

Browse files
authored
Edit README for clarity (#27)
Recent changes (e.g., #17) introduced new concepts necessary for the specification. This change migrates this new text to the WASI proposal template style by: - linking each section in the document index - using paragraph style more extensively - clarifying and compacting several sentences
1 parent e2feef4 commit f203624

1 file changed

Lines changed: 30 additions & 68 deletions

File tree

README.md

Lines changed: 30 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ _TODO before entering Phase 2._
2424
- [Use case: support various languages](#use-case-support-various-languages)
2525
- [Use case: support thread-local storage](#use-case-support-thread-local-storage)
2626
- [Detailed design discussion](#detailed-design-discussion)
27+
- [Design choice: thread IDs](#design-choice-thread-ids)
28+
- [Design choice: termination](#design-choice-termination)
2729
- [Design choice: pthreads](#design-choice-pthreads)
2830
- [Design choice: instance-per-thread](#design-choice-instance-per-thread)
2931
- [Considered alternatives](#considered-alternatives)
@@ -90,9 +92,9 @@ The API consists of a single function. In pseudo-code:
9092
status wasi_thread_spawn(thread_start_arg* start_arg);
9193
```
9294
93-
where the `status` is a unique non-negative integer thread ID of the new
94-
thread or negative number representing an error in case the thread failed to
95-
start.
95+
where the `status` is a unique non-negative integer thread ID (TID) of the new
96+
thread (see [Design choice: thread IDs](#design-choice-thread-ids)) or a
97+
negative number representing an error if the host failed to spawn the thread.
9698
The host implementing `wasi_thread_spawn` will call a predetermined function
9799
export (`wasi_thread_start`) in a new WebAssembly instance. Any necessary
98100
locking/signaling/thread-local storage will be implemented using existing
@@ -127,13 +129,10 @@ TLS bookkeeping (this is not much different than how C starts threads natively).
127129
128130
### Detailed design discussion
129131
130-
Threads are tricky to implement. This proposal relies on a specific WebAssembly
131-
convention in order to work correctly.
132-
133-
When instantiating a module which is expected to run with wasi-threads,
134-
the WASI host must:
135-
136-
1. create and provide shared memories to satisfy the module's imports.
132+
Threads are tricky to implement. This proposal relies on a specific convention
133+
in order to work correctly. When instantiating a module which is expected to run
134+
with `wasi-threads`, the WASI host must first allocate shared memories to
135+
satisfy the module's imports.
137136
138137
Upon a call to `wasi_thread_spawn`, the WASI host must:
139138
@@ -152,69 +151,32 @@ Upon a call to `wasi_thread_spawn`, the WASI host must:
152151
A WASI host that implements the above should be able to spawn threads for a
153152
variety of languages.
154153
155-
### TID (thread ID)
156-
157-
TID is a 32-bit integer to identify threads created with `wasi_thread_spawn`.
158-
159-
* TIDs are managed and provided by host.
160-
161-
* TID 0 is reserved. `wasi_thread_spawn` should not return this value.
162-
163-
* It's widely assumed in musl/wasi-libc.
164-
165-
* The upper-most 3-bits of TID are always 0.
166-
`wasi_thread_spawn` should not return values with these bits set.
167-
168-
* The most significant bit is the sign bit. As `wasi_thread_spawn` uses
169-
signed integer and uses negative values to indicate errors, a TID needs
170-
to be positive.
171-
172-
* The second bit need to be 0 in order to be compatible with the TID-based
173-
locking implementation in musl/wasi-libc.
174-
175-
* The third bit need to be 0 in order to make an extra room for other
176-
reserved values in wasi-libc.
177-
For example, it can be used to indicate the main thread, which doesn't
178-
have a TID in the current version of this proposal.
179-
180-
### Process
181-
182-
* A process is a group of threads.
183-
184-
* The main thread starts with a process which only contains
185-
the main thread.
186-
187-
* Threads created by a thread in a process using `wasi_thread_spawn`
188-
are added to the process.
189-
190-
* When a thread is terminated, it's removed from the process.
191-
192-
### Voluntary thread termination
193-
194-
A thread can terminate itself voluntarily by returning from
195-
`wasi_thread_start`.
196-
197-
### Changes to WASI `proc_exit`
198-
199-
With this proposal, the `proc_exit` function takes extra responsibility
200-
to terminate all threads in the process, not only the calling one.
201-
202-
Any of the threads in the process can call `proc_exit`.
154+
#### Design choice: thread IDs
203155
204-
### Traps
156+
When `wasi_thread_spawn` successfully spawns a thread, it returns a thread ID
157+
(TID) — 32-bit integer with several restrictions. TIDs are managed and
158+
provided by the WASI host. To avoid leaking information, the host may choose to
159+
return arbitrary TIDs (as opposed to leaking OS TIDs).
205160
206-
When a trap occurs in any thread, the entire process is terminated.
161+
Valid TIDs fall in the range $[1, 2^{29})$. Some considerations apply:
162+
- `0` is reserved for compatibility reasons with existing libraries (e.g.,
163+
wasi-libc) and must not be returned by `wasi_thread_spawn`
164+
- the uppermost three bits of a valid TID must always be `0`. The most
165+
significant bit is the sign bit and recall that `wasi_thread_spawn` uses
166+
negative values to indicate errors. The remaining bits are reserved for
167+
compatibility with existing language implementations.
207168
208-
### Process exit status
169+
#### Design choice: termination
209170
210-
If one or more threads call WASI `proc_exit` or raise a trap,
211-
one of them is chosen by the runtime to represent the exit status
212-
of the process.
213-
It's non deterministic which one is chosen.
171+
A `wasi-threads` module initially executes a single thread — the main
172+
thread. As `wasi_thread_spawn` is called, more threads begin to execute. Threads
173+
terminate in the following ways:
214174
215-
If all the threads in the process have been terminated without calling
216-
`proc_exit` or raising a trap, it's treated as if the last thread called
217-
`proc_exit` with exit code 0.
175+
- __upon return__ from `wasi_thread_start`, and other threads continue to
176+
execute
177+
- __upon a trap__ in any thread; all threads are immediately terminated
178+
- __upon a `proc_exit` call__ in any thread; all threads are immediately
179+
terminated.
218180
219181
#### Design choice: pthreads
220182

0 commit comments

Comments
 (0)