Merge 'core/vtab: Use AtomicPtr for table_ptr' from Pekka Enberg

Make it Send.

Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>

Closes #3169
This commit is contained in:
Pekka Enberg
2025-09-17 12:13:18 +03:00
committed by GitHub

View File

@@ -6,6 +6,7 @@ use crate::{Connection, LimboError, SymbolTable, Value};
use std::cell::RefCell;
use std::ffi::c_void;
use std::ptr::NonNull;
use std::sync::atomic::AtomicPtr;
use std::sync::Arc;
use turso_ext::{ConstraintInfo, IndexInfo, OrderByInfo, ResultCode, VTabKind, VTabModuleImpl};
use turso_parser::{ast, parser::Parser};
@@ -214,10 +215,19 @@ impl VirtualTableCursor {
}
}
#[derive(Clone, Debug)]
#[derive(Debug)]
pub(crate) struct ExtVirtualTable {
implementation: Arc<VTabModuleImpl>,
table_ptr: *const c_void,
table_ptr: AtomicPtr<c_void>,
}
impl Clone for ExtVirtualTable {
fn clone(&self) -> Self {
Self {
implementation: self.implementation.clone(),
table_ptr: AtomicPtr::new(self.table_ptr.load(std::sync::atomic::Ordering::Relaxed)),
}
}
}
impl ExtVirtualTable {
@@ -261,7 +271,7 @@ impl ExtVirtualTable {
let (schema, table_ptr) = module.implementation.create(args)?;
let vtab = ExtVirtualTable {
implementation: module.implementation.clone(),
table_ptr,
table_ptr: AtomicPtr::new(table_ptr as *mut c_void),
};
Ok((vtab, schema))
}
@@ -280,7 +290,10 @@ impl ExtVirtualTable {
let ext_conn_ptr = NonNull::new(Box::into_raw(Box::new(conn))).expect("null pointer");
// store the leaked connection pointer on the table so it can be freed on drop
let Some(cursor) = NonNull::new(unsafe {
(self.implementation.open)(self.table_ptr, ext_conn_ptr.as_ptr()) as *mut c_void
(self.implementation.open)(
self.table_ptr.load(std::sync::atomic::Ordering::Relaxed) as *const c_void,
ext_conn_ptr.as_ptr(),
) as *mut c_void
}) else {
return Err(LimboError::ExtensionError("Open returned null".to_string()));
};
@@ -293,7 +306,7 @@ impl ExtVirtualTable {
let newrowid = 0i64;
let rc = unsafe {
(self.implementation.update)(
self.table_ptr,
self.table_ptr.load(std::sync::atomic::Ordering::Relaxed) as *const c_void,
arg_count as i32,
ext_args.as_ptr(),
&newrowid as *const _ as *mut i64,
@@ -312,7 +325,11 @@ impl ExtVirtualTable {
}
fn destroy(&self) -> crate::Result<()> {
let rc = unsafe { (self.implementation.destroy)(self.table_ptr) };
let rc = unsafe {
(self.implementation.destroy)(
self.table_ptr.load(std::sync::atomic::Ordering::Relaxed) as *const c_void
)
};
match rc {
ResultCode::OK => Ok(()),
_ => Err(LimboError::ExtensionError(rc.to_string())),