Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
Created December 11, 2023 17:47
Show Gist options
  • Save ClarkeRemy/4cb24f71d56e8205a598e595905fd70b to your computer and use it in GitHub Desktop.
Save ClarkeRemy/4cb24f71d56e8205a598e595905fd70b to your computer and use it in GitHub Desktop.
Thread Local Add
use core::cell::RefCell;
macro_rules! call {
( $FUNCTION:ident $ARGS:expr => $RETURN:ident) => {
unsafe { $FUNCTION::LOCAL.set(Funct { args: $ARGS, ret: &mut $RETURN }) }
$FUNCTION();
};
}
fn main() -> std::process::ExitCode
{
let mut val = 0;
call!(add(5,4)=>val);
std::println!("{}", val);
std::process::ExitCode::SUCCESS
}
pub mod add {
use super::*;
thread_local! {
pub static LOCAL : RefCell <Funct<(i32,i32),*mut i32>> = RefCell::new( Funct{args : (0,0), ret : core::ptr::null_mut()});
}
}
fn add() {
unsafe {
add::LOCAL.with_borrow_mut(|l| {
*l.ret = l.args.0 + l.args.1;
})
}
}
struct Funct<A, R> {
args: A,
ret: R,
}
struct AddArgs {
l: i32,
r: i32,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment