@@ -17,19 +17,18 @@ use std::{
17
17
ptr:: { null, null_mut} ,
18
18
sync:: RwLock ,
19
19
} ;
20
+ use std:: sync:: atomic:: { AtomicPtr , Ordering } ;
20
21
21
- static GLOBAL_MODULE : Lazy < RwLock < Module > > = Lazy :: new ( Default :: default ) ;
22
+ static GLOBAL_MODULE : AtomicPtr < Module > = AtomicPtr :: new ( null_mut ( ) ) ;
22
23
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" ) )
27
27
}
28
28
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" ) )
33
32
}
34
33
35
34
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) {
84
83
display_ini_entries ( zend_module) ;
85
84
}
86
85
87
- #[ derive( Default ) ]
88
86
pub struct Module {
89
87
name : String ,
90
88
version : String ,
@@ -105,19 +103,18 @@ impl Module {
105
103
static STR_INI_ENTITIES : RefCell <HashMap <String , IniEntity <StrPtrBox >>> = Default :: default ( ) ;
106
104
}
107
105
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
+ }
121
118
}
122
119
123
120
pub fn on_module_init ( & mut self , func : impl Fn ( ModuleArgs ) -> bool + Send + Sync + ' static ) {
@@ -225,7 +222,7 @@ impl Module {
225
222
. insert ( name. to_string ( ) , unsafe { ClassEntity :: new ( name, class) } ) ;
226
223
}
227
224
228
- pub unsafe fn module_entry ( & mut self ) -> * const zend_module_entry {
225
+ pub unsafe fn module_entry ( mut self ) -> * const zend_module_entry {
229
226
assert ! ( !self . name. is_empty( ) , "module name must be set" ) ;
230
227
assert ! ( !self . version. is_empty( ) , "module version must be set" ) ;
231
228
@@ -261,7 +258,9 @@ impl Module {
261
258
build_id : PHP_MODULE_BUILD_ID ,
262
259
} ) ;
263
260
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
265
264
}
266
265
267
266
fn function_entries ( & self ) -> * const zend_function_entry {
0 commit comments