]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/locks/fuchsia_mutex.rs
Auto merge of #105018 - zertosh:path_buf_deref_mut, r=dtolnay
[rust.git] / library / std / src / sys / unix / locks / fuchsia_mutex.rs
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_HANDLE,
46     ZX_ERR_BAD_STATE, ZX_ERR_INVALID_ARGS, ZX_ERR_TIMED_OUT, ZX_ERR_WRONG_TYPE, ZX_OK,
47     ZX_TIME_INFINITE,
48 };
49
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;
55
56 pub struct Mutex {
57     futex: AtomicU32,
58 }
59
60 #[inline]
61 fn to_state(owner: zx_handle_t) -> u32 {
62     owner
63 }
64
65 #[inline]
66 fn to_owner(state: u32) -> zx_handle_t {
67     state | CONTESTED_BIT
68 }
69
70 #[inline]
71 fn is_contested(state: u32) -> bool {
72     state & CONTESTED_BIT == 0
73 }
74
75 #[inline]
76 fn mark_contested(state: u32) -> u32 {
77     state & !CONTESTED_BIT
78 }
79
80 impl Mutex {
81     #[inline]
82     pub const fn new() -> Mutex {
83         Mutex { futex: AtomicU32::new(UNLOCKED) }
84     }
85
86     #[inline]
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()
90     }
91
92     #[inline]
93     pub fn lock(&self) {
94         let thread_self = unsafe { zx_thread_self() };
95         if let Err(state) =
96             self.futex.compare_exchange(UNLOCKED, to_state(thread_self), Acquire, Relaxed)
97         {
98             unsafe {
99                 self.lock_contested(state, thread_self);
100             }
101         }
102     }
103
104     /// # Safety
105     /// `thread_self` must be the handle for the current thread.
106     #[cold]
107     unsafe 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 | 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
127                         // prevent that.
128                         ZX_ERR_INVALID_ARGS | ZX_ERR_BAD_HANDLE | ZX_ERR_WRONG_TYPE => {
129                             panic!(
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
132                                 before exiting"
133                             )
134                         }
135                         error => panic!("unexpected error in zx_futex_wait: {error}"),
136                     }
137                 }
138             }
139
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) {
142                 Ok(_) => return,
143                 Err(updated) => state = updated,
144             }
145         }
146     }
147
148     #[inline]
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.
154             self.wake();
155         }
156     }
157
158     #[cold]
159     fn wake(&self) {
160         unsafe {
161             zx_futex_wake_single_owner(&self.futex);
162         }
163     }
164 }