[go: up one dir, main page]

saa 0.4.0

Low-level synchronization primitives that provide both synchronous and asynchronous interfaces
Documentation
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
//! Define base operations for synchronization primitives.

use std::pin::Pin;
use std::ptr::{addr_of, null, with_exposed_provenance};
#[cfg(not(feature = "loom"))]
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed, Release};
use std::thread;

#[cfg(feature = "loom")]
use loom::sync::atomic::AtomicUsize;

use crate::opcode::Opcode;
use crate::wait_queue::{PinnedWaitQueue, WaitQueue};

/// Define base operations for synchronization primitives.
pub(crate) trait SyncPrimitive: Sized {
    /// Returns a reference to the state.
    fn state(&self) -> &AtomicUsize;

    /// Returns the maximum number of shared owners.
    fn max_shared_owners() -> usize;

    /// Converts a reference to `Self` into a memory address.
    fn self_addr(&self) -> usize {
        let self_ptr: *const Self = addr_of!(*self);
        self_ptr.expose_provenance()
    }

    /// Tries to push a wait queue entry into the wait queue.
    #[must_use]
    fn try_push_wait_queue_entry(&self, entry: Pin<&WaitQueue>, state: usize) -> bool {
        let entry_addr = WaitQueue::ref_to_ptr(&entry).expose_provenance();
        debug_assert_eq!(entry_addr & (!WaitQueue::ADDR_MASK), 0);

        entry.update_next_entry_ptr(WaitQueue::addr_to_ptr(state & WaitQueue::ADDR_MASK));
        let next_state = (state & (!WaitQueue::ADDR_MASK)) | entry_addr;
        self.state()
            .compare_exchange(state, next_state, AcqRel, Acquire)
            .is_ok()
    }

    /// Waits for the desired resources asynchronously.
    async fn wait_resources_async(&self, state: usize, mode: Opcode) -> bool {
        debug_assert!(state & WaitQueue::ADDR_MASK != 0 || state & WaitQueue::DATA_MASK != 0);

        if cfg!(miri) {
            return false;
        }

        let async_wait = WaitQueue::new(
            mode,
            Some((self.self_addr(), Self::cleanup_wait_queue_entry)),
        );
        let pinned_async_wait = PinnedWaitQueue(Pin::new(&async_wait));
        debug_assert_eq!(
            addr_of!(async_wait),
            WaitQueue::ref_to_ptr(&pinned_async_wait.0)
        );

        if !self.try_push_wait_queue_entry(pinned_async_wait.0, state) {
            pinned_async_wait.0.set_result(false);
            pinned_async_wait.0.result_acknowledged();
            return false;
        }
        pinned_async_wait.await
    }

    /// Waits for the desired resources synchronously.
    #[must_use]
    fn wait_resources_sync(&self, state: usize, mode: Opcode) -> bool {
        debug_assert!(state & WaitQueue::ADDR_MASK != 0 || state & WaitQueue::DATA_MASK != 0);

        if cfg!(miri) {
            return false;
        }

        let sync_wait = WaitQueue::new(mode, None);
        let pinned_sync_wait = Pin::new(&sync_wait);
        debug_assert_eq!(
            addr_of!(sync_wait),
            WaitQueue::ref_to_ptr(&pinned_sync_wait)
        );

        if self.try_push_wait_queue_entry(pinned_sync_wait, state) {
            pinned_sync_wait.poll_result_sync()
        } else {
            false
        }
    }

    /// Releases resources represented by the supplied operation mode.
    ///
    /// Returns `false` if the resource cannot be released.
    fn release_loop(&self, mut state: usize, mode: Opcode) -> bool {
        while mode.can_release(state) {
            if state & WaitQueue::ADDR_MASK == 0
                || state & WaitQueue::LOCKED_FLAG == WaitQueue::LOCKED_FLAG
            {
                // Release the resource in-place.
                match self.state().compare_exchange(
                    state,
                    state - mode.release_count(),
                    Release,
                    Relaxed,
                ) {
                    Ok(_) => return true,
                    Err(new_state) => state = new_state,
                }
            } else {
                // The wait queue is not empty and not being processed.
                match self.try_start_process_wait_queue(state, mode) {
                    Ok(new_state) => {
                        self.process_wait_queue(new_state);
                        return true;
                    }
                    Err(new_state) => state = new_state,
                }
            }
        }
        false
    }

