1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! The calling thread: its identity, its name, yielding the processor, and
//! how many processors there are to share.
//!
//! Everything portable here acts on the thread that calls it. What the
//! systems do differently is in the extension modules `linux`, `macos` and
//! `windows`, each present on its own system only: processor affinity
//! (Linux and Windows, in different shapes; macOS has none), how urgent a
//! thread is (a nice value, a quality of service class, a priority: three
//! scales with no meaning in common), where the stack of the thread is
//! (macOS and Windows; Linux keeps no such record), and creating threads.
//!
//! The processor time a thread has used is a clock:
//! [`Clock::ThreadCpu`](crate::time::Clock::ThreadCpu).
//!
//! # Creating threads
//!
//! There is no portable function for it, and the reason is Linux.
//!
//! A thread needs more than a stack: the *runtime* of the program keeps a
//! block of memory per thread (thread-local storage, and what it hangs
//! there: its allocator's caches, the identity of the thread, the state of
//! a panic), found through a register the thread is created with.
//!
//! - On **macOS** and **Windows** the runtime is the system's.
//! `pthread_create` and `CreateThread` make threads that are complete,
//! whoever calls them, and `std` adopts a thread it did not create the
//! first time one asks it something. `macos::spawn` and `windows::spawn`
//! are safe, and their threads may do anything.
//! - On **Linux** the kernel knows nothing of that block: `clone` makes a
//! thread with a stack and no more, and the block is made by whatever
//! runtime the program has: glibc, musl, or the `std` of fullrust, none
//! of which offers to make one for a thread it did not create (fullrust
//! exports nothing of the kind; a C library offers `pthread_create`
//! itself). oscall calls no library on Linux: that is what lets it work
//! under fullrust and in a program with no runtime at all, and a
//! function that imported `pthread_create` where there is a C library
//! would not exist where there is none, which is not a portable function
//! either. What oscall can make there is `linux::spawn_raw`, an
//! `unsafe` function whose thread may only run code that needs no
//! runtime: no `std`, no allocation, no thread-local variable, no panic.
//!
//! A portable `spawn` would have to hold to the contract of the weakest of
//! the three, and would then be `linux::spawn_raw` under another name:
//! safe code could not call it and its thread could not allocate. A
//! program that wants threads everywhere uses `std::thread`, which is the
//! runtime's own way in; the functions here are for programs that have no
//! `std`, or that want what `std` does not offer.
use cratesys;
use crate::;
pub
/// The identifier the system gives the calling thread.
///
/// It is unique among the threads that exist at the same time, in the whole
/// system on Linux, macOS and Windows; the system may give it to a new
/// thread once this one has exited. It is the number the system's own tools
/// show (`ps -L`, `/proc/<pid>/task/<tid>`, debuggers, Process Explorer).
///
/// Linux: `gettid`, which is the process id in the first thread of a
/// process. macOS: `pthread_threadid_np`. Windows: `GetCurrentThreadId`.
/// WebAssembly has one thread, and the identifier is always 1.
///
/// This is a system call on Linux: keep the result rather than asking in a
/// loop.
/// Offers the processor to another thread that is ready to run. Returns at
/// once if there is none.
///
/// This is a hint to the scheduler, and useful only for spinning a few
/// times before blocking: to wait for something, block on it
/// ([`sync::wait`](crate::sync::wait)).
///
/// Linux, macOS and WASI: `sched_yield`. Windows: `SwitchToThread`. Does
/// nothing on WebAssembly without WASI.
/// The number of threads that can run at the same time: the count of
/// logical processors available.
///
/// Linux: the processors the calling thread is allowed to run on
/// (`sched_getaffinity`), which accounts for `taskset` and for the CPU sets
/// of containers, but not for CPU-time quotas (cgroup `cpu.max`), which
/// `std::thread::available_parallelism` also reads. macOS: the processors
/// currently active (`sysctl hw.activecpu`). Windows: the active processors
/// of all processor groups (`GetActiveProcessorCount`), whatever the
/// affinity of the process. WebAssembly: 1.
///
/// The result is at least 1.
/// Names the calling thread, for debuggers, profilers and the system's
/// process listings.
///
/// The systems keep names of different lengths: 15 bytes on Linux, 63 on
/// macOS, and oscall passes at most 255 on Windows. A longer name is cut to
/// the limit, at a character boundary. A name with a NUL character in it
/// fails with [`InvalidInput`](ErrorKind::InvalidInput).
///
/// Linux: `prctl(PR_SET_NAME)`; in the first thread of a process this also
/// changes the name of the process that `ps` and `top` show (`comm`).
/// macOS: `pthread_setname_np`. Windows: `SetThreadDescription`, which
/// Windows 10 has since version 1607; before that the call fails with
/// [`Unsupported`](ErrorKind::Unsupported).
///
/// Fails with [`Unsupported`](ErrorKind::Unsupported) on WebAssembly.
/// Reads the name of the calling thread into `buf`, and returns it.
///
/// A thread that was never named has the name the system gave it: that of
/// the program on Linux, none (an empty string) on macOS and Windows.
///
/// If `buf` is too small for the name the call fails with
/// [`BufferTooSmall`](ErrorKind::BufferTooSmall); 64 bytes are enough on Linux
/// and macOS, and 256 for any name set through [`set_name`]. A name that
/// is not valid UTF-8, which only another library can have set, is returned
/// up to its first invalid byte.
///
/// Linux: `prctl(PR_GET_NAME)`. macOS: `pthread_getname_np`. Windows:
/// `GetThreadDescription`, with the same condition as [`set_name`].
///
/// Fails with [`Unsupported`](ErrorKind::Unsupported) on WebAssembly.
/// What Linux alone offers for threads.
/// What macOS alone offers for threads.
/// What Windows alone offers for threads.