#shared-state #shared #module #state #dylib #cdylib

crossdylib

Cross-platform shared state across shared libraries/modules

7 stable releases

3.0.0 Dec 4, 2021
2.1.0 Nov 29, 2021
2.0.0 Nov 28, 2021
1.1.0 Nov 28, 2021
1.0.2 Nov 28, 2021

#338 in FFI

MIT license

11KB
154 lines

crossdylib

This library can be used to achieve shared state across shared libraries/modules.

Support

Supported platforms

  • Windows
  • Linux

Example

a.dll

#[macro_use] extern crate crossdylib;

crossdylib! {
	static THE_ANSWER: std::sync::Mutex<u32> = std::sync::Mutex::new(39);
}

#[no_mangle]
pub unsafe extern "C" fn increment() {
	THE_ANSWER.sync().unwrap();

	let mut lock = THE_ANSWER.lock().unwrap();
	*lock += 1;
	assert_eq!(*lock, 40);
}

b.dll

#[macro_use] extern crate crossdylib;

crossdylib! {
	static THE_ANSWER: std::sync::Mutex<u32> = std::sync::Mutex::new(39);
}

#[no_mangle]
pub unsafe extern "C" fn increment() {
	THE_ANSWER.sync().unwrap();

	let mut lock = THE_ANSWER.lock().unwrap();
	*lock += 1;
	assert_eq!(*lock, 41);
}

main.exe

fn main() {
	let a = Library::new("a.dll").unwrap();
	a.get::<extern "C" fn()>("increment").unwrap()();

	let b = Library::new("b.dll").unwrap();
	b.get::<extern "C" fn()>("increment").unwrap()();

	println!("Success");
}

Dependencies

~0.5–7MB
~26K SLoC