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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
//! Preemptive Mutex //! //! # Behaviour //! //! Lock that preempts if it cannot be obtained immediately. See the [sync] module. //! //! The mutex holds a queue of the waiters, and when unlocking it checks if there is contention, //! in which case it wakes up the head of the queue by popping it and adding it to the schedule queue. //! //! The lock performs additional checks around the owner of the lock, and panics if double-locking //! is detected. //! //! When there is contention and the thread is put to sleep, it is removed from the schedule queue, //! and an Arc to its [`ThreadStruct`] is put in the waiters queue. This means that the thread will //! stay alive at least until it is waked up. //! //! Most of this module is copy-pasted from std Mutexes, and try to preserve the same structure, //! while the documentation has been re-written. //! //! However we don't implement poisons, as kernel thread panicking while holding a Mutex //! should simply kernel panic, and abort. //! //! # Internal workings //! //! The secret about these mutex is that they're just fancy wrappers around a [`SpinLock`]. //! //! This `SpinLock` protects the queue. When checking for contention, we take the SpinLock, //! which arbitrates all concurrent operations for us, and then simply check if the queue of waiters //! is empty. //! //! If necessary we add ourselves to the queue, and then both unschedule ourselves and unlock it //! simultaneously. //! //! Unlocking performs pretty much the same operation. //! //! [sync]: crate::sync //! [`ThreadStruct`]: crate::process::ThreadStruct //! [`SpinLock`]: crate::sync::SpinLock use super::SpinLock; use crate::process::ThreadStruct; use crate::scheduler::{get_current_thread, add_to_schedule_queue, unschedule}; use alloc::sync::Arc; use alloc::vec::Vec; use core::cell::UnsafeCell; use core::fmt; use core::ops::{Deref, DerefMut}; use core::marker::PhantomData; /// A type alias for the result of a nonblocking locking method. pub type TryLockResult<Guard> = Result<Guard, ()>; /// A mutual exclusion primitive useful for protecting shared data /// /// This mutex will block kernel threads waiting for the lock to become available. The /// mutex can also be statically initialized or created via a [`new`] /// constructor. Each mutex has a type parameter which represents the data that /// it is protecting. The data can only be accessed through the RAII guards /// returned from [`lock`] and [`try_lock`], which guarantees that the data is only /// ever accessed when the mutex is locked. /// /// [`new`]: Mutex::new /// [`lock`]: Mutex::lock /// [`try_lock`]: Mutex::try_lock pub struct Mutex<T> { /// The data that we're protecting. /// /// Std Mutex boxes the data so it is Pin. We don't care for that in the kernel. /// However this adds a bound for T: Sized. data: UnsafeCell<T>, /// The struct responsible for arbitrating accesses to `.data`. inner: MutexInner, } unsafe impl<T: Send> Send for Mutex<T> { } unsafe impl<T: Send> Sync for Mutex<T> { } /// The type responsible of actually performing the locking of the mutex. /// /// Just a `SpinLock<`[`MutexInnerInner`]`>>`. /// /// This might seem a bit weird to have an intermediate a struct just for that, /// but it is to stay as close as possible to std's Mutex design, so we can copy-paste it with ease. struct MutexInner { /// A spin lock arbitrating accesses to the mutex's state. spin_lock: SpinLock<MutexInnerInner> } /// The bookkeeping of a Mutex. Knows the current owner, and holds the waiters queue. struct MutexInnerInner { /// The owner of this Mutex. None means free. /// /// We represent the owner as a pointer to its ThreadStruct. owner: Option<usize>, /// Queue of threads waiting on this mutex. waiters: Vec<Arc<ThreadStruct>> } /// An RAII implementation of a "scoped lock" of a mutex. When this structure is /// dropped (falls out of scope), the lock will be unlocked. /// /// The data protected by the mutex can be accessed through this guard via its /// [`Deref`] and [`DerefMut`] implementations. /// /// This structure is created by the [`lock`] and [`try_lock`] methods on /// [`Mutex`]. /// /// [`Deref`]: core::ops::Deref /// [`DerefMut`]: core::ops::DerefMut /// [`lock`]: Mutex::lock /// [`try_lock`]: Mutex::try_lock #[must_use = "if unused the Mutex will immediately unlock"] pub struct MutexGuard<'a, T: 'a> { /// Reference to the Mutex we'll unlock when dropped. __lock: &'a Mutex<T>, /// Raw pointer just to make MutexGuard !Send. __phantom: PhantomData<*mut ()> } unsafe impl<T: Sync> Sync for MutexGuard<'_, T> { } /* ****************************************** MUTEX ********************************************* */ // copied from std, removed poison and some std specific doc tests // impl<T> Mutex<T> { /// Creates a new mutex in an unlocked state ready for use. /// /// # Examples /// /// ``` /// use crate::sync::Mutex; /// /// let mutex = Mutex::new(0); /// ``` pub const fn new(t: T) -> Mutex<T> { Self { data: UnsafeCell::new(t), inner: MutexInner { spin_lock: SpinLock::new(MutexInnerInner { owner: None, waiters: Vec::new() }) } } } /// Consumes this mutex, returning the underlying data. /// /// # Examples /// /// ``` /// use std::sync::Mutex; /// /// let mutex = Mutex::new(0); /// assert_eq!(mutex.into_inner().unwrap(), 0); /// ``` pub fn into_inner(self) -> T { // We know statically that there are no outstanding references to // `self` so there's no need to lock the inner mutex. self.data.into_inner() } /// Acquires a mutex, blocking the current kernel thread until it is able to do so. /// /// This function will block the local kernel thread until it is available to acquire /// the mutex. Upon returning, the thread is the only thread with the lock /// held. An RAII guard is returned to allow scoped unlock of the lock. When /// the guard goes out of scope, the mutex will be unlocked. /// /// # Panics /// /// This function panics when called if the lock is already held by /// the current thread. pub fn lock(&self) -> MutexGuard<'_, T> { unsafe { self.inner.raw_lock(); MutexGuard::new(self) } } /// Attempts to acquire this lock. /// /// If the lock could not be acquired at this time, then [`Err`] is returned. /// Otherwise, an RAII guard is returned. The lock will be unlocked when the /// guard is dropped. /// /// This function does not preempt. /// /// Note however that it still needs to lock the internal [`SpinLock`], and might temporarily /// be blocking. /// /// # Double locking /// /// Unlike [`lock`], this function will not panic if we already are the holder of this mutex, /// and simply return [`Err`] instead. /// /// This makes it suitable for the kernel panic handler for example, where we want to acquire /// locks to resources possibly already held by the current thread, without panicking once more. /// /// [`lock`]: Mutex::lock pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>> { unsafe { if self.inner.try_lock() { Ok(MutexGuard::new(self)) } else { Err(()) } } } /// Returns a mutable reference to the underlying data. /// /// Since this call borrows the `Mutex` mutably, no actual locking needs to /// take place -- the mutable borrow statically guarantees no locks exist. /// /// # Examples /// /// ``` /// use std::sync::Mutex; /// /// let mut mutex = Mutex::new(0); /// *mutex.get_mut().unwrap() = 10; /// assert_eq!(*mutex.lock().unwrap(), 10); /// ``` pub fn get_mut(&mut self) -> &mut T { unsafe { // safe: // We know statically that there are no other references to `self`, so // there's no need to lock the inner mutex. &mut *self.data.get() } } } impl<T> From<T> for Mutex<T> { /// Creates a new mutex in an unlocked state ready for use. /// This is equivalent to [`Mutex::new`]. /// /// [`Mutex::new`]: ../../std/sync/struct.Mutex.html#method.new fn from(t: T) -> Self { Mutex::new(t) } } impl<T: Default> Default for Mutex<T> { /// Creates a `Mutex<T>`, with the `Default` value for T. fn default() -> Mutex<T> { Mutex::new(Default::default()) } } impl<T: fmt::Debug> fmt::Debug for Mutex<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self.try_lock() { Ok(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(), Err(()) => { /// Struct displayed as `<locked>`. struct LockedPlaceholder; impl fmt::Debug for LockedPlaceholder { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("<locked>") } } f.debug_struct("Mutex").field("data", &LockedPlaceholder).finish() } } } } /* *************************************** MUTEX GUARD ****************************************** */ // copied from std, no edits // impl<'mutex, T> MutexGuard<'mutex, T> { /// Create an MutexGuard. /// /// # Safety /// /// Must only be called once we are ensured we are holing the lock, /// as it will unlock it when dropped unsafe fn new(lock: &'mutex Mutex<T>) -> MutexGuard<'mutex, T> { MutexGuard { __lock: lock, __phantom: PhantomData, } } } impl<T> Deref for MutexGuard<'_, T> { type Target = T; fn deref(&self) -> &T { unsafe { &*self.__lock.data.get() } } } impl<T> DerefMut for MutexGuard<'_, T> { fn deref_mut(&mut self) -> &mut T { unsafe { &mut *self.__lock.data.get() } } } impl<T> Drop for MutexGuard<'_, T> { #[inline] fn drop(&mut self) { unsafe { self.__lock.inner.raw_unlock(); } } } impl<T: fmt::Debug> fmt::Debug for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } impl<T: fmt::Display> fmt::Display for MutexGuard<'_, T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { (**self).fmt(f) } } /* *************************************** MUTEX INNER ****************************************** */ // original edits for sunrise kernel // impl MutexInner { /// Try to obtain the mutex, without preempting. /// /// Returns false if the mutex was not immediately available. unsafe fn try_lock(&self) -> bool { let mut inner_guard = self.spin_lock.lock(); if let Some(_owner) = inner_guard.owner { // already taken :/ false } else { debug_assert!(inner_guard.waiters.is_empty(), "Mutex is not held, but there are some waiters"); // wow cool ! take it let me = &*get_current_thread() as *const ThreadStruct as usize; inner_guard.owner = Some(me); true } } /// Locks the mutex blocking the current thread until it is available. /// /// # Panics /// /// Panics if we're already the owner of the mutex, as this is a deadlock otherwise. unsafe fn raw_lock(&self) { let me = get_current_thread(); let mut inner_guard = self.spin_lock.lock(); if let Some(owner) = inner_guard.owner { if owner == &*me as *const ThreadStruct as usize { panic!("Deadlock ! Re-taking the mutex when we already are its owner"); } // add ourselves to the queue of waiters, inner_guard.waiters.push(me); // and unschedule. // unschedule will drop the inner_guard only once we're properly unscheduled, // so that we can't miss a wake-up between the registration and actual unschedule. // // it will also re-lock the inner_guard for us when we are finally waked up, // but we don't care about that, so immediately drop it. let _ = unschedule(&self.spin_lock, inner_guard); // cool, we now have the mutex for us, // return. } else { // no owner, we can take it ! debug_assert!(inner_guard.waiters.is_empty(), "Mutex is not held, but there are some waiters"); inner_guard.owner = Some(&*me as *const ThreadStruct as usize); } } /// Unlocks the mutex. /// /// Consider switching from the pair of raw_lock() and raw_unlock() to /// lock() whenever possible. /// /// # Panics /// /// Panics if the mutex wasn't held, or if our thread was not the owner of this mutex, /// as this definitely is a bug and we shouldn't have created a MutexGuard for it. unsafe fn raw_unlock(&self) { let me = &*get_current_thread() as *const ThreadStruct as usize; let mut inner = self.spin_lock.lock(); match inner.owner { None => panic!("Unlocked a non-held mutex"), Some(x) if x != me => panic!("Unlocked a mutex held by someone else"), Some(_) => (), } if inner.waiters.is_empty() { // no waiter, make the mutex non-held and return inner.owner = None } else { // has a waiter, make it the owner of the mutex, schedule it, and return let waiter = inner.waiters.remove(0); inner.owner = Some(&*waiter as *const ThreadStruct as usize); add_to_schedule_queue(waiter); } } } /* ****************************************** TESTS ********************************************* */ /* Only tests what's trivially testable :/ */ #[cfg(test)] mod tests { /* use crate::sync::mpsc::channel; use crate::sync::{Arc, Mutex, Condvar}; use crate::sync::atomic::{AtomicUsize, Ordering}; use crate::thread; */ use crate::sync::Mutex; use core::sync::atomic::{AtomicUsize, Ordering}; use alloc::sync::Arc; /* struct Packet<T>(Arc<(Mutex<T>, Condvar)>); */ #[derive(Eq, PartialEq, Debug)] struct NonCopy(i32); /* #[test] fn smoke() { let m = Mutex::new(()); drop(m.lock()); drop(m.lock()); } */ /* #[test] fn lots_and_lots() { const J: u32 = 1000; const K: u32 = 3; let m = Arc::new(Mutex::new(0)); fn inc(m: &Mutex<u32>) { for _ in 0..J { *m.lock().unwrap() += 1; } } let (tx, rx) = channel(); for _ in 0..K { let tx2 = tx.clone(); let m2 = m.clone(); thread::spawn(move|| { inc(&m2); tx2.send(()).unwrap(); }); let tx2 = tx.clone(); let m2 = m.clone(); thread::spawn(move|| { inc(&m2); tx2.send(()).unwrap(); }); } drop(tx); for _ in 0..2 * K { rx.recv().unwrap(); } assert_eq!(*m.lock().unwrap(), J * K * 2); } */ /* #[test] fn try_lock() { let m = Mutex::new(()); *m.try_lock().unwrap() = (); } */ #[test] fn test_into_inner() { let m = Mutex::new(NonCopy(10)); assert_eq!(m.into_inner(), NonCopy(10)); } #[test] fn test_into_inner_drop() { struct Foo(Arc<AtomicUsize>); impl Drop for Foo { fn drop(&mut self) { self.0.fetch_add(1, Ordering::SeqCst); } } let num_drops = Arc::new(AtomicUsize::new(0)); let m = Mutex::new(Foo(num_drops.clone())); assert_eq!(num_drops.load(Ordering::SeqCst), 0); { let _inner = m.into_inner(); assert_eq!(num_drops.load(Ordering::SeqCst), 0); } assert_eq!(num_drops.load(Ordering::SeqCst), 1); } /* no poison here :) #[test] fn test_into_inner_poison() { let m = Arc::new(Mutex::new(NonCopy(10))); let m2 = m.clone(); let _ = thread::spawn(move || { let _lock = m2.lock().unwrap(); panic!("test panic in inner thread to poison mutex"); }).join(); assert!(m.is_poisoned()); match Arc::try_unwrap(m).unwrap().into_inner() { Err(e) => assert_eq!(e.into_inner(), NonCopy(10)), Ok(x) => panic!("into_inner of poisoned Mutex is Ok: {:?}", x), } } */ #[test] fn test_get_mut() { let mut m = Mutex::new(NonCopy(10)); *m.get_mut() = NonCopy(20); assert_eq!(m.into_inner(), NonCopy(20)); } /* no poison :) #[test] fn test_get_mut_poison() { let m = Arc::new(Mutex::new(NonCopy(10))); let m2 = m.clone(); let _ = thread::spawn(move || { let _lock = m2.lock().unwrap(); panic!("test panic in inner thread to poison mutex"); }).join(); assert!(m.is_poisoned()); match Arc::try_unwrap(m).unwrap().get_mut() { Err(e) => assert_eq!(*e.into_inner(), NonCopy(10)), Ok(x) => panic!("get_mut of poisoned Mutex is Ok: {:?}", x), } } */ /* #[test] fn test_mutex_arc_condvar() { let packet = Packet(Arc::new((Mutex::new(false), Condvar::new()))); let packet2 = Packet(packet.0.clone()); let (tx, rx) = channel(); let _t = thread::spawn(move|| { // wait until parent gets in rx.recv().unwrap(); let &(ref lock, ref cvar) = &*packet2.0; let mut lock = lock.lock().unwrap(); *lock = true; cvar.notify_one(); }); let &(ref lock, ref cvar) = &*packet.0; let mut lock = lock.lock().unwrap(); tx.send(()).unwrap(); assert!(!*lock); while !*lock { lock = cvar.wait(lock).unwrap(); } } */ /* no poison :) #[test] fn test_arc_condvar_poison() { let packet = Packet(Arc::new((Mutex::new(1), Condvar::new()))); let packet2 = Packet(packet.0.clone()); let (tx, rx) = channel(); let _t = thread::spawn(move || -> () { rx.recv().unwrap(); let &(ref lock, ref cvar) = &*packet2.0; let _g = lock.lock().unwrap(); cvar.notify_one(); // Parent should fail when it wakes up. panic!(); }); let &(ref lock, ref cvar) = &*packet.0; let mut lock = lock.lock().unwrap(); tx.send(()).unwrap(); while *lock == 1 { match cvar.wait(lock) { Ok(l) => { lock = l; assert_eq!(*lock, 1); } Err(..) => break, } } } */ /* no poison :) #[test] fn test_mutex_arc_poison() { let arc = Arc::new(Mutex::new(1)); assert!(!arc.is_poisoned()); let arc2 = arc.clone(); let _ = thread::spawn(move|| { let lock = arc2.lock().unwrap(); assert_eq!(*lock, 2); }).join(); assert!(arc.lock().is_err()); assert!(arc.is_poisoned()); } */ /* #[test] fn test_mutex_arc_nested() { // Tests nested mutexes and access // to underlying data. let arc = Arc::new(Mutex::new(1)); let arc2 = Arc::new(Mutex::new(arc)); let (tx, rx) = channel(); let _t = thread::spawn(move|| { let lock = arc2.lock().unwrap(); let lock2 = lock.lock().unwrap(); assert_eq!(*lock2, 1); tx.send(()).unwrap(); }); rx.recv().unwrap(); } */ /* #[test] fn test_mutex_arc_access_in_unwind() { let arc = Arc::new(Mutex::new(1)); let arc2 = arc.clone(); let _ = thread::spawn(move|| -> () { struct Unwinder { i: Arc<Mutex<i32>>, } impl Drop for Unwinder { fn drop(&mut self) { *self.i.lock().unwrap() += 1; } } let _u = Unwinder { i: arc2 }; panic!(); }).join(); let lock = arc.lock().unwrap(); assert_eq!(*lock, 2); } */ /* our mutex don't work on T: ?Sized #[test] fn test_mutex_unsized() { let mutex: &Mutex<[i32]> = &Mutex::new([1, 2, 3]); { let b = &mut *mutex.lock().unwrap(); b[0] = 4; b[2] = 5; } let comp: &[i32] = &[4, 2, 5]; assert_eq!(&*mutex.lock().unwrap(), comp); } */ }