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
//! The calling thread: its identity, its name, yielding the processor, and
//! how many processors there are to share.
//!
//! oscall does not create threads: a thread made behind the back of the
//! runtime has no thread-local storage that runtime knows of.
//! `std::thread` owns that, and everything here acts on the thread that
//! calls it.
//!
//! Processor affinity exists on Linux and Windows in different shapes, and
//! not on macOS: it lives in the extension modules `linux` and `windows` of
//! this module, each present on its own system only.
use cratesys;
use crate::;
/// 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 Windows alone offers for threads.