@@ -5,14 +5,9 @@ use core::ptr::{self, null_mut};
55use alloc:: format;
66use alloc:: string:: String ;
77
8- use lock_api:: { GuardSend , Mutex as MutexApi , RawMutex } ;
9- use serde:: de:: Visitor ;
10- use sqlite_nostd:: bindings:: SQLITE_MUTEX_FAST ;
11- use sqlite_nostd:: { api_routines, Connection , Context } ;
12-
13- use crate :: error:: SQLiteError ;
8+ #[ cfg( not( feature = "getrandom" ) ) ]
149use crate :: sqlite;
15- use crate :: sqlite :: bindings :: sqlite3_mutex ;
10+ use serde :: de :: Visitor ;
1611
1712use uuid:: Uuid ;
1813
@@ -94,103 +89,6 @@ where
9489 deserializer. deserialize_option ( ValueVisitor )
9590}
9691
97- pub struct SqliteMutex {
98- ptr : * mut sqlite3_mutex ,
99- }
100-
101- // We always invoke mutex APIs through the api routines, even when we link the rest of SQLite
102- // statically.
103- // The reason is that it's possible to omit the mutex code (in which case we don't want to link
104- // undefined symbols).
105- pub ( crate ) static mut SQLITE3_API : * mut api_routines = ptr:: null_mut ( ) ;
106-
107- impl SqliteMutex {
108- pub fn new ( ) -> Self {
109- let native_alloc = unsafe {
110- // SAFETY: SQLITE3_API is only set once when the library is loaded by SQLite.
111- ( * SQLITE3_API ) . mutex_alloc
112- } ;
113-
114- Self {
115- ptr : match native_alloc {
116- None => null_mut ( ) ,
117- Some ( mutex_alloc) => unsafe {
118- // SAFETY: We're allowed to call sqlite3_mutex_alloc with this bitmask:
119- // https://sqlite.org/c3ref/mutex_alloc.html
120- mutex_alloc ( SQLITE_MUTEX_FAST as i32 )
121- } ,
122- } ,
123- }
124- }
125- }
126-
127- unsafe impl RawMutex for SqliteMutex {
128- const INIT : Self = SqliteMutex { ptr : null_mut ( ) } ;
129-
130- type GuardMarker = GuardSend ;
131-
132- fn lock ( & self ) {
133- if self . ptr . is_null ( ) {
134- // Disable mutex code
135- } else {
136- unsafe {
137- // SAFETY: When we get here, we were able to allocate a mutex (so mutex methods
138- // must be present).
139- ( * SQLITE3_API ) . mutex_enter . unwrap_unchecked ( ) ( self . ptr )
140- }
141- }
142- }
143-
144- fn try_lock ( & self ) -> bool {
145- if self . ptr . is_null ( ) {
146- // Disable mutex code
147- true
148- } else {
149- let res = unsafe {
150- // SAFETY: When we get here, we were able to allocate a mutex (so mutex methods
151- // must be present).
152- ( * SQLITE3_API ) . mutex_try . unwrap_unchecked ( ) ( self . ptr )
153- } ;
154- res == 0
155- }
156- }
157-
158- unsafe fn unlock ( & self ) {
159- if self . ptr . is_null ( ) {
160- // Disable mutex code
161- } else {
162- unsafe {
163- // SAFETY: When we get here, we were able to allocate a mutex (so mutex methods
164- // must be present). Also, this method is only allowed to be called after a caller
165- // has locked the mutex before.
166- ( * SQLITE3_API ) . mutex_leave . unwrap_unchecked ( ) ( self . ptr )
167- }
168- }
169- }
170- }
171-
172- impl Drop for SqliteMutex {
173- fn drop ( & mut self ) {
174- if !self . ptr . is_null ( ) {
175- unsafe {
176- // SAFETY: The pointer points to a valid mutex we own. This means that we have been
177- // able to allocate a mutex, so mutex methods must be present.
178- ( * SQLITE3_API ) . mutex_free . unwrap_unchecked ( ) ( self . ptr )
179- } ;
180- }
181- }
182- }
183-
184- pub type Mutex < T > = MutexApi < SqliteMutex , T > ;
185-
186- /// Creates a [Mutex] implementation using `sqlite3_mutex_enter` and `sqlite3_mutex_free`.
187- ///
188- /// When SQLite has been compiled without mutexes, the returned mutex doesn't do anything.
189- pub fn sqlite3_mutex < T > ( value : T ) -> Mutex < T > {
190- let raw = SqliteMutex :: new ( ) ;
191- MutexApi :: from_raw ( raw, value)
192- }
193-
19492// Use getrandom crate to generate UUID.
19593// This is not available in all WASM builds - use the default in those cases.
19694#[ cfg( feature = "getrandom" ) ]
0 commit comments