ksync/
raw_kernel_brwlock.rs1use core::ffi::c_void;
6use core::pin::Pin;
7use pin_init::{PinInit, pin_data, pinned_drop};
8
9unsafe extern "C" {
10 fn cpp_brwlock_pi_init(lock: *mut c_void, class_id: *const c_void);
11 fn cpp_brwlock_pi_destroy(lock: *mut c_void);
12 fn cpp_brwlock_pi_acquire_read(lock: *mut c_void, entry_storage: *mut c_void);
13 fn cpp_brwlock_pi_release_read(lock: *mut c_void, entry_storage: *mut c_void);
14 fn cpp_brwlock_pi_acquire_write(lock: *mut c_void, entry_storage: *mut c_void);
15 fn cpp_brwlock_pi_release_write(lock: *mut c_void, entry_storage: *mut c_void);
16}
17
18#[cfg(not(target_arch = "riscv64"))]
19#[repr(C, align(16))]
20struct RawBrwLockPiStorage(zr::OpaqueBytes<128>);
21
22#[cfg(target_arch = "riscv64")]
23#[repr(C, align(8))]
24struct RawBrwLockPiStorage(zr::OpaqueBytes<72>);
25
26#[cfg(not(target_arch = "riscv64"))]
28#[pin_data(PinnedDrop)]
29#[repr(C, align(16))]
30pub struct RawBrwLockPi {
31 #[cfg(feature = "lock_dep")]
32 class_id: *const c_void,
33 storage: RawBrwLockPiStorage,
34}
35
36#[cfg(target_arch = "riscv64")]
37#[pin_data(PinnedDrop)]
38#[repr(C, align(8))]
39pub struct RawBrwLockPi {
40 #[cfg(feature = "lock_dep")]
41 class_id: *const c_void,
42 storage: RawBrwLockPiStorage,
43}
44
45unsafe impl Sync for RawBrwLockPi {}
48unsafe impl Send for RawBrwLockPi {}
49
50#[pinned_drop]
51impl PinnedDrop for RawBrwLockPi {
52 fn drop(self: Pin<&mut Self>) {
53 unsafe {
57 let me = self.get_unchecked_mut();
58 cpp_brwlock_pi_destroy(me.as_mut_ptr());
59 }
60 }
61}
62
63impl RawBrwLockPi {
64 pub const LOCK_FLAGS: lockdep::LockFlags = lockdep::LOCK_FLAGS_NONE;
65
66 #[inline]
73 pub unsafe fn init(class_id: *const c_void) -> impl PinInit<Self, core::convert::Infallible> {
74 zr::pin_init_ffi!(cpp_brwlock_pi_init, class_id)
75 }
76
77 #[inline]
79 pub fn as_mut_ptr(&self) -> *mut c_void {
80 self as *const Self as *mut Self as *mut c_void
81 }
82
83 #[inline]
90 pub unsafe fn acquire_read(&self, entry: *mut c_void) {
91 unsafe {
94 cpp_brwlock_pi_acquire_read(self.as_mut_ptr(), entry);
95 }
96 }
97
98 #[inline]
105 pub unsafe fn release_read(&self, entry: *mut c_void) {
106 unsafe {
109 cpp_brwlock_pi_release_read(self.as_mut_ptr(), entry);
110 }
111 }
112
113 #[inline]
120 pub unsafe fn acquire_write(&self, entry: *mut c_void) {
121 unsafe {
124 cpp_brwlock_pi_acquire_write(self.as_mut_ptr(), entry);
125 }
126 }
127
128 #[inline]
135 pub unsafe fn release_write(&self, entry: *mut c_void) {
136 unsafe {
139 cpp_brwlock_pi_release_write(self.as_mut_ptr(), entry);
140 }
141 }
142}
143
144#[cfg(all(not(target_arch = "riscv64"), not(feature = "lock_dep")))]
145const _: () = {
146 assert!(core::mem::size_of::<RawBrwLockPi>() == 128);
147 assert!(core::mem::align_of::<RawBrwLockPi>() == 16);
148};
149
150#[cfg(all(not(target_arch = "riscv64"), feature = "lock_dep"))]
151const _: () = {
152 assert!(core::mem::size_of::<RawBrwLockPi>() == 144);
153 assert!(core::mem::align_of::<RawBrwLockPi>() == 16);
154};
155
156#[cfg(all(target_arch = "riscv64", not(feature = "lock_dep")))]
157const _: () = {
158 assert!(core::mem::size_of::<RawBrwLockPi>() == 72);
159 assert!(core::mem::align_of::<RawBrwLockPi>() == 8);
160};
161
162#[cfg(all(target_arch = "riscv64", feature = "lock_dep"))]
163const _: () = {
164 assert!(core::mem::size_of::<RawBrwLockPi>() == 80);
165 assert!(core::mem::align_of::<RawBrwLockPi>() == 8);
166};