    /// Tries to start processing the wait queue by releasing a resource of the specified type and
    /// locking the wait queue.
    ///
    /// Returns the updated state if successful, or returns the latest state as an error if another
    /// thread was processing the wait queue or the resource could not be released.
    fn try_start_process_wait_queue(&self, mut state: usize, mode: Opcode) -> Result<usize, usize> {
        if state & WaitQueue::ADDR_MASK == 0
            || state & WaitQueue::LOCKED_FLAG == WaitQueue::LOCKED_FLAG
            || !mode.can_release(state)
        {
            return Err(state);
        }

        let mut next_state = (state | WaitQueue::LOCKED_FLAG) - mode.release_count();
        while let Err(new_state) = self
            .state()
            .compare_exchange(state, next_state, AcqRel, Relaxed)
        {
            if new_state & WaitQueue::ADDR_MASK == 0
                || new_state & WaitQueue::LOCKED_FLAG == WaitQueue::LOCKED_FLAG
                || !mode.can_release(state)
            {
                return Err(new_state);
            }
            state = new_state;
            next_state = (state | WaitQueue::LOCKED_FLAG) - mode.release_count();
        }
        Ok(next_state)
    }

    /// Processes the wait queue.
    fn process_wait_queue(&self, mut state: usize) {
        let mut head_entry_ptr: *const WaitQueue = null();
        let mut unlocked = false;
        while !unlocked {
            debug_assert_eq!(state & WaitQueue::LOCKED_FLAG, WaitQueue::LOCKED_FLAG);

            let tail_entry_ptr = WaitQueue::addr_to_ptr(state & WaitQueue::ADDR_MASK);
            if head_entry_ptr.is_null() {
                WaitQueue::iter_forward(tail_entry_ptr, |entry, next_entry| {
                    if next_entry.is_none() {
                        head_entry_ptr = WaitQueue::ref_to_ptr(entry);
                    }
                    false
                });
            } else {
                WaitQueue::install_backward_link(tail_entry_ptr);
            }

            let data = state & WaitQueue::DATA_MASK;
            let mut transferred = 0;
            let mut resolved_entry_ptr: *const WaitQueue = null();
            let mut reset_failed = false;

            WaitQueue::iter_backward(head_entry_ptr, |entry, prev_entry| {
                let desired = entry.opcode().release_count();
                if data + transferred == 0
                    || data + transferred + desired <= Self::max_shared_owners()
                {
                    // The entry can inherit ownerhip.
                    if prev_entry.is_none() {
                        // This is the tail of the wait queue: try to reset.
                        debug_assert_eq!(tail_entry_ptr, addr_of!(*entry));
                        if self
                            .state()
                            .compare_exchange(state, data + transferred + desired, AcqRel, Acquire)
                            .is_err()
                        {
                            // This entry will be processed on a retry.
                            entry.update_next_entry_ptr(null());
                            head_entry_ptr = WaitQueue::ref_to_ptr(entry);
                            reset_failed = true;
                            return true;
                        }

                        // The wait queue was reset.
                        unlocked = true;
                    }

                    resolved_entry_ptr = WaitQueue::ref_to_ptr(entry);
                    transferred += desired;
                    unlocked
                } else {
                    // Unlink those that have succeeded in acquiring shared ownership.
                    entry.update_next_entry_ptr(null());
                    head_entry_ptr = WaitQueue::ref_to_ptr(entry);
                    true
                }
            });
            debug_assert_eq!(resolved_entry_ptr.is_null(), transferred == 0);

            if reset_failed {
                debug_assert!(!unlocked);
                state = self.state().fetch_add(transferred, Release) + transferred;
            } else if !unlocked {
                if self
                    .state()
                    .fetch_update(AcqRel, Acquire, |new_state| {
                        let new_data = new_state & WaitQueue::DATA_MASK;
                        debug_assert!(new_data + transferred <= WaitQueue::DATA_MASK);

                        if new_state == state || new_data == data {
                            let next_state =
                                (new_state & WaitQueue::ADDR_MASK) | (new_data + transferred);
                            Some(next_state)
                        } else {
                            None
                        }
                    })
                    .is_err()
                {
                    state = self.state().fetch_add(transferred, Release) + transferred;
                } else {
                    unlocked = true;
                }
            }

            WaitQueue::iter_forward(resolved_entry_ptr, |entry, _next_entry| {
                entry.set_result(true);
                // The lock can be released here.
                false
            });
        }
    }

