~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/sync/locked_by.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/kernel/sync/locked_by.rs (Version linux-6.12-rc7) and /rust/kernel/sync/locked_by.rs (Version linux-4.18.20)


  1 // SPDX-License-Identifier: GPL-2.0               
  2                                                   
  3 //! A wrapper for data protected by a lock tha    
  4                                                   
  5 use super::{lock::Backend, lock::Lock};           
  6 use crate::build_assert;                          
  7 use core::{cell::UnsafeCell, mem::size_of, ptr    
  8                                                   
  9 /// Allows access to some data to be serialise    
 10 ///                                               
 11 /// In most cases, data protected by a lock is    
 12 /// [`Mutex`] or [`SpinLock`]. [`LockedBy`] is    
 13 /// For example, if a container has a lock and    
 14 /// to be protected by the same lock.             
 15 ///                                               
 16 /// [`LockedBy`] wraps the data in lieu of ano    
 17 /// when the caller shows evidence that the 'e    
 18 /// refers to the wrong instance of the lock.     
 19 ///                                               
 20 /// [`Mutex`]: super::Mutex                       
 21 /// [`SpinLock`]: super::SpinLock                 
 22 ///                                               
 23 /// # Examples                                    
 24 ///                                               
 25 /// The following is an example for illustrati    
 26 /// aggregate of all `InnerFile::bytes_used` a    
 27 /// a `LockedBy` so that it shares a lock with    
 28 /// compile-time that access to `InnerFile` is    
 29 /// locked; we enforce at run time that the ri    
 30 ///                                               
 31 /// ```                                           
 32 /// use kernel::sync::{LockedBy, Mutex};          
 33 ///                                               
 34 /// struct InnerFile {                            
 35 ///     bytes_used: u64,                          
 36 /// }                                             
 37 ///                                               
 38 /// struct File {                                 
 39 ///     _ino: u32,                                
 40 ///     inner: LockedBy<InnerFile, InnerDirect    
 41 /// }                                             
 42 ///                                               
 43 /// struct InnerDirectory {                       
 44 ///     /// The sum of the bytes used by all f    
 45 ///     bytes_used: u64,                          
 46 ///     _files: Vec<File>,                        
 47 /// }                                             
 48 ///                                               
 49 /// struct Directory {                            
 50 ///     _ino: u32,                                
 51 ///     inner: Mutex<InnerDirectory>,             
 52 /// }                                             
 53 ///                                               
 54 /// /// Prints `bytes_used` from both the dire    
 55 /// fn print_bytes_used(dir: &Directory, file:    
 56 ///     let guard = dir.inner.lock();             
 57 ///     let inner_file = file.inner.access(&gu    
 58 ///     pr_info!("{} {}", guard.bytes_used, in    
 59 /// }                                             
 60 ///                                               
 61 /// /// Increments `bytes_used` for both the d    
 62 /// fn inc_bytes_used(dir: &Directory, file: &    
 63 ///     let mut guard = dir.inner.lock();         
 64 ///     guard.bytes_used += 10;                   
 65 ///                                               
 66 ///     let file_inner = file.inner.access_mut    
 67 ///     file_inner.bytes_used += 10;              
 68 /// }                                             
 69 ///                                               
 70 /// /// Creates a new file.                       
 71 /// fn new_file(ino: u32, dir: &Directory) ->     
 72 ///     File {                                    
 73 ///         _ino: ino,                            
 74 ///         inner: LockedBy::new(&dir.inner, I    
 75 ///     }                                         
 76 /// }                                             
 77 /// ```                                           
 78 pub struct LockedBy<T: ?Sized, U: ?Sized> {       
 79     owner: *const U,                              
 80     data: UnsafeCell<T>,                          
 81 }                                                 
 82                                                   
 83 // SAFETY: `LockedBy` can be transferred acros    
 84 unsafe impl<T: ?Sized + Send, U: ?Sized> Send     
 85                                                   
 86 // SAFETY: If `T` is not `Sync`, then parallel    
 87 // `access_mut` to hand out `&mut T` on one th    
 88 // sufficient to allow that.                      
 89 //                                                
 90 // If `T` is `Sync`, then the `access` method     
 91 // several `&T` from several threads at once.     
 92 unsafe impl<T: ?Sized + Send, U: ?Sized> Sync     
 93                                                   
 94 impl<T, U> LockedBy<T, U> {                       
 95     /// Constructs a new instance of [`LockedB    
 96     ///                                           
 97     /// It stores a raw pointer to the owner t    
 98     /// that the right owner is being used to     
 99     /// data becomes inaccessible; if another     
100     /// memory location*, the data becomes acc    
101     /// because in any case at most one thread    
102     pub fn new<B: Backend>(owner: &Lock<U, B>,    
103         build_assert!(                            
104             size_of::<Lock<U, B>>() > 0,          
105             "The lock type cannot be a ZST bec    
106         );                                        
107         Self {                                    
108             owner: owner.data.get(),              
109             data: UnsafeCell::new(data),          
110         }                                         
111     }                                             
112 }                                                 
113                                                   
114 impl<T: ?Sized, U> LockedBy<T, U> {               
115     /// Returns a reference to the protected d    
116     /// reference) that the owner is locked.      
117     ///                                           
118     /// `U` cannot be a zero-sized type (ZST)     
119     /// the data protected by the lock without    
120     ///                                           
121     /// # Panics                                  
122     ///                                           
123     /// Panics if `owner` is different from th    
124     /// [`new`](LockedBy::new).                   
125     pub fn access<'a>(&'a self, owner: &'a U)     
126     where                                         
127         T: Sync,                                  
128     {                                             
129         build_assert!(                            
130             size_of::<U>() > 0,                   
131             "`U` cannot be a ZST because `owne    
132         );                                        
133         if !ptr::eq(owner, self.owner) {          
134             panic!("mismatched owners");          
135         }                                         
136                                                   
137         // SAFETY: `owner` is evidence that th    
138         // duration of 'a, so it's not possibl    
139         // reference to the inner value that a    
140         // so there are no other requirements.    
141         unsafe { &*self.data.get() }              
142     }                                             
143                                                   
144     /// Returns a mutable reference to the pro    
145     /// mutable owner) that the owner is locke    
146     ///                                           
147     /// `U` cannot be a zero-sized type (ZST)     
148     /// matches the data protected by the lock    
149     ///                                           
150     /// Showing a mutable reference to the own    
151     /// can exist to it.                          
152     ///                                           
153     /// # Panics                                  
154     ///                                           
155     /// Panics if `owner` is different from th    
156     /// [`new`](LockedBy::new).                   
157     pub fn access_mut<'a>(&'a self, owner: &'a    
158         build_assert!(                            
159             size_of::<U>() > 0,                   
160             "`U` cannot be a ZST because `owne    
161         );                                        
162         if !ptr::eq(owner, self.owner) {          
163             panic!("mismatched owners");          
164         }                                         
165                                                   
166         // SAFETY: `owner` is evidence that th    
167         unsafe { &mut *self.data.get() }          
168     }                                             
169 }                                                 
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php