1
0

feat: fix LazyLock issue and add something in cdylib

This commit is contained in:
2026-05-11 13:33:01 +08:00
parent f0e610f8c8
commit 76ddddb6cd
4 changed files with 134 additions and 69 deletions

View File

@@ -12,6 +12,9 @@ use std::cell::RefCell;
use std::ffi::{CStr, CString, c_char};
use thiserror::Error as TeError;
/// The type representing the raw pointer to immutable C-style NUL-terminated string.
pub type CStyleString = *const c_char;
// region: Error
/// Error occurs in this crate.
@@ -31,13 +34,13 @@ type Result<T> = std::result::Result<T, Error>;
// endregion
// region: FFI String
// region: String Cache for Exposing
struct FfiString {
struct StringCache {
msg: CString,
}
impl FfiString {
impl StringCache {
fn new() -> Self {
Self {
msg: CString::new("").expect("empty string must be valid for CString"),
@@ -49,7 +52,7 @@ impl FfiString {
Ok(())
}
pub fn get_msg(&self) -> *const c_char {
pub fn get_msg(&self) -> CStyleString {
self.msg.as_ptr()
}
@@ -63,7 +66,7 @@ impl FfiString {
// region: Exposed Functions
thread_local! {
static STRING_CACHE: RefCell<FfiString> = RefCell::new(FfiString::new());
static STRING_CACHE: RefCell<StringCache> = RefCell::new(StringCache::new());
}
/// Set thread local string exposed for C code.
@@ -74,7 +77,7 @@ pub fn set_ffi_string(msg: &str) -> Result<()> {
}
/// Get const pointer to thread local string exposed for C code.
pub fn get_ffi_string() -> *const c_char {
pub fn get_ffi_string() -> CStyleString {
STRING_CACHE.with(|e| e.borrow().get_msg())
}
@@ -88,7 +91,7 @@ pub fn clear_ffi_string() {
}
/// Parse string given by C code into Rust string.
pub fn parse_ffi_string<'a>(ptr: *const c_char) -> Result<&'a str> {
pub fn parse_ffi_string<'a>(ptr: CStyleString) -> Result<&'a str> {
if ptr.is_null() {
Err(Error::NullPtr)
} else {

View File

@@ -3,11 +3,11 @@ mod last_error;
mod object_pool;
use object_pool::ObjectPool;
use std::ffi::{CString, c_char};
use std::sync::{LazyLock, PoisonError, RwLock};
use std::sync::{LazyLock, RwLock};
use thiserror::Error as TeError;
use wfassoc::highlevel::{Program, Schema};
pub use cstr_ffi::CStyleString;
pub use object_pool::Token;
// region: Error
@@ -44,6 +44,36 @@ type Result<T> = std::result::Result<T, Error>;
// region: Macros
macro_rules! in_param_ty {
($t:ty) => {
$t
};
}
macro_rules! out_param_ty {
($t:ty) => {
*mut $t
};
}
macro_rules! set_out_param {
($lhs:expr, $rhs:expr) => {
unsafe { *$lhs = $rhs };
};
}
macro_rules! pull_reader {
($pool:expr) => {
$pool.read().map_err(|_| Error::PoisonRwLock)
};
}
macro_rules! pull_writer {
($pool:expr) => {
$pool.write().map_err(|_| Error::PoisonRwLock)
};
}
// endregion
// region: Exposed Functions
@@ -52,16 +82,18 @@ type Result<T> = std::result::Result<T, Error>;
#[unsafe(no_mangle)]
pub extern "C" fn WFStartup() -> bool {
// TODO: Initialize all pool by fetching writer from them
true
}
#[unsafe(no_mangle)]
pub extern "C" fn WFShutdown() -> bool {
// TODO: Free all pool stored objects
true
}
#[unsafe(no_mangle)]
pub extern "C" fn WFGetLastError() -> *const c_char {
pub extern "C" fn WFGetLastError() -> CStyleString {
last_error::get_last_error()
}
@@ -70,32 +102,24 @@ pub extern "C" fn WFHasPrivilege() -> bool {
wfassoc::win32::utilities::has_privilege()
}
#[unsafe(no_mangle)]
pub extern "C" fn WFAdd(left: u32, right: u32, rv: *mut u32) -> bool {
unsafe {
*rv = left + right;
}
return true;
}
// endregion
// region: Schema
const SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaCreate(out_token: *mut Token) -> bool {
pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
fn inner() -> Result<Token> {
let binding = &SCHEMA_POOL;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
let mut pool = pull_writer!(SCHEMA_POOL)?;
Ok(pool.allocate(Schema::new())?)
}
match inner() {
Ok(rv) => {
unsafe { *out_token = rv };
set_out_param!(out_schema, rv);
last_error::clear_last_error();
true
}
Err(e) => {
@@ -105,23 +129,48 @@ pub extern "C" fn WFSchemaCreate(out_token: *mut Token) -> bool {
}
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaSetIdentifier(
in_schema: in_param_ty!(Token),
in_value: in_param_ty!(CStyleString),
) -> bool {
fn inner(in_schema: Token, in_value: CStyleString) -> Result<()> {
let mut pool = pull_writer!(SCHEMA_POOL)?;
let schema = pool.get_mut(in_schema)?;
schema.set_identifier(cstr_ffi::parse_ffi_string(in_value)?);
Ok(())
}
match inner(in_schema, in_value) {
Ok(_) => {
last_error::clear_last_error();
true
}
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaIntoProgram(in_token: Token, out_token: *mut Token) -> bool {
pub extern "C" fn WFSchemaIntoProgram(
in_schema: in_param_ty!(Token),
out_program: out_param_ty!(Token),
) -> bool {
fn inner(in_token: Token) -> Result<Token> {
let binding = &SCHEMA_POOL;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
let mut pool = pull_writer!(SCHEMA_POOL)?;
let schema = pool.pop(in_token)?;
let binding = &PROGRAM_POOL;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = schema.into_program()?;
Ok(pool.allocate(program)?)
}
match inner(in_token) {
match inner(in_schema) {
Ok(rv) => {
unsafe { *out_token = rv };
set_out_param!(out_program, rv);
last_error::clear_last_error();
true
}
Err(e) => {
@@ -132,15 +181,17 @@ pub extern "C" fn WFSchemaIntoProgram(in_token: Token, out_token: *mut Token) ->
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_token: Token) -> bool {
pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
fn inner(in_token: Token) -> Result<()> {
let binding = &SCHEMA_POOL;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
let mut pool = pull_writer!(SCHEMA_POOL)?;
Ok(pool.free(in_token)?)
}
match inner(in_token) {
Ok(_) => true,
match inner(in_schema) {
Ok(_) => {
last_error::clear_last_error();
true
}
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
@@ -152,7 +203,7 @@ pub extern "C" fn WFSchemaDestroy(in_token: Token) -> bool {
// region: Program
const PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
// endregion