2017-09-21 03:28:19 +08:00
|
|
|
//! [Unlock Notification](http://sqlite.org/unlock_notify.html)
|
|
|
|
|
2017-09-22 01:27:07 +08:00
|
|
|
use std::os::raw::c_int;
|
|
|
|
use std::os::raw::c_void;
|
2018-12-16 16:40:14 +08:00
|
|
|
use std::panic::catch_unwind;
|
2018-08-11 18:48:21 +08:00
|
|
|
use std::sync::{Condvar, Mutex};
|
2017-09-21 03:28:19 +08:00
|
|
|
|
2018-10-31 03:11:35 +08:00
|
|
|
use crate::ffi;
|
2017-09-21 03:28:19 +08:00
|
|
|
|
|
|
|
struct UnlockNotification {
|
2017-09-22 01:27:07 +08:00
|
|
|
cond: Condvar, // Condition variable to wait on
|
2017-09-21 03:28:19 +08:00
|
|
|
mutex: Mutex<bool>, // Mutex to protect structure
|
|
|
|
}
|
|
|
|
|
2019-10-19 16:10:17 +08:00
|
|
|
#[allow(clippy::mutex_atomic)]
|
2017-09-21 03:28:19 +08:00
|
|
|
impl UnlockNotification {
|
|
|
|
fn new() -> UnlockNotification {
|
|
|
|
UnlockNotification {
|
|
|
|
cond: Condvar::new(),
|
|
|
|
mutex: Mutex::new(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 03:05:31 +08:00
|
|
|
fn fired(&self) {
|
2022-01-05 12:23:32 +08:00
|
|
|
let mut flag = unpoison(self.mutex.lock());
|
2020-04-16 03:05:31 +08:00
|
|
|
*flag = true;
|
2017-09-21 03:28:19 +08:00
|
|
|
self.cond.notify_one();
|
|
|
|
}
|
|
|
|
|
2020-04-16 03:05:31 +08:00
|
|
|
fn wait(&self) {
|
2022-01-05 12:23:32 +08:00
|
|
|
let mut fired = unpoison(self.mutex.lock());
|
2018-05-05 00:07:11 +08:00
|
|
|
while !*fired {
|
2022-01-05 12:23:32 +08:00
|
|
|
fired = unpoison(self.cond.wait(fired));
|
2017-09-21 03:28:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:23:32 +08:00
|
|
|
#[inline]
|
|
|
|
fn unpoison<T>(r: Result<T, std::sync::PoisonError<T>>) -> T {
|
|
|
|
r.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
|
|
}
|
|
|
|
|
2017-09-21 03:28:19 +08:00
|
|
|
/// This function is an unlock-notify callback
|
|
|
|
unsafe extern "C" fn unlock_notify_cb(ap_arg: *mut *mut c_void, n_arg: c_int) {
|
2017-09-22 02:19:23 +08:00
|
|
|
use std::slice::from_raw_parts;
|
2020-04-16 03:05:31 +08:00
|
|
|
let args = from_raw_parts(ap_arg as *const &UnlockNotification, n_arg as usize);
|
|
|
|
for un in args {
|
2022-01-06 02:26:06 +08:00
|
|
|
drop(catch_unwind(std::panic::AssertUnwindSafe(|| un.fired())));
|
2017-09-22 02:19:23 +08:00
|
|
|
}
|
2017-09-21 03:28:19 +08:00
|
|
|
}
|
|
|
|
|
2020-04-07 10:38:33 +08:00
|
|
|
pub unsafe fn is_locked(db: *mut ffi::sqlite3, rc: c_int) -> bool {
|
2018-08-11 18:48:21 +08:00
|
|
|
rc == ffi::SQLITE_LOCKED_SHAREDCACHE
|
|
|
|
|| (rc & 0xFF) == ffi::SQLITE_LOCKED
|
2020-04-07 10:38:33 +08:00
|
|
|
&& ffi::sqlite3_extended_errcode(db) == ffi::SQLITE_LOCKED_SHAREDCACHE
|
2018-03-31 16:22:19 +08:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:27:07 +08:00
|
|
|
/// This function assumes that an SQLite API call (either `sqlite3_prepare_v2()`
|
|
|
|
/// or `sqlite3_step()`) has just returned `SQLITE_LOCKED`. The argument is the
|
|
|
|
/// associated database connection.
|
|
|
|
///
|
|
|
|
/// This function calls `sqlite3_unlock_notify()` to register for an
|
|
|
|
/// unlock-notify callback, then blocks until that callback is delivered
|
|
|
|
/// and returns `SQLITE_OK`. The caller should then retry the failed operation.
|
|
|
|
///
|
|
|
|
/// Or, if `sqlite3_unlock_notify()` indicates that to block would deadlock
|
|
|
|
/// the system, then this function returns `SQLITE_LOCKED` immediately. In
|
|
|
|
/// this case the caller should not retry the operation and should roll
|
|
|
|
/// back the current transaction (if any).
|
|
|
|
#[cfg(feature = "unlock_notify")]
|
2020-04-07 10:38:33 +08:00
|
|
|
pub unsafe fn wait_for_unlock_notify(db: *mut ffi::sqlite3) -> c_int {
|
2020-04-16 03:05:31 +08:00
|
|
|
let un = UnlockNotification::new();
|
2017-09-22 01:27:07 +08:00
|
|
|
/* Register for an unlock-notify callback. */
|
2020-04-07 10:38:33 +08:00
|
|
|
let rc = ffi::sqlite3_unlock_notify(
|
|
|
|
db,
|
|
|
|
Some(unlock_notify_cb),
|
2020-04-16 03:05:31 +08:00
|
|
|
&un as *const UnlockNotification as *mut c_void,
|
2020-04-07 10:38:33 +08:00
|
|
|
);
|
2018-03-28 03:49:09 +08:00
|
|
|
debug_assert!(
|
|
|
|
rc == ffi::SQLITE_LOCKED || rc == ffi::SQLITE_LOCKED_SHAREDCACHE || rc == ffi::SQLITE_OK
|
|
|
|
);
|
2017-09-22 01:27:07 +08:00
|
|
|
if rc == ffi::SQLITE_OK {
|
|
|
|
un.wait();
|
2017-09-21 03:28:19 +08:00
|
|
|
}
|
2017-09-22 01:27:07 +08:00
|
|
|
rc
|
|
|
|
}
|
2017-09-21 03:28:19 +08:00
|
|
|
|
2017-09-23 05:27:05 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-11-03 17:32:46 +08:00
|
|
|
use crate::{Connection, OpenFlags, Result, Transaction, TransactionBehavior};
|
2017-09-23 05:27:05 +08:00
|
|
|
use std::sync::mpsc::sync_channel;
|
|
|
|
use std::thread;
|
|
|
|
use std::time;
|
|
|
|
|
|
|
|
#[test]
|
2020-11-06 05:14:00 +08:00
|
|
|
fn test_unlock_notify() -> Result<()> {
|
2017-09-23 05:27:05 +08:00
|
|
|
let url = "file::memory:?cache=shared";
|
2018-03-24 17:15:10 +08:00
|
|
|
let flags = OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_URI;
|
2020-11-06 05:14:00 +08:00
|
|
|
let db1 = Connection::open_with_flags(url, flags)?;
|
|
|
|
db1.execute_batch("CREATE TABLE foo (x)")?;
|
2018-03-24 17:15:10 +08:00
|
|
|
let (rx, tx) = sync_channel(0);
|
2017-09-23 05:27:05 +08:00
|
|
|
let child = thread::spawn(move || {
|
|
|
|
let mut db2 = Connection::open_with_flags(url, flags).unwrap();
|
|
|
|
let tx2 = Transaction::new(&mut db2, TransactionBehavior::Immediate).unwrap();
|
|
|
|
tx2.execute_batch("INSERT INTO foo VALUES (42)").unwrap();
|
2018-03-24 17:15:10 +08:00
|
|
|
rx.send(1).unwrap();
|
2017-09-23 05:27:05 +08:00
|
|
|
let ten_millis = time::Duration::from_millis(10);
|
|
|
|
thread::sleep(ten_millis);
|
|
|
|
tx2.commit().unwrap();
|
|
|
|
});
|
2018-03-24 17:15:10 +08:00
|
|
|
assert_eq!(tx.recv().unwrap(), 1);
|
2022-11-11 23:32:42 +08:00
|
|
|
let the_answer: i64 = db1.one_column("SELECT x FROM foo")?;
|
|
|
|
assert_eq!(42i64, the_answer);
|
2017-09-23 05:27:05 +08:00
|
|
|
child.join().unwrap();
|
2020-11-06 05:14:00 +08:00
|
|
|
Ok(())
|
2017-09-23 05:27:05 +08:00
|
|
|
}
|
|
|
|
}
|