/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ //! Different objects protected by the same lock use crate::derives::MallocSizeOf; use crate::stylesheets::Origin; use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut}; use servo_arc::Arc; use std::cell::UnsafeCell; use std::fmt; use std::ptr; use style_traits::{CssString, CssStringWriter}; use to_shmem::{SharedMemoryBuilder, ToShmem}; /// A shared read/write lock that can protect multiple objects. /// /// We don't need the blocking behavior, just the safety. As such we implement /// this with an AtomicRefCell, which is ~2x as fast as an RwLock, and panics /// (rather than deadlocking) when things go wrong (which is much easier to /// debug on CI). /// /// Gecko also needs the ability to have "read only" SharedRwLocks, which are /// used for objects stored in (read only) shared memory. Attempting to acquire /// write access to objects protected by a read only SharedRwLock will panic. #[derive(Clone)] pub struct SharedRwLock { cell: Option>>, } #[cfg(feature = "servo")] malloc_size_of::malloc_size_of_is_0!(SharedRwLock); #[derive(MallocSizeOf)] struct SomethingZeroSizedButTyped; impl fmt::Debug for SharedRwLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("SharedRwLock") } } impl Default for SharedRwLock { fn default() -> Self { Self::new() } } impl SharedRwLock { /// Create a new shared lock. pub fn new() -> Self { SharedRwLock { cell: Some(Arc::new(AtomicRefCell::new(SomethingZeroSizedButTyped))), } } /// Create a new global shared lock. pub fn new_leaked() -> Self { SharedRwLock { cell: Some(Arc::new_leaked(AtomicRefCell::new( SomethingZeroSizedButTyped, ))), } } /// Create a new read-only shared lock. pub fn read_only() -> Self { SharedRwLock { cell: None } } #[inline] fn ptr(&self) -> *const SomethingZeroSizedButTyped { self.cell .as_ref() .map(|cell| cell.as_ptr() as *const _) .unwrap_or(ptr::null()) } /// Wrap the given data to make its access protected by this lock. pub fn wrap(&self, data: T) -> Locked { Locked { shared_lock: self.clone(), data: UnsafeCell::new(data), } } /// Obtain the lock for reading. pub fn read(&self) -> SharedRwLockReadGuard<'_> { SharedRwLockReadGuard(self.cell.as_ref().map(|cell| cell.borrow())) } /// Obtain the lock for writing. pub fn write(&self) -> SharedRwLockWriteGuard<'_> { SharedRwLockWriteGuard(self.cell.as_ref().unwrap().borrow_mut()) } } /// Proof that a shared lock was obtained for reading. pub struct SharedRwLockReadGuard<'a>(Option>); impl<'a> SharedRwLockReadGuard<'a> { #[inline] fn ptr(&self) -> *const SomethingZeroSizedButTyped { self.0 .as_ref() .map(|r| &**r as *const _) .unwrap_or(ptr::null()) } } /// Proof that a shared lock was obtained for writing. pub struct SharedRwLockWriteGuard<'a>(AtomicRefMut<'a, SomethingZeroSizedButTyped>); /// Data protect by a shared lock. pub struct Locked { shared_lock: SharedRwLock, data: UnsafeCell, } // Unsafe: the data inside `UnsafeCell` is only accessed in `read_with` and `write_with`, // where guards ensure synchronization. unsafe impl Send for Locked {} unsafe impl Sync for Locked {} impl fmt::Debug for Locked { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let guard = self.shared_lock.read(); self.read_with(&guard).fmt(f) } } impl Locked { #[inline] fn is_read_only_lock(&self) -> bool { self.shared_lock.cell.is_none() } fn same_lock_as(&self, ptr: *const SomethingZeroSizedButTyped) -> bool { ptr::eq(self.shared_lock.ptr(), ptr) } /// Access the data for reading. pub fn read_with<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a T { assert!( self.is_read_only_lock() || self.same_lock_as(guard.ptr()), "Locked::read_with called with a guard from an unrelated SharedRwLock: {:?} vs. {:?}", self.shared_lock.ptr(), guard.ptr(), ); let ptr = self.data.get(); // Unsafe: // // * The guard guarantees that the lock is taken for reading, // and we’ve checked that it’s the correct lock. // * The returned reference borrows *both* the data and the guard, // so that it can outlive neither. unsafe { &*ptr } } /// Access the data for reading without verifying the lock. Use with caution. pub unsafe fn read_unchecked(&self) -> &T { unsafe { let ptr = self.data.get(); &*ptr } } /// Access the data for writing. pub fn write_with<'a>(&'a self, guard: &'a mut SharedRwLockWriteGuard) -> &'a mut T { assert!( !self.is_read_only_lock() && self.same_lock_as(&*guard.0), "Locked::write_with called with a guard from a read only or unrelated SharedRwLock" ); let ptr = self.data.get(); // Unsafe: // // * The guard guarantees that the lock is taken for writing, // and we’ve checked that it’s the correct lock. // * The returned reference borrows *both* the data and the guard, // so that it can outlive neither. // * We require a mutable borrow of the guard, // so that one write guard can only be used once at a time. unsafe { &mut *ptr } } } impl ToShmem for Locked { fn to_shmem(&self, builder: &mut SharedMemoryBuilder) -> to_shmem::Result { use std::mem::ManuallyDrop; let guard = self.shared_lock.read(); Ok(ManuallyDrop::new(Locked { shared_lock: SharedRwLock::read_only(), data: UnsafeCell::new(ManuallyDrop::into_inner( self.read_with(&guard).to_shmem(builder)?, )), })) } } #[allow(dead_code)] mod compile_time_assert { use super::{SharedRwLockReadGuard, SharedRwLockWriteGuard}; trait Marker1 {} impl Marker1 for T {} impl<'a> Marker1 for SharedRwLockReadGuard<'a> {} // Assert SharedRwLockReadGuard: !Clone impl<'a> Marker1 for SharedRwLockWriteGuard<'a> {} // Assert SharedRwLockWriteGuard: !Clone trait Marker2 {} impl Marker2 for T {} impl<'a> Marker2 for SharedRwLockReadGuard<'a> {} // Assert SharedRwLockReadGuard: !Copy impl<'a> Marker2 for SharedRwLockWriteGuard<'a> {} // Assert SharedRwLockWriteGuard: !Copy } /// Like ToCss, but with a lock guard given by the caller, and with the writer specified /// concretely rather than with a parameter. pub trait ToCssWithGuard { /// Serialize `self` in CSS syntax, writing to `dest`, using the given lock guard. fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result; /// Serialize `self` in CSS syntax using the given lock guard and return a string. /// /// (This is a convenience wrapper for `to_css` and probably should not be overridden.) #[inline] fn to_css_string(&self, guard: &SharedRwLockReadGuard) -> CssString { let mut s = CssString::new(); self.to_css(guard, &mut s).unwrap(); s } } /// A trait to do a deep clone of a given CSS type. Gets a lock and a read /// guard, in order to be able to read and clone nested structures. pub trait DeepCloneWithLock: Sized { /// Deep clones this object. fn deep_clone_with_lock(&self, lock: &SharedRwLock, guard: &SharedRwLockReadGuard) -> Self; } /// Guards for a document #[derive(Clone)] pub struct StylesheetGuards<'a> { /// For author-origin stylesheets. pub author: &'a SharedRwLockReadGuard<'a>, /// For user-agent-origin and user-origin stylesheets pub ua_or_user: &'a SharedRwLockReadGuard<'a>, } impl<'a> StylesheetGuards<'a> { /// Get the guard for a given stylesheet origin. pub fn for_origin(&self, origin: Origin) -> &SharedRwLockReadGuard<'a> { match origin { Origin::Author => self.author, _ => self.ua_or_user, } } /// Same guard for all origins pub fn same(guard: &'a SharedRwLockReadGuard<'a>) -> Self { StylesheetGuards { author: guard, ua_or_user: guard, } } }