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
//! Per-thread storage for the shell state C keeps per THREAD OF EXECUTION.
//!
//! **zshrs-original infrastructure — no `Src/*.c` counterpart.** It is the
//! generalisation of [`crate::errflag_cell`], and it exists for the same
//! reason: zshrs replaced a `fork(2)` with a thread.
//!
//! C zsh runs on one thread. `scriptname` (`Src/init.c`, the diagnostic
//! prefix `zwarning` reads at `Src/utils.c:147`), `funcstack`
//! (`Src/exec.c:340`, behind `$funcstack` / `$functrace` /
//! `$funcfiletrace` / `$funcsourcetrace`), `zsh_eval_context`
//! (`zsh.export:355`, behind `$ZSH_EVAL_CONTEXT`) and `locallevel`
//! (`Src/params.c:54`) are all plain globals that describe WHERE THE ONE
//! THREAD CURRENTLY IS. Background work in C is a forked child, so the
//! child's pushes onto those globals are invisible to the parent.
//!
//! zshrs runs some of that work on the worker pool ([`crate::worker`])
//! instead — `async_precmd` (`crate::async_precmd`) hands hook FUNCTIONS to
//! a worker, and `compsys::in_editor` drives the completer tree on a
//! dedicated thread. A hook function moves every one of those globals while
//! it runs, and the shell thread reads the moved value:
//!
//! ```text
//! async_precmd_functions=(spin_hook) # spin_hook is running on a worker
//! % done # typed at the prompt
//! spin_hook:4: parse error near `done' <- zshrs, before this module
//! zsh: parse error near `done' <- zsh
//! ```
//!
//! The user is pointed at a function that has nothing to do with what they
//! typed, which is worse than no information at all. `quiesce`
//! (`crate::async_precmd::quiesce`) closes the window where the shell
//! thread EXECUTES shell code during a batch, but the line above is
//! diagnosed by the LEXER, which runs before the `quiesce()` in
//! `init::loop_` — and `quiesce` is a serialisation, not a fix for the
//! sharing.
//!
//! Restoring the C model is the fix: the shell thread keeps the
//! process-wide value, every other thread gets its own. A thread that runs
//! shell code off the shell thread IS a forked child in the C design, so
//! where it is stays with it.
//!
//! Two shapes live here:
//!
//! * [`ThreadMutex`] for the stack-shaped state (`scriptname`,
//! `scriptfilename`, `funcstack`, `zsh_eval_context`). It mirrors
//! `Mutex`'s `lock()` / `clear_poison()` so the ~50 existing
//! `FUNCSTACK.lock()` call sites read exactly as they did before.
//! * [`LocalLevelCell`] for `locallevel`, which cannot simply become
//! per-thread — see its own docs.
use Cell;
use HashMap;
use ;
use ;
use ThreadId;
thread_local!
/// True on the shell thread, false on every worker.
///
/// !!! WARNING: RUST-ONLY !!! C has one thread and needs no such test.
///
/// Sharp edge, same as `errflag_cell`'s: a thread deliberately named
/// `"main"` by `thread::Builder` would be misclassified as the shell.
/// Nothing in this tree does that.
/// A `Mutex` whose contents are the shell thread's on the shell thread and
/// this thread's own everywhere else.
///
/// !!! WARNING: RUST-ONLY !!! No counterpart in `Src/*.c` — C gets this
/// split from `fork(2)`.
///
/// The API is deliberately `Mutex`'s, down to the `LockResult`, so a
/// global can be re-typed from `Mutex<T>` to `ThreadMutex<T>` without
/// touching its readers. The per-thread halves are leaked `Mutex<T>`s so
/// both branches can hand back the same `MutexGuard<'static, T>` type; the
/// leak is bounded by (threads that ever ran shell code) × (cells), which
/// is the small fixed worker pool.
///
/// A worker's half starts EMPTY rather than as a copy of the shell
/// thread's. That is the `fork()` a C zsh would do from `preprompt()`,
/// where the shell is at top level with nothing on any of these stacks;
/// it is not a copy of an arbitrary mid-function state, because no site
/// that runs shell code off the shell thread is reached from inside one.
/// `locallevel` storage: shared for parameter scoping, per-thread for
/// diagnostics.
///
/// !!! WARNING: RUST-ONLY !!! C has one `int locallevel`
/// (`Src/params.c:54`) serving both jobs, because with one thread they are
/// the same number.
///
/// zshrs cannot simply make it per-thread. `locallevel` is the level a
/// parameter is STAMPED with (`Src/params.c:5837` `locallevel++` in
/// `startparamscope`) and the level `scanendscope` compares every entry
/// against on scope exit (`Src/params.c:5904-5907`, `if (pm->level >
/// locallevel)`) — and the param table those stamps live in is one shared
/// table that `async_precmd` hooks deliberately write into. Give each
/// thread its own counter and two threads both stamp level 1 in the same
/// table, so one thread's `endparamscope` deletes the other's locals: a
/// strictly worse version of the `$BUFFER` loss `quiesce` was added for.
/// So [`load`](Self::load) — every parameter-scoping reader — keeps
/// returning the shared value, unchanged.
///
/// What a worker must NOT contaminate is the other job: the
/// "am I inside a function" test the error formatters make
/// (`Src/utils.c:150` `if (unset(SHINSTDIN) || locallevel)`, `:301` in
/// `zerrmsg`). A hook running on a worker raised the shared counter, and
/// an error the user caused at top level came out formatted as if it had
/// happened inside a function — with a line number and a function-name
/// prefix zsh does not print. [`here`](Self::here) answers that question
/// for the CALLING thread, and the formatters read it.
///
/// Every mutation goes through this type, so the per-thread depth cannot
/// drift away from the sites that move the shared counter: the four
/// methods below are the entire mutation surface of `params::locallevel`.