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
// Implementation mostly based from https://github.com/v8/v8/blob/main/src/execution/futex-emulation.cc
// TODO: track https://github.com/rust-lang/rfcs/pull/3467 to see if we can use `UnsafeAliased` instead
// of raw pointers.
// A bit of context about how exactly this thing works.
//
// `Atomics.wait/notify` is basically an emulation of the "futex" syscall, which internally uses
// a wait queue attached to a certain memory address, where processes and threads can manipulate
// it to synchronize between them.
// More information: https://en.wikipedia.org/wiki/Futex
//
// Our emulation of the API is composed by three components:
//
// - `FutexWaiters`, which is a map of addresses to the corresponding wait queue for that address.
// Internally uses intrusive linked lists to avoid allocating when adding a new waiter, which
// reduces the time spent by a thread in the critical section.
//
// - `FutexWaiter`, which contains all the data necessary to be able to wake a waiter from another
// thread. It also contains a `waiting` boolean, that is checked after waking up to see
// if the waiter was indeed woken up or if it just sporadically woke up (yes, this is a thing that
// can happen per the documentation of `CondVar`).
//
// - `CRITICAL_SECTION`, a global static that must be locked before registering or notifying any
// waiter. This guarantees that only one agent can write to the wait queues at any point in time.
//
// We can emulate a typical execution using the API for demonstration purposes.
// At the start of the program, we initially have an empty map of wait queues. We represent this
// graphically as:
//
// Address │
// │
// ────────────┼────────────────────────────────────────────────────────────────────
// │
// │
// <empty> │
// │
// │
//
// Each row here will represent an address and the corresponding wait queue for that address.
//
// Let's suppose that "Thread 2" wants to wait on the address 50. After locking the global mutex,
// it first creates a new instante of a `FutexWaiter` and passes a pointer to it to the
// `FutexWaiters::add_waiter`:
//
// Address │
// │
// ────────────┼──────────────────────────────────────────────────────────────────────
// │
// │ ┌───────────────┐
// │ ┌─►│ │
// │ │ │ Thread 2 │
// │ │ │ FutexWaiter │
// 50 ├────┘ │ │
// │ │ │
// │ │ cond_var │
// │ │ waiting: true │
// │ │ │
// │ └───────────────┘
// │
//
// Immediately after this, "Thread 2" calls `cond_var.wait`, unlocks the global mutex and sleeps
// until it is notified again (ignoring the spurious wakeups, those are handled in an infinite loop
// anyways).
//
// Now, let's suppose that `Thread 1` has now acquired the lock and now wants to also
// wait on the address `50`. Doing the same procedure as "Thread 2", our map now looks like:
//
// Address │
// │
// ────────────┼──────────────────────────────────────────────────────────────────────
// │
// │ ┌───────────────┐ ┌───────────────┐
// │ ┌─►│ ├───────►│ │
// │ │ │ Thread 2 │ │ Thread 1 │
// │ │ │ FutexWaiter │ │ FutexWaiter │
// 50 ├────┘ │ │ │ │
// │ │ │ │ │
// │ │ cond_var │ │ cond_var │
// │ │ waiting: true │◄───────┤ waiting: true │
// │ │ │ │ │
// │ └───────────────┘ └───────────────┘
// │
//
// Note how the head of our list contains the first waiter which was registered, and the
// tail of our list is our most recent waiter.
//
// After "Thread 1" sleeps, "Thread 3" has the opportunity to lock the global mutex.
// In this case, "Thread 3" will notify one waiter of the address 50 using the `cond_var` inside
// `FutexWaiter`, and will also remove it from the linked list. In this case
// the notified thread is "Thread 2":
//
// Address │
// │
// ────────────┼──────────────────────────────────────────────────────────────────────
// │
// │ ┌────────────────┐ ┌────────────────┐
// │ │ │ ┌──►│ │
// │ │ Thread 2 │ │ │ Thread 1 │
// │ │ FutexWaiter │ │ │ FutexWaiter │
// 50 ├───┐ │ │ │ │ │
// │ │ │ │ │ │ │
// │ │ │ cond_var │ │ │ cond_var │
// │ │ │ waiting: false │ │ │ waiting: true │
// │ │ │ │ │ │ │
// │ │ └────────────────┘ │ └────────────────┘
// │ │ │
// │ └────────────────────────┘
// │
//
// Then, when the lock is released and "Thread 2" has woken up, it tries to lock the global mutex
// again, checking if `waiting` is true to manually remove itself from the queue if that's the case.
// In this case, `waiting` is false, which doesn't require any other handling, so it just
// removes the `FutexWaiter` from its stack and returns `AtomicsWaitResult::Ok`.
//
// Address │
// │
// ────────────┼──────────────────────────────────────────────────────────────────────
// │
// │ ┌────────────────┐
// │ ┌──────────────────────────►│ │
// │ │ │ Thread 1 │
// │ │ │ FutexWaiter │
// 50 ├────┘ │ │
// │ │ │
// │ │ cond_var │
// │ │ waiting: true │
// │ │ │
// │ └────────────────┘
// │
// │
// │
//
// In a future point in time, "Thread 1" will be notified, which will proceed with the
// exact same steps as "Thread 2", emptying the wait queue and finishing the execution of our
// program.
use ;
use Strict;
use crate::;
pub
/// Adds this agent to the wait queue for the address pointed to by `buffer[offset..]`.
///
/// # Safety
///
/// - `addr` must be a multiple of `std::mem::size_of::<E>()`.
/// - `buffer` must contain at least `std::mem::size_of::<E>()` bytes to read starting from `usize`.
// our implementation guarantees that `SharedArrayBuffer` is always aligned to `u64` at minimum.
pub unsafe
/// Notifies at most `count` agents waiting on the memory address pointed to by `buffer[offset..]`.
pub