Skip to content

Commit cc2e561

Browse files
committed
Remove more atomcis
1 parent 505a202 commit cc2e561

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

crates/core/src/crud_vtab.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use alloc::boxed::Box;
44
use alloc::rc::Rc;
55
use const_format::formatcp;
66
use core::ffi::{CStr, c_char, c_int, c_void};
7-
use core::sync::atomic::Ordering;
87
use serde::Serialize;
98
use serde_json::value::RawValue;
109

@@ -88,7 +87,7 @@ impl VirtualTable {
8887
.ok_or_else(|| PowerSyncError::state_error("Not in tx"))?;
8988
let db = self.db;
9089

91-
if self.state.is_in_sync_local.load(Ordering::Relaxed) {
90+
if self.state.is_in_sync_local.get() {
9291
// Don't collect CRUD writes while we're syncing the local database - writes made here
9392
// aren't writes we should upload.
9493
// This normally doesn't happen because we insert directly into the data tables, but

crates/core/src/state.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use core::{
2-
cell::{Ref, RefCell},
2+
cell::{Cell, Ref, RefCell},
33
ffi::{c_int, c_void},
4-
sync::atomic::{AtomicBool, Ordering},
54
};
65

76
use alloc::{
@@ -21,7 +20,7 @@ use crate::schema::Schema;
2120
/// functions/vtabs that need access to it.
2221
#[derive(Default)]
2322
pub struct DatabaseState {
24-
pub is_in_sync_local: AtomicBool,
23+
pub is_in_sync_local: Cell<bool>,
2524
schema: RefCell<Option<Schema>>,
2625
pending_updates: RefCell<BTreeSet<String>>,
2726
commited_updates: RefCell<BTreeSet<String>>,
@@ -47,15 +46,15 @@ impl DatabaseState {
4746
}
4847

4948
pub fn sync_local_guard<'a>(&'a self) -> impl Drop + use<'a> {
50-
self.is_in_sync_local
51-
.compare_exchange(false, true, Ordering::Acquire, Ordering::Acquire)
52-
.expect("should not be syncing already");
49+
if self.is_in_sync_local.replace(true) {
50+
panic!("Should ont be syncing already");
51+
}
5352

5453
struct ClearOnDrop<'a>(&'a DatabaseState);
5554

5655
impl Drop for ClearOnDrop<'_> {
5756
fn drop(&mut self) {
58-
self.0.is_in_sync_local.store(false, Ordering::Release);
57+
self.0.is_in_sync_local.set(false);
5958
}
6059
}
6160

@@ -129,11 +128,7 @@ pub fn register(db: *mut sqlite::sqlite3, state: Rc<DatabaseState>) -> Result<()
129128
) {
130129
let data = unsafe { DatabaseState::from_context(&ctx) };
131130

132-
ctx.result_int(if data.is_in_sync_local.load(Ordering::Relaxed) {
133-
1
134-
} else {
135-
0
136-
});
131+
ctx.result_int(if data.is_in_sync_local.get() { 1 } else { 0 });
137132
}
138133

139134
db.create_function_v2(

crates/core/src/update_hooks.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::{
2+
cell::Cell,
23
ffi::{CStr, c_char, c_int, c_void},
34
ptr::null_mut,
4-
sync::atomic::{AtomicBool, Ordering},
55
};
66

77
use alloc::{boxed::Box, rc::Rc};
@@ -22,7 +22,7 @@ use crate::{constants::SUBTYPE_JSON, error::PowerSyncError, state::DatabaseState
2222
/// closed and the function is unregistered.
2323
pub fn register(db: *mut sqlite::sqlite3, state: Rc<DatabaseState>) -> Result<(), ResultCode> {
2424
let state = Box::new(HookState {
25-
has_registered_hooks: AtomicBool::new(false),
25+
has_registered_hooks: Cell::new(false),
2626
db,
2727
state,
2828
});
@@ -41,15 +41,15 @@ pub fn register(db: *mut sqlite::sqlite3, state: Rc<DatabaseState>) -> Result<()
4141
}
4242

4343
struct HookState {
44-
has_registered_hooks: AtomicBool,
44+
has_registered_hooks: Cell<bool>,
4545
db: *mut sqlite::sqlite3,
4646
state: Rc<DatabaseState>,
4747
}
4848

4949
extern "C" fn destroy_function(ctx: *mut c_void) {
5050
let state = unsafe { Box::from_raw(ctx as *mut HookState) };
5151

52-
if state.has_registered_hooks.load(Ordering::Relaxed) {
52+
if state.has_registered_hooks.get() {
5353
check_previous(
5454
"update",
5555
&state.state,
@@ -107,7 +107,7 @@ extern "C" fn powersync_update_hooks(
107107
Rc::into_raw(db_state.clone()) as *mut c_void,
108108
),
109109
);
110-
state.has_registered_hooks.store(true, Ordering::Relaxed);
110+
state.has_registered_hooks.set(true);
111111
}
112112
"get" => {
113113
let state = unsafe { user_data.as_ref().unwrap_unchecked() };

0 commit comments

Comments
 (0)