Skip to content

Commit 6657223

Browse files
committed
Further cleanup
1 parent c517931 commit 6657223

File tree

3 files changed

+57
-48
lines changed

3 files changed

+57
-48
lines changed

crates/macros/src/method.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,8 @@ pub fn parser(
192192
input.block = parse_quote! {{
193193
#this
194194
#hack_tokens
195-
let future = async move #stmts;
196195

197-
let future = ::ext_php_rs::zend::EVENTLOOP.with_borrow_mut(move |c| {
198-
let c = c.as_mut().unwrap();
199-
let idx = c.prepare_resume();
200-
201-
let sender = c.get_sender();
202-
let mut notifier = c.get_notify_sender();
203-
204-
::ext_php_rs::zend::RUNTIME.spawn(async move {
205-
let res = future.await;
206-
sender.send(idx).unwrap();
207-
::std::io::Write::write_all(&mut notifier, &[0]).unwrap();
208-
res
209-
})
210-
});
211-
212-
::ext_php_rs::zend::EventLoop::suspend();
213-
214-
return ::ext_php_rs::zend::RUNTIME
215-
.block_on(future).unwrap();
196+
::ext_php_rs::zend::EventLoop::suspend_on(async move #stmts)
216197
}};
217198
}
218199
let this = match method_type {

src/zend/fibers.rs

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::zend::Function;
66

77
use std::cell::RefCell;
88
use std::fs::File;
9+
use std::future::Future;
910
use std::io;
1011
use std::os::fd::{RawFd, FromRawFd};
1112
use std::sync::mpsc::{Sender, Receiver, channel};
@@ -18,6 +19,10 @@ lazy_static! {
1819
pub static ref RUNTIME: Runtime = Runtime::new().expect("Could not allocate runtime");
1920
}
2021

22+
thread_local! {
23+
static EVENTLOOP: RefCell<Option<EventLoop>> = RefCell::new(None);
24+
}
25+
2126
#[cfg(any(target_os = "linux", target_os = "solaris"))]
2227
fn sys_pipe() -> io::Result<(RawFd, RawFd)> {
2328
let mut pipefd = [0; 2];
@@ -43,7 +48,50 @@ pub struct EventLoop {
4348
}
4449

4550
impl EventLoop {
46-
pub fn new() -> PhpResult<Self> {
51+
pub fn init() -> PhpResult<u64> {
52+
EVENTLOOP.with_borrow_mut(|e| {
53+
Ok(
54+
match e {
55+
None => e.insert(Self::new()?),
56+
Some(ev) => ev
57+
}.notify_receiver.as_raw_fd() as u64
58+
)
59+
})
60+
}
61+
62+
pub fn suspend_on<T: Send + 'static, F: Future<Output = T> + Send + 'static>(future: F) -> T {
63+
let future = EVENTLOOP.with_borrow_mut(move |c| {
64+
let c = c.as_mut().unwrap();
65+
let idx = c.fibers.len() as u64;
66+
c.fibers.insert_at_index(idx, call_user_func!(c.get_current_suspension).unwrap()).unwrap();
67+
68+
let sender = c.sender.clone();
69+
let mut notifier = c.notify_sender.try_clone().unwrap();
70+
71+
RUNTIME.spawn(async move {
72+
let res = future.await;
73+
sender.send(idx).unwrap();
74+
::std::io::Write::write_all(&mut notifier, &[0]).unwrap();
75+
res
76+
})
77+
});
78+
79+
call_user_func!(Function::from_method("\\Revolt\\EventLoop", "getSuspension")).unwrap().try_call_method("suspend", vec![]).unwrap();
80+
81+
return RUNTIME.block_on(future).unwrap();
82+
}
83+
84+
pub fn wakeup() -> PhpResult<()> {
85+
EVENTLOOP.with_borrow_mut(|c| {
86+
c.as_mut().unwrap().wakeup_internal()
87+
})
88+
}
89+
90+
pub fn shutdown() {
91+
EVENTLOOP.set(None)
92+
}
93+
94+
fn new() -> PhpResult<Self> {
4795
let (sender, receiver) = channel();
4896
let (notify_receiver, notify_sender) =
4997
sys_pipe().map_err(|err| format!("Could not create pipe: {}", err))?;
@@ -62,11 +110,7 @@ impl EventLoop {
62110
})
63111
}
64112

65-
pub fn get_event_fd(&self)->u64 {
66-
self.notify_receiver.as_raw_fd() as u64
67-
}
68-
69-
pub fn wakeup_internal(&mut self) -> PhpResult<()> {
113+
fn wakeup_internal(&mut self) -> PhpResult<()> {
70114
self.notify_receiver.read_exact(&mut self.dummy).unwrap();
71115

72116
for fiber_id in self.receiver.try_iter() {
@@ -77,26 +121,11 @@ impl EventLoop {
77121
}
78122
Ok(())
79123
}
80-
81-
pub fn prepare_resume(&mut self) -> u64 {
82-
let idx = self.fibers.len() as u64;
83-
self.fibers.insert_at_index(idx, call_user_func!(self.get_current_suspension).unwrap()).unwrap();
84-
85-
idx
86-
}
87-
88-
pub fn suspend() {
89-
call_user_func!(Function::from_method("\\Revolt\\EventLoop", "getSuspension")).unwrap().try_call_method("suspend", vec![]).unwrap();
90-
}
91-
92-
pub fn get_sender(&self) -> Sender<u64> {
93-
self.sender.clone()
94-
}
95-
pub fn get_notify_sender(&self) -> File {
96-
self.notify_sender.try_clone().unwrap()
97-
}
98124
}
99125

100-
thread_local! {
101-
pub static EVENTLOOP: RefCell<Option<EventLoop>> = RefCell::new(None);
102-
}
126+
impl Drop for EventLoop {
127+
fn drop(&mut self) {
128+
unsafe { libc::close(self.notify_receiver.as_raw_fd()) };
129+
unsafe { libc::close(self.notify_sender.as_raw_fd()) };
130+
}
131+
}

src/zend/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub use function::FunctionEntry;
2121
pub use function::Function;
2222
pub use globals::ExecutorGlobals;
2323
pub use fibers::RUNTIME;
24-
pub use fibers::EVENTLOOP;
2524
pub use fibers::EventLoop;
2625
pub use borrow_unchecked::borrow_unchecked;
2726
pub use borrow_unchecked::BorrowUnchecked;

0 commit comments

Comments
 (0)