1
1
2
2
use crate :: boxed:: ZBox ;
3
- use crate :: class:: { ClassMetadata , RegisteredClass } ;
4
3
use crate :: prelude:: PhpResult ;
5
- use crate :: props:: Property ;
6
- use crate :: types:: { ZendHashTable , ZendClassObject } ;
4
+ use crate :: types:: ZendHashTable ;
7
5
use crate :: zend:: Function ;
8
6
9
7
use std:: cell:: RefCell ;
10
- use std:: collections:: HashMap ;
11
8
use std:: fs:: File ;
12
9
use std:: io;
13
10
use std:: os:: fd:: { RawFd , FromRawFd } ;
@@ -31,13 +28,13 @@ fn sys_pipe() -> io::Result<(RawFd, RawFd)> {
31
28
Ok ( ( pipefd[ 0 ] , pipefd[ 1 ] ) )
32
29
}
33
30
34
- pub struct GlobalConnection {
31
+ pub struct EventLoop {
35
32
fibers : ZBox < ZendHashTable > ,
36
33
37
- _sender : Sender < u64 > ,
34
+ sender : Sender < u64 > ,
38
35
receiver : Receiver < u64 > ,
39
36
40
- _notify_sender : File ,
37
+ notify_sender : File ,
41
38
notify_receiver : File ,
42
39
43
40
get_current_suspension : Function ,
@@ -47,39 +44,33 @@ pub struct GlobalConnection {
47
44
dummy : [ u8 ; 1 ] ,
48
45
}
49
46
50
- impl GlobalConnection {
51
- pub fn getEventFd ( ) -> u64 {
52
- EVENTLOOP . with_borrow ( |c| {
53
- c. as_ref ( ) . unwrap ( ) . notify_receiver . as_raw_fd ( ) as u64
54
- } )
55
- }
56
- pub fn wakeup ( ) -> PhpResult < ( ) > {
57
- EVENTLOOP . with_borrow_mut ( |c| {
58
- c. as_mut ( ) . unwrap ( ) . wakeup_internal ( )
59
- } )
60
- }
61
- }
62
-
63
- impl GlobalConnection {
64
- fn new ( ) -> PhpResult < Self > {
47
+ impl EventLoop {
48
+ pub fn new ( ) -> PhpResult < Self > {
65
49
let ( sender, receiver) = channel ( ) ;
66
50
let ( notify_receiver, notify_sender) =
67
51
sys_pipe ( ) . map_err ( |err| format ! ( "Could not create pipe: {}" , err) ) ?;
68
52
53
+ call_user_func ! ( Function :: try_from_function( "class_exists" ) . unwrap( ) , "\\ Revolt\\ EventLoop" ) . unwrap ( ) ;
54
+ call_user_func ! ( Function :: try_from_function( "interface_exists" ) . unwrap( ) , "\\ Revolt\\ EventLoop\\ Suspension" ) . unwrap ( ) ;
55
+
69
56
Ok ( Self {
70
57
fibers : ZendHashTable :: new ( ) ,
71
- _sender : sender,
58
+ sender : sender,
72
59
receiver : receiver,
73
- _notify_sender : unsafe { File :: from_raw_fd ( notify_sender) } ,
60
+ notify_sender : unsafe { File :: from_raw_fd ( notify_sender) } ,
74
61
notify_receiver : unsafe { File :: from_raw_fd ( notify_receiver) } ,
75
62
dummy : [ 0 ; 1 ] ,
76
63
get_current_suspension : Function :: try_from_method ( "\\ Revolt\\ EventLoop" , "getSuspension" ) . unwrap ( ) ,
77
64
suspend : Function :: try_from_method ( "\\ Revolt\\ EventLoop\\ Suspension" , "suspend" ) . unwrap ( ) ,
78
65
resume : Function :: try_from_method ( "\\ Revolt\\ EventLoop\\ Suspension" , "resume" ) . unwrap ( )
79
66
} )
80
67
}
68
+
69
+ pub fn get_event_fd ( & self ) ->u64 {
70
+ self . notify_receiver . as_raw_fd ( ) as u64
71
+ }
81
72
82
- fn wakeup_internal ( & mut self ) -> PhpResult < ( ) > {
73
+ pub fn wakeup_internal ( & mut self ) -> PhpResult < ( ) > {
83
74
self . notify_receiver . read_exact ( & mut self . dummy ) . unwrap ( ) ;
84
75
85
76
for fiber_id in self . receiver . try_iter ( ) {
@@ -106,34 +97,15 @@ impl GlobalConnection {
106
97
} ) . unwrap ( ) ;
107
98
( )
108
99
}
109
- }
110
-
111
- class_derives ! ( GlobalConnection ) ;
112
-
113
- static EVENTLOOP_META : ClassMetadata < GlobalConnection > = ClassMetadata :: new ( ) ;
114
-
115
- impl RegisteredClass for GlobalConnection {
116
- const CLASS_NAME : & ' static str = "GlobalConnection" ;
117
100
118
- fn get_metadata ( ) -> & ' static ClassMetadata < Self > {
119
- & EVENTLOOP_META
101
+ pub fn get_sender ( & self ) -> Sender < u64 > {
102
+ self . sender . clone ( )
120
103
}
121
-
122
- fn get_properties < ' a > ( ) -> HashMap < & ' static str , Property < ' a , Self > > {
123
- HashMap :: new ( )
104
+ pub fn get_notify_sender ( & self ) -> File {
105
+ self . notify_sender . try_clone ( ) . unwrap ( )
124
106
}
125
107
}
126
108
127
109
thread_local ! {
128
- pub static EVENTLOOP : RefCell <Option <ZBox <ZendClassObject <GlobalConnection >>>> = RefCell :: new( None ) ;
129
- }
130
-
131
- pub extern "C" fn request_startup ( _type : i32 , _module_number : i32 ) -> i32 {
132
- EVENTLOOP . set ( Some ( ZendClassObject :: new ( GlobalConnection :: new ( ) . unwrap ( ) ) ) ) ;
133
- 0
110
+ pub static EVENTLOOP : RefCell <Option <EventLoop >> = RefCell :: new( None ) ;
134
111
}
135
-
136
- pub extern "C" fn request_shutdown ( _type : i32 , _module_number : i32 ) -> i32 {
137
- EVENTLOOP . set ( None ) ;
138
- 0
139
- }
0 commit comments