Skip to content

Commit ba5464b

Browse files
committed
Refacotr global module read and write api.
1 parent b9aa4f2 commit ba5464b

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

examples/hello/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn test_func() {}
137137

138138
#[no_mangle]
139139
pub extern "C" fn get_module() -> *const ::phper::sys::zend_module_entry {
140-
let f = |module: &mut Module| {
140+
write_global_module(|module| {
141141
module.set_name(env!("CARGO_PKG_NAME"));
142142
module.set_version(env!("CARGO_PKG_VERSION"));
143143

@@ -166,9 +166,11 @@ pub extern "C" fn get_module() -> *const ::phper::sys::zend_module_entry {
166166
println!("hello test1");
167167
});
168168
module.add_class("Test1", std_class);
169-
};
169+
});
170170

171-
f(&mut *write_global_module());
172-
173-
unsafe { read_global_module().module_entry() }
171+
unsafe {
172+
read_global_module(|module| {
173+
module.module_entry()
174+
})
175+
}
174176
}

phper/src/modules.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,53 +26,58 @@ use std::thread::LocalKey;
2626

2727
static GLOBAL_MODULE: Lazy<RwLock<Module>> = Lazy::new(Default::default);
2828

29-
pub fn read_global_module() -> RwLockReadGuard<'static, Module> {
30-
(&*GLOBAL_MODULE).read().expect("get write lock failed")
29+
pub fn read_global_module<R>(f: impl FnOnce(&Module) -> R) -> R {
30+
let module = (&*GLOBAL_MODULE).read().expect("get write lock failed");
31+
f(&module)
3132
}
3233

33-
pub fn write_global_module() -> RwLockWriteGuard<'static, Module> {
34-
(&*GLOBAL_MODULE).write().expect("get write lock failed")
34+
pub fn write_global_module<R>(f: impl FnOnce(&mut Module) -> R) -> R {
35+
let mut module = (&*GLOBAL_MODULE).write().expect("get write lock failed");
36+
f(&mut module)
3537
}
3638

3739
unsafe extern "C" fn module_startup(r#type: c_int, module_number: c_int) -> c_int {
3840
let args = ModuleArgs::new(r#type, module_number);
39-
{
40-
args.register_ini_entries(read_global_module().ini_entries());
41-
}
42-
{
43-
for class_entity in &read_global_module().class_entities {
41+
read_global_module(|module| {
42+
args.register_ini_entries(module.ini_entries());
43+
for class_entity in &module.class_entities {
4444
class_entity.init();
4545
class_entity.declare_properties();
4646
}
47-
}
48-
match &read_global_module().module_init {
49-
Some(f) => f(args) as c_int,
50-
None => 1,
51-
}
47+
match &module.module_init {
48+
Some(f) => f(args) as c_int,
49+
None => 1,
50+
}
51+
})
5252
}
5353

5454
unsafe extern "C" fn module_shutdown(r#type: c_int, module_number: c_int) -> c_int {
5555
let args = ModuleArgs::new(r#type, module_number);
5656
args.unregister_ini_entries();
57-
58-
match &read_global_module().module_shutdown {
59-
Some(f) => f(args) as c_int,
60-
None => 1,
61-
}
57+
read_global_module(|module| {
58+
match &module.module_shutdown {
59+
Some(f) => f(args) as c_int,
60+
None => 1,
61+
}
62+
})
6263
}
6364

6465
unsafe extern "C" fn request_startup(r#type: c_int, request_number: c_int) -> c_int {
65-
match &read_global_module().request_init {
66-
Some(f) => f(ModuleArgs::new(r#type, request_number)) as c_int,
67-
None => 1,
68-
}
66+
read_global_module(|module| {
67+
match &module.request_init {
68+
Some(f) => f(ModuleArgs::new(r#type, request_number)) as c_int,
69+
None => 1,
70+
}
71+
})
6972
}
7073

7174
unsafe extern "C" fn request_shutdown(r#type: c_int, request_number: c_int) -> c_int {
72-
match &read_global_module().request_shutdown {
73-
Some(f) => f(ModuleArgs::new(r#type, request_number)) as c_int,
74-
None => 1,
75-
}
75+
read_global_module(|module| {
76+
match &module.request_shutdown {
77+
Some(f) => f(ModuleArgs::new(r#type, request_number)) as c_int,
78+
None => 1,
79+
}
80+
})
7681
}
7782

7883
#[derive(Default)]

0 commit comments

Comments
 (0)