diff options
Diffstat (limited to 'src/unlock_notify.rs')
-rw-r--r-- | src/unlock_notify.rs | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/src/unlock_notify.rs b/src/unlock_notify.rs index 5f9cdbf..8fba6b3 100644 --- a/src/unlock_notify.rs +++ b/src/unlock_notify.rs @@ -1,22 +1,17 @@ //! [Unlock Notification](http://sqlite.org/unlock_notify.html) use std::os::raw::c_int; -#[cfg(feature = "unlock_notify")] use std::os::raw::c_void; -#[cfg(feature = "unlock_notify")] use std::panic::catch_unwind; -#[cfg(feature = "unlock_notify")] use std::sync::{Condvar, Mutex}; use crate::ffi; -#[cfg(feature = "unlock_notify")] struct UnlockNotification { cond: Condvar, // Condition variable to wait on mutex: Mutex<bool>, // Mutex to protect structure } -#[cfg(feature = "unlock_notify")] #[allow(clippy::mutex_atomic)] impl UnlockNotification { fn new() -> UnlockNotification { @@ -27,30 +22,33 @@ impl UnlockNotification { } fn fired(&self) { - let mut flag = self.mutex.lock().unwrap(); + let mut flag = unpoison(self.mutex.lock()); *flag = true; self.cond.notify_one(); } fn wait(&self) { - let mut fired = self.mutex.lock().unwrap(); + let mut fired = unpoison(self.mutex.lock()); while !*fired { - fired = self.cond.wait(fired).unwrap(); + fired = unpoison(self.cond.wait(fired)); } } } +#[inline] +fn unpoison<T>(r: Result<T, std::sync::PoisonError<T>>) -> T { + r.unwrap_or_else(std::sync::PoisonError::into_inner) +} + /// This function is an unlock-notify callback -#[cfg(feature = "unlock_notify")] unsafe extern "C" fn unlock_notify_cb(ap_arg: *mut *mut c_void, n_arg: c_int) { use std::slice::from_raw_parts; let args = from_raw_parts(ap_arg as *const &UnlockNotification, n_arg as usize); for un in args { - let _ = catch_unwind(std::panic::AssertUnwindSafe(|| un.fired())); + drop(catch_unwind(std::panic::AssertUnwindSafe(|| un.fired()))); } } -#[cfg(feature = "unlock_notify")] pub unsafe fn is_locked(db: *mut ffi::sqlite3, rc: c_int) -> bool { rc == ffi::SQLITE_LOCKED_SHAREDCACHE || (rc & 0xFF) == ffi::SQLITE_LOCKED @@ -87,17 +85,6 @@ pub unsafe fn wait_for_unlock_notify(db: *mut ffi::sqlite3) -> c_int { rc } -#[cfg(not(feature = "unlock_notify"))] -pub unsafe fn is_locked(_db: *mut ffi::sqlite3, _rc: c_int) -> bool { - unreachable!() -} - -#[cfg(not(feature = "unlock_notify"))] -pub unsafe fn wait_for_unlock_notify(_db: *mut ffi::sqlite3) -> c_int { - unreachable!() -} - -#[cfg(feature = "unlock_notify")] #[cfg(test)] mod test { use crate::{Connection, OpenFlags, Result, Transaction, TransactionBehavior}; |