Skip to content

Commit 52770c3

Browse files
committed
Modify GLOBAL_MODULE type.
1 parent 60658ff commit 52770c3

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

examples/hello/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ fn throw_exception(_: &mut [Val]) -> phper::Result<()> {
2323
}
2424

2525
#[php_get_module]
26-
pub fn get_module(module: &mut Module) {
27-
// set module metadata
28-
module.set_name(env!("CARGO_PKG_NAME"));
29-
module.set_version(env!("CARGO_PKG_VERSION"));
30-
module.set_author(env!("CARGO_PKG_AUTHORS"));
26+
pub fn get_module() -> Module {
27+
let mut module = Module::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
3128

3229
// register module ini
3330
module.add_bool_ini("hello.enable", false, Policy::All);
@@ -81,4 +78,6 @@ pub fn get_module(module: &mut Module) {
8178
vec![Argument::by_val("foo")],
8279
);
8380
module.add_class("FooClass", foo_class);
81+
82+
module
8483
}

examples/log/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ use phper::{
44
};
55

66
#[php_get_module]
7-
pub fn get_module(module: &mut Module) {
8-
// set module metadata
9-
module.set_name(env!("CARGO_PKG_NAME"));
10-
module.set_version(env!("CARGO_PKG_VERSION"));
11-
module.set_author(env!("CARGO_PKG_AUTHORS"));
7+
pub fn get_module() -> Module {
8+
let mut module = Module::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"));
129

1310
module.add_function(
1411
"log_say",
@@ -54,4 +51,6 @@ pub fn get_module(module: &mut Module) {
5451
},
5552
vec![Argument::by_val("message")],
5653
);
54+
55+
module
5756
}

phper-macros/src/inner.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,8 @@ pub(crate) fn php_get_module(_attr: TokenStream, input: TokenStream) -> TokenStr
2727
fn internal(#inputs) #ret {
2828
#body
2929
}
30-
let internal: fn(module: &mut ::phper::modules::Module) = internal;
31-
32-
::phper::modules::write_global_module(internal);
33-
unsafe {
34-
::phper::modules::write_global_module(|module| {
35-
module.module_entry()
36-
})
37-
}
30+
let internal: fn() -> ::phper::modules::Module = internal;
31+
unsafe { internal().module_entry() }
3832
}
3933
};
4034

phper/src/modules.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ use std::{
1717
ptr::{null, null_mut},
1818
sync::RwLock,
1919
};
20+
use std::sync::atomic::{AtomicPtr, Ordering};
2021

21-
static GLOBAL_MODULE: Lazy<RwLock<Module>> = Lazy::new(Default::default);
22+
static GLOBAL_MODULE: AtomicPtr<Module> = AtomicPtr::new(null_mut());
2223

23-
#[doc(hidden)]
24-
pub fn read_global_module<R>(f: impl FnOnce(&Module) -> R) -> R {
25-
let module = (&*GLOBAL_MODULE).read().expect("get write lock failed");
26-
f(&module)
24+
pub(crate) fn read_global_module<R>(f: impl FnOnce(&Module) -> R) -> R {
25+
let module = GLOBAL_MODULE.load(Ordering::SeqCst);
26+
f(unsafe { module.as_ref() }.expect("GLOBAL_MODULE is null"))
2727
}
2828

29-
#[doc(hidden)]
30-
pub fn write_global_module<R>(f: impl FnOnce(&mut Module) -> R) -> R {
31-
let mut module = (&*GLOBAL_MODULE).write().expect("get write lock failed");
32-
f(&mut module)
29+
pub(crate) fn write_global_module<R>(f: impl FnOnce(&mut Module) -> R) -> R {
30+
let module = GLOBAL_MODULE.load(Ordering::SeqCst);
31+
f(unsafe { module.as_mut() }.expect("GLOBAL_MODULE is null"))
3332
}
3433

3534
unsafe extern "C" fn module_startup(r#type: c_int, module_number: c_int) -> c_int {
@@ -84,7 +83,6 @@ unsafe extern "C" fn module_info(zend_module: *mut zend_module_entry) {
8483
display_ini_entries(zend_module);
8584
}
8685

87-
#[derive(Default)]
8886
pub struct Module {
8987
name: String,
9088
version: String,
@@ -105,19 +103,18 @@ impl Module {
105103
static STR_INI_ENTITIES: RefCell<HashMap<String, IniEntity<StrPtrBox>>> = Default::default();
106104
}
107105

108-
pub fn set_name(&mut self, name: impl ToString) {
109-
let name = ensure_end_with_zero(name);
110-
self.name = name;
111-
}
112-
113-
pub fn set_version(&mut self, version: impl ToString) {
114-
let version = ensure_end_with_zero(version);
115-
self.version = version;
116-
}
117-
118-
pub fn set_author(&mut self, author: impl ToString) {
119-
let author = ensure_end_with_zero(author);
120-
self.author = author;
106+
pub fn new(name: impl ToString, version: impl ToString, author: impl ToString) -> Self {
107+
Self {
108+
name: ensure_end_with_zero(name),
109+
version: ensure_end_with_zero(version),
110+
author: ensure_end_with_zero(author),
111+
module_init: None,
112+
module_shutdown: None,
113+
request_init: None,
114+
request_shutdown: None,
115+
function_entities: vec![],
116+
class_entities: Default::default(),
117+
}
121118
}
122119

123120
pub fn on_module_init(&mut self, func: impl Fn(ModuleArgs) -> bool + Send + Sync + 'static) {
@@ -225,7 +222,7 @@ impl Module {
225222
.insert(name.to_string(), unsafe { ClassEntity::new(name, class) });
226223
}
227224

228-
pub unsafe fn module_entry(&mut self) -> *const zend_module_entry {
225+
pub unsafe fn module_entry(mut self) -> *const zend_module_entry {
229226
assert!(!self.name.is_empty(), "module name must be set");
230227
assert!(!self.version.is_empty(), "module version must be set");
231228

@@ -261,7 +258,9 @@ impl Module {
261258
build_id: PHP_MODULE_BUILD_ID,
262259
});
263260

264-
Box::into_raw(entry)
261+
let entry = Box::into_raw(entry);
262+
GLOBAL_MODULE.store(Box::into_raw(Box::new(self)), Ordering::SeqCst);
263+
entry
265264
}
266265

267266
fn function_entries(&self) -> *const zend_function_entry {

0 commit comments

Comments
 (0)