1 //! A priority inheriting mutex for Fuchsia.
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.
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
11 //! libsync is licenced under the following BSD-style licence:
13 //! Copyright 2016 The Fuchsia Authors.
15 //! Redistribution and use in source and binary forms, with or without
16 //! modification, are permitted provided that the following conditions are
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.
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.
38 //! [mutex in Fuchsia's libsync]: https://cs.opensource.google/fuchsia/fuchsia/+/main:zircon/system/ulib/sync/mutex.c
40 use crate::sync::atomic::{
42 Ordering::{Acquire, Relaxed, Release},
44 use crate::sys::futex::zircon::{
45 zx_futex_wait, zx_futex_wake_single_owner, zx_handle_t, zx_thread_self, ZX_ERR_BAD_HANDLE,
46 ZX_ERR_BAD_STATE, ZX_ERR_INVALID_ARGS, ZX_ERR_TIMED_OUT, ZX_ERR_WRONG_TYPE, ZX_OK,
50 // The lowest two bits of a `zx_handle_t` are always set, so the lowest bit is used to mark the
51 // mutex as contested by clearing it.
52 const CONTESTED_BIT: u32 = 1;
53 // This can never be a valid `zx_handle_t`.
54 const UNLOCKED: u32 = 0;
61 fn to_state(owner: zx_handle_t) -> u32 {
66 fn to_owner(state: u32) -> zx_handle_t {
71 fn is_contested(state: u32) -> bool {
72 state & CONTESTED_BIT == 0
76 fn mark_contested(state: u32) -> u32 {
77 state & !CONTESTED_BIT
82 pub const fn new() -> Mutex {
83 Mutex { futex: AtomicU32::new(UNLOCKED) }
87 pub fn try_lock(&self) -> bool {
88 let thread_self = unsafe { zx_thread_self() };
89 self.futex.compare_exchange(UNLOCKED, to_state(thread_self), Acquire, Relaxed).is_ok()
94 let thread_self = unsafe { zx_thread_self() };
96 self.futex.compare_exchange(UNLOCKED, to_state(thread_self), Acquire, Relaxed)
99 self.lock_contested(state, thread_self);
105 /// `thread_self` must be the handle for the current thread.
107 unsafe fn lock_contested(&self, mut state: u32, thread_self: zx_handle_t) {
108 let owned_state = mark_contested(to_state(thread_self));
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()
115 // The mutex has been marked as contested, wait for the state to change.
119 AtomicU32::new(contested),
123 ZX_OK | ZX_ERR_BAD_STATE | ZX_ERR_TIMED_OUT => (),
124 // Note that if a thread handle is reused after its associated thread
125 // exits without unlocking the mutex, an arbitrary thread's priority
126 // could be boosted by the wait, but there is currently no way to
128 ZX_ERR_INVALID_ARGS | ZX_ERR_BAD_HANDLE | ZX_ERR_WRONG_TYPE => {
130 "either the current thread is trying to lock a mutex it has
131 already locked, or the previous owner did not unlock the mutex
135 error => panic!("unexpected error in zx_futex_wait: {error}"),
140 // The state has changed or a wakeup occurred, try to lock the mutex.
141 match self.futex.compare_exchange(UNLOCKED, owned_state, Acquire, Relaxed) {
143 Err(updated) => state = updated,
149 pub unsafe fn unlock(&self) {
150 if is_contested(self.futex.swap(UNLOCKED, Release)) {
151 // The woken thread will mark the mutex as contested again,
152 // and return here, waking until there are no waiters left,
153 // in which case this is a noop.
161 zx_futex_wake_single_owner(&self.futex);