|
| 1 | +//! A priority inheriting mutex for Fuchsia. |
| 2 | +//! |
| 3 | +//! This is a port of the [mutex in Fuchsia's libsync]. Contrary to the original, |
| 4 | +//! it does not abort the process when reentrant locking is detected, but deadlocks. |
| 5 | +//! |
| 6 | +//! Priority inheritance is achieved by storing the owning thread's handle in an |
| 7 | +//! atomic variable. Fuchsia's futex operations support setting an owner thread |
| 8 | +//! for a futex, which can boost that thread's priority while the futex is waited |
| 9 | +//! upon. |
| 10 | +//! |
| 11 | +//! libsync is licenced under the following BSD-style licence: |
| 12 | +//! |
| 13 | +//! Copyright 2016 The Fuchsia Authors. |
| 14 | +//! |
| 15 | +//! Redistribution and use in source and binary forms, with or without |
| 16 | +//! modification, are permitted provided that the following conditions are |
| 17 | +//! met: |
| 18 | +//! |
| 19 | +//! * Redistributions of source code must retain the above copyright |
| 20 | +//! notice, this list of conditions and the following disclaimer. |
| 21 | +//! * Redistributions in binary form must reproduce the above |
| 22 | +//! copyright notice, this list of conditions and the following |
| 23 | +//! disclaimer in the documentation and/or other materials provided |
| 24 | +//! with the distribution. |
| 25 | +//! |
| 26 | +//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 27 | +//! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 28 | +//! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 29 | +//! A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 30 | +//! OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 31 | +//! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 32 | +//! LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 33 | +//! DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 34 | +//! THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | +//! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 36 | +//! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | +//! |
| 38 | +//! [mutex in Fuchsia's libsync]: https://cs.opensource.google/fuchsia/fuchsia/+/main:zircon/system/ulib/sync/mutex.c |
| 39 | +
|
| 40 | +use crate::sync::atomic::{ |
| 41 | + AtomicU32, |
| 42 | + Ordering::{Acquire, Relaxed, Release}, |
| 43 | +}; |
| 44 | +use crate::sys::futex::zircon::{ |
| 45 | + zx_futex_wait, zx_futex_wake_single_owner, zx_handle_t, zx_thread_self, ZX_ERR_BAD_STATE, |
| 46 | + ZX_OK, ZX_TIME_INFINITE, |
| 47 | +}; |
| 48 | + |
| 49 | +// The lowest two bits of a `zx_handle_t` are always set, so the lowest bit is used to mark the |
| 50 | +// mutex as contested by clearing it. |
| 51 | +const CONTESTED_BIT: u32 = 1; |
| 52 | +// This can never be a valid `zx_handle_t`. |
| 53 | +const UNLOCKED: u32 = 0; |
| 54 | + |
| 55 | +pub type MovableMutex = Mutex; |
| 56 | + |
| 57 | +pub struct Mutex { |
| 58 | + futex: AtomicU32, |
| 59 | +} |
| 60 | + |
| 61 | +#[inline] |
| 62 | +fn to_state(owner: zx_handle_t) -> u32 { |
| 63 | + owner |
| 64 | +} |
| 65 | + |
| 66 | +#[inline] |
| 67 | +fn to_owner(state: u32) -> zx_handle_t { |
| 68 | + state | CONTESTED_BIT |
| 69 | +} |
| 70 | + |
| 71 | +#[inline] |
| 72 | +fn is_contested(state: u32) -> bool { |
| 73 | + state & CONTESTED_BIT == 0 |
| 74 | +} |
| 75 | + |
| 76 | +#[inline] |
| 77 | +fn mark_contested(state: u32) -> u32 { |
| 78 | + state & !CONTESTED_BIT |
| 79 | +} |
| 80 | + |
| 81 | +impl Mutex { |
| 82 | + #[inline] |
| 83 | + pub const fn new() -> Mutex { |
| 84 | + Mutex { futex: AtomicU32::new(UNLOCKED) } |
| 85 | + } |
| 86 | + |
| 87 | + #[inline] |
| 88 | + pub unsafe fn init(&mut self) {} |
| 89 | + |
| 90 | + #[inline] |
| 91 | + pub unsafe fn try_lock(&self) -> bool { |
| 92 | + let thread_self = zx_thread_self(); |
| 93 | + self.futex.compare_exchange(UNLOCKED, to_state(thread_self), Acquire, Relaxed).is_ok() |
| 94 | + } |
| 95 | + |
| 96 | + #[inline] |
| 97 | + pub unsafe fn lock(&self) { |
| 98 | + let thread_self = zx_thread_self(); |
| 99 | + if let Err(state) = |
| 100 | + self.futex.compare_exchange(UNLOCKED, to_state(thread_self), Acquire, Relaxed) |
| 101 | + { |
| 102 | + self.lock_contested(state, thread_self); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + #[cold] |
| 107 | + fn lock_contested(&self, mut state: u32, thread_self: zx_handle_t) { |
| 108 | + let owned_state = mark_contested(to_state(thread_self)); |
| 109 | + loop { |
| 110 | + // Mark the mutex as contested if it is not already. |
| 111 | + let contested = mark_contested(state); |
| 112 | + if is_contested(state) |
| 113 | + || self.futex.compare_exchange(state, contested, Relaxed, Relaxed).is_ok() |
| 114 | + { |
| 115 | + // The mutex has been marked as contested, wait for the state to change. |
| 116 | + unsafe { |
| 117 | + match zx_futex_wait( |
| 118 | + &self.futex, |
| 119 | + AtomicU32::new(contested), |
| 120 | + to_owner(state), |
| 121 | + ZX_TIME_INFINITE, |
| 122 | + ) { |
| 123 | + ZX_OK | ZX_ERR_BAD_STATE => (), |
| 124 | + // Deadlock even in the case of reentrant locking, as leaking a guard |
| 125 | + // could lead to the same condition if the thread id is reused, but |
| 126 | + // panicking is not expected in that situation. This makes things |
| 127 | + // quite a bit harder to debug, but encourages portable programming. |
| 128 | + _ if to_owner(state) == thread_self => loop {}, |
| 129 | + error => panic!("futex operation failed with error code {error}"), |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // The state has changed or a wakeup occured, try to lock the mutex. |
| 135 | + match self.futex.compare_exchange(UNLOCKED, owned_state, Acquire, Relaxed) { |
| 136 | + Ok(_) => return, |
| 137 | + Err(updated) => state = updated, |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + #[inline] |
| 143 | + pub unsafe fn unlock(&self) { |
| 144 | + if is_contested(self.futex.swap(UNLOCKED, Release)) { |
| 145 | + // The woken thread will mark the mutex as contested again, |
| 146 | + // and return here, waking until there are no waiters left, |
| 147 | + // in which case this is a noop. |
| 148 | + self.wake(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + #[cold] |
| 153 | + fn wake(&self) { |
| 154 | + unsafe { |
| 155 | + zx_futex_wake_single_owner(&self.futex); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments