1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::sys;
#[cfg(not(feature = "no-std"))]
static RUNTIME: std::sync::Once = std::sync::Once::new();
pub fn release_lock() {
unsafe { sys::caml_enter_blocking_section() }
}
pub fn acquire_lock() {
unsafe { sys::caml_leave_blocking_section() }
}
pub fn locked<T, F: FnOnce() -> T>(f: F) -> T {
acquire_lock();
let x = f();
release_lock();
x
}
pub fn unlocked<T, F: FnOnce() -> T>(f: F) -> T {
release_lock();
let x = f();
acquire_lock();
x
}
#[cfg(not(feature = "no-std"))]
pub fn init() {
RUNTIME.call_once(|| {
let args = std::env::args()
.map(|arg| std::ffi::CString::new(arg).unwrap())
.collect::<Vec<std::ffi::CString>>();
let mut c_args = args
.iter()
.map(|arg| arg.as_ptr() as *const std::os::raw::c_char)
.collect::<Vec<*const std::os::raw::c_char>>();
c_args.push(std::ptr::null());
unsafe { crate::sys::caml_main(c_args.as_ptr()) }
})
}
pub fn shutdown() {
unsafe { crate::sys::caml_shutdown() }
}