    /// Removes a wait queue entry from the wait queue.
    fn remove_wait_queue_entry(
        &self,
        mut state: usize,
        entry_addr_to_remove: usize,
    ) -> (usize, bool) {
        let target_ptr = WaitQueue::addr_to_ptr(entry_addr_to_remove);
        let mut result = Ok((state, false));

        loop {
            debug_assert_eq!(state & WaitQueue::LOCKED_FLAG, WaitQueue::LOCKED_FLAG);
            debug_assert_ne!(state & WaitQueue::ADDR_MASK, 0);

            let tail_entry_ptr = WaitQueue::addr_to_ptr(state & WaitQueue::ADDR_MASK);
            WaitQueue::iter_forward(tail_entry_ptr, |entry, next_entry| {
                if WaitQueue::ref_to_ptr(entry) == target_ptr {
                    if let Some(next_entry) = next_entry {
                        next_entry.update_prev_entry_ptr(entry.prev_entry_ptr());
                    }
                    if let Some(prev_entry) = unsafe { entry.prev_entry_ptr().as_ref() } {
                        prev_entry.update_next_entry_ptr(entry.next_entry_ptr());
                        result = Ok((state, true));
                    } else {
                        let next_entry_ptr = next_entry.map_or(null(), WaitQueue::ref_to_ptr);
                        debug_assert_eq!(next_entry_ptr.addr() & (!WaitQueue::ADDR_MASK), 0);

                        let next_state =
                            (state & (!WaitQueue::ADDR_MASK)) | next_entry_ptr.expose_provenance();
                        debug_assert_eq!(
                            next_state & WaitQueue::LOCKED_FLAG,
                            WaitQueue::LOCKED_FLAG
                        );

                        match self
                            .state()
                            .compare_exchange(state, next_state, AcqRel, Acquire)
                        {
                            Ok(_) => result = Ok((next_state, true)),
                            Err(new_state) => result = Err(new_state),
                        }
                    }
                    true
                } else {
                    false
                }
            });

            match result {
                Ok((state, removed)) => return (state, removed),
                Err(new_state) => state = new_state,
            }
        }
    }

    /// Cleans up a [`WaitQueue`] entry that was pushed into the wait queue, but has not been
    /// processed.
    fn cleanup_wait_queue_entry(entry: &WaitQueue, self_addr: usize) {
        let this: &Self = unsafe { &*with_exposed_provenance(self_addr) };
        let wait_queue_ptr: *const WaitQueue = addr_of!(*entry);
        let wait_queue_addr = wait_queue_ptr.expose_provenance();

        // Remove the wait queue entry from the wait queue list.
        let mut state = this.state().load(Acquire);
        let mut need_completion = false;
        loop {
            match this.try_start_process_wait_queue(state, Opcode::Cleanup) {
                Ok(new_state) => state = new_state,
                Err(new_state) => {
                    if new_state & WaitQueue::LOCKED_FLAG == WaitQueue::LOCKED_FLAG {
                        // Another thread is processing the wait queue.
                        thread::yield_now();
                        state = this.state().load(Acquire);
                        continue;
                    }

                    // The wait queue is empty.
                    debug_assert_eq!(new_state & WaitQueue::LOCKED_FLAG, 0);
                    debug_assert_eq!(new_state & WaitQueue::ADDR_MASK, 0);
                    state = new_state;
                    need_completion = true;
                }
            }
            break;
        }

        if state & WaitQueue::LOCKED_FLAG == WaitQueue::LOCKED_FLAG {
            let (new_state, removed) = this.remove_wait_queue_entry(state, wait_queue_addr);
            this.process_wait_queue(new_state);

            if !removed {
                need_completion = true;
            }
        }

        if need_completion {
            // The entry was removed from another thread, so will be completed.
            while !entry.result_finalized() {
                thread::yield_now();
            }
            this.release_loop(state, entry.opcode());
        }
    }

    /// Tests whether dropping a wait queue entry without waiting for its completion is safe.
    #[cfg(test)]
    fn test_drop_wait_queue_entry(&self, mode: Opcode) {
        let state = self.state().load(Acquire);
        let async_wait = WaitQueue::new(
            mode,
            Some((self.self_addr(), Self::cleanup_wait_queue_entry)),
        );
        let pinned_async_wait = PinnedWaitQueue(Pin::new(&async_wait));
        debug_assert_eq!(
            addr_of!(async_wait),
            WaitQueue::ref_to_ptr(&pinned_async_wait.0)
        );

        if !self.try_push_wait_queue_entry(pinned_async_wait.0, state) {
            pinned_async_wait.0.set_result(false);
            pinned_async_wait.0.result_acknowledged();
        }
    }
}

impl Opcode {
    /// Checks if the resourced expressed in `self` can be released from `state`.
    pub(crate) const fn can_release(self, state: usize) -> bool {
        match self {
            Opcode::Exclusive => {
                let data = state & WaitQueue::DATA_MASK;
                data == WaitQueue::DATA_MASK
            }
            Opcode::Shared => {
                let data = state & WaitQueue::DATA_MASK;
                data >= 1 && data != WaitQueue::DATA_MASK
            }
            Opcode::Semaphore(count) => {
                let data = state & WaitQueue::DATA_MASK;
                let count = count as usize;
                data >= count
            }
            Opcode::Cleanup => true,
        }
    }

    /// Converts the operation mode into a `usize` value representing resources held by the
    /// corresponding synchronization primitive.
    pub(crate) const fn release_count(self) -> usize {
        match self {
            Opcode::Exclusive => WaitQueue::DATA_MASK,
            Opcode::Shared => 1,
            Opcode::Semaphore(count) => {
                let count = count as usize;
                debug_assert!(count <= WaitQueue::LOCKED_FLAG);
                count
            }
            Opcode::Cleanup => 0,
        }
    }
}