Skip to content

Commit ab55a49

Browse files
committed
Cleanup
1 parent ee5c3e1 commit ab55a49

File tree

4 files changed

+29
-58
lines changed

4 files changed

+29
-58
lines changed

crates/macros/src/method.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,18 @@ pub fn parser(
198198
let c = c.as_mut().unwrap();
199199
let idx = c.prepare_resume();
200200

201-
let sender = c._sender.clone();
202-
let mut notifier = c._notify_sender.try_clone().unwrap();
201+
let sender = c.get_sender();
202+
let mut notifier = c.get_notify_sender();
203203

204-
let res = ::ext_php_rs::zend::RUNTIME.spawn(async move {
204+
::ext_php_rs::zend::RUNTIME.spawn(async move {
205205
let res = future.await;
206206
sender.send(idx).unwrap();
207207
::std::io::Write::write_all(&mut notifier, &[0]).unwrap();
208208
res
209209
})
210210
});
211211

212-
::ext_php_rs::zend::EVENTLOOP.suspend();
212+
::ext_php_rs::zend::EventLoop::suspend();
213213

214214
return ::ext_php_rs::zend::RUNTIME
215215
.block_on(future).unwrap();

src/builders/module.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
error::Result,
33
ffi::{ext_php_rs_php_build_id, ZEND_MODULE_API_NO},
4-
zend::{FunctionEntry, ModuleEntry, request_shutdown, request_startup},
4+
zend::{FunctionEntry, ModuleEntry},
55
PHP_DEBUG, PHP_ZTS,
66
};
77

@@ -64,8 +64,8 @@ impl ModuleBuilder {
6464
functions: ptr::null(),
6565
module_startup_func: None,
6666
module_shutdown_func: None,
67-
request_startup_func: Some(request_startup),
68-
request_shutdown_func: Some(request_shutdown),
67+
request_startup_func: None,
68+
request_shutdown_func: None,
6969
info_func: None,
7070
version: ptr::null(),
7171
globals_size: 0,

src/zend/fibers.rs

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11

22
use crate::boxed::ZBox;
3-
use crate::class::{ClassMetadata, RegisteredClass};
43
use crate::prelude::PhpResult;
5-
use crate::props::Property;
6-
use crate::types::{ZendHashTable, ZendClassObject};
4+
use crate::types::ZendHashTable;
75
use crate::zend::Function;
86

97
use std::cell::RefCell;
10-
use std::collections::HashMap;
118
use std::fs::File;
129
use std::io;
1310
use std::os::fd::{RawFd, FromRawFd};
@@ -31,13 +28,13 @@ fn sys_pipe() -> io::Result<(RawFd, RawFd)> {
3128
Ok((pipefd[0], pipefd[1]))
3229
}
3330

34-
pub struct GlobalConnection {
31+
pub struct EventLoop {
3532
fibers: ZBox<ZendHashTable>,
3633

37-
_sender: Sender<u64>,
34+
sender: Sender<u64>,
3835
receiver: Receiver<u64>,
3936

40-
_notify_sender: File,
37+
notify_sender: File,
4138
notify_receiver: File,
4239

4340
get_current_suspension: Function,
@@ -47,39 +44,33 @@ pub struct GlobalConnection {
4744
dummy: [u8; 1],
4845
}
4946

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> {
6549
let (sender, receiver) = channel();
6650
let (notify_receiver, notify_sender) =
6751
sys_pipe().map_err(|err| format!("Could not create pipe: {}", err))?;
6852

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+
6956
Ok(Self {
7057
fibers: ZendHashTable::new(),
71-
_sender: sender,
58+
sender: sender,
7259
receiver: receiver,
73-
_notify_sender: unsafe { File::from_raw_fd(notify_sender) },
60+
notify_sender: unsafe { File::from_raw_fd(notify_sender) },
7461
notify_receiver: unsafe { File::from_raw_fd(notify_receiver) },
7562
dummy: [0; 1],
7663
get_current_suspension: Function::try_from_method("\\Revolt\\EventLoop", "getSuspension").unwrap(),
7764
suspend: Function::try_from_method("\\Revolt\\EventLoop\\Suspension", "suspend").unwrap(),
7865
resume: Function::try_from_method("\\Revolt\\EventLoop\\Suspension", "resume").unwrap()
7966
})
8067
}
68+
69+
pub fn get_event_fd(&self)->u64 {
70+
self.notify_receiver.as_raw_fd() as u64
71+
}
8172

82-
fn wakeup_internal(&mut self) -> PhpResult<()> {
73+
pub fn wakeup_internal(&mut self) -> PhpResult<()> {
8374
self.notify_receiver.read_exact(&mut self.dummy).unwrap();
8475

8576
for fiber_id in self.receiver.try_iter() {
@@ -106,34 +97,15 @@ impl GlobalConnection {
10697
}).unwrap();
10798
()
10899
}
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";
117100

118-
fn get_metadata() -> &'static ClassMetadata<Self> {
119-
&EVENTLOOP_META
101+
pub fn get_sender(&self) -> Sender<u64> {
102+
self.sender.clone()
120103
}
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()
124106
}
125107
}
126108

127109
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);
134111
}
135-
136-
pub extern "C" fn request_shutdown(_type: i32, _module_number: i32) -> i32 {
137-
EVENTLOOP.set(None);
138-
0
139-
}

src/zend/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ pub use function::Function;
2222
pub use globals::ExecutorGlobals;
2323
pub use fibers::RUNTIME;
2424
pub use fibers::EVENTLOOP;
25-
pub use fibers::request_shutdown;
26-
pub use fibers::request_startup;
25+
pub use fibers::EventLoop;
2726
pub use borrow_unchecked::borrow_unchecked;
2827
pub use borrow_unchecked::BorrowUnchecked;
2928
pub use handlers::ZendObjectHandlers;

0 commit comments

Comments
 (0)