use wdk_sys::{NTSTATUS, WDF_OBJECT_ATTRIBUTES, WDFSPINLOCK, call_unsafe_wdf_function_binding};
use crate::nt_success;
pub struct SpinLock {
wdf_spin_lock: WDFSPINLOCK,
}
impl SpinLock {
pub fn try_new(attributes: &mut WDF_OBJECT_ATTRIBUTES) -> Result<Self, NTSTATUS> {
let mut spin_lock = Self {
wdf_spin_lock: core::ptr::null_mut(),
};
let nt_status;
unsafe {
nt_status = call_unsafe_wdf_function_binding!(
WdfSpinLockCreate,
attributes,
&mut spin_lock.wdf_spin_lock as *mut _,
);
}
nt_success(nt_status).then_some(spin_lock).ok_or(nt_status)
}
pub fn create(attributes: &mut WDF_OBJECT_ATTRIBUTES) -> Result<Self, NTSTATUS> {
Self::try_new(attributes)
}
pub fn acquire(&self) {
unsafe {
call_unsafe_wdf_function_binding!(WdfSpinLockAcquire, self.wdf_spin_lock);
}
}
pub fn release(&self) {
unsafe {
call_unsafe_wdf_function_binding!(WdfSpinLockRelease, self.wdf_spin_lock);
}
}
}