Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(capi): use RwLock to replace mut static reference #663

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified capi/data/mini.dat
Binary file not shown.
40 changes: 12 additions & 28 deletions capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
mem,
ptr::{null, null_mut},
slice, str,
sync::OnceLock,
sync::RwLock,
};

use chewing::{
Expand Down Expand Up @@ -46,25 +46,19 @@
CUShortSlice(usize),
}

static mut OWNED: OnceLock<BTreeMap<*mut c_void, Owned>> = OnceLock::new();
static OWNED: RwLock<BTreeMap<usize, Owned>> = RwLock::new(BTreeMap::new());

fn owned_into_raw<T>(owned: Owned, ptr: *mut T) -> *mut T {
unsafe {
if OWNED.get().is_none() {
let _ = OWNED.set(BTreeMap::new());
}
}
let void_ptr: *mut c_void = ptr.cast();
match unsafe { OWNED.get_mut() } {
Some(map) => {
map.insert(void_ptr, owned);
match OWNED.write() {
Ok(mut map) => {
map.insert(ptr as usize, owned);
ptr
}
None => null_mut(),
Err(_) => null_mut(),

Check warning on line 57 in capi/src/io.rs

View check run for this annotation

Codecov / codecov/patch

capi/src/io.rs#L57

Added line #L57 was not covered by tests
}
}

static mut EMPTY_STRING_BUFFER: [u8; 1] = [0; 1];
static EMPTY_STRING_BUFFER: [u8; 1] = [0; 1];

fn copy_cstr(buf: &mut [u8], buffer: &str) -> *const c_char {
let n = min(buf.len(), buffer.len());
Expand All @@ -73,8 +67,8 @@
buf.as_ptr().cast()
}

fn global_empty_cstr() -> *mut c_char {
unsafe { EMPTY_STRING_BUFFER.as_mut_ptr().cast() }
fn global_empty_cstr() -> *const c_char {
EMPTY_STRING_BUFFER.as_ptr().cast()
}

unsafe fn slice_from_ptr_with_nul<'a>(ptr: *const c_char) -> Option<&'a [c_char]> {
Expand Down Expand Up @@ -110,11 +104,6 @@
logger: Option<unsafe extern "C" fn(data: *mut c_void, level: c_int, fmt: *const c_char, ...)>,
loggerdata: *mut c_void,
) -> *mut ChewingContext {
unsafe {
if OWNED.get().is_none() {
let _ = OWNED.set(BTreeMap::new());
}
}
LOGGER.init();
let _ = log::set_logger(&LOGGER);
log::set_max_level(log::LevelFilter::Trace);
Expand Down Expand Up @@ -220,11 +209,6 @@
#[no_mangle]
pub unsafe extern "C" fn chewing_delete(ctx: *mut ChewingContext) {
if !ctx.is_null() {
unsafe {
if OWNED.get().is_none() {
let _ = OWNED.take();
}
}
LOGGER.set(None);
info!("Destroying context {ctx:?}");
drop(unsafe { Box::from_raw(ctx) })
Expand All @@ -237,8 +221,8 @@
#[no_mangle]
pub unsafe extern "C" fn chewing_free(ptr: *mut c_void) {
if !ptr.is_null() {
if let Some(map) = unsafe { OWNED.get() } {
if let Some(owned) = map.get(&ptr) {
if let Ok(map) = OWNED.write() {
if let Some(owned) = map.get(&(ptr as usize)) {
match owned {
Owned::CString => drop(unsafe { CString::from_raw(ptr.cast()) }),
Owned::CUShortSlice(len) => {
Expand Down Expand Up @@ -437,7 +421,7 @@
options.esc_clear_all_buffer = value > 0;
}
"chewing.auto_commit_threshold" => {
if value < 0 || value > 39 {
if !(0..=39).contains(&value) {
return ERROR;
}
options.auto_commit_threshold = value as usize;
Expand Down
Loading