Skip to content
This repository was archived by the owner on Feb 2, 2026. It is now read-only.

Commit 4c8a58f

Browse files
authored
Add Handler::tick() method (#12)
1 parent ae0b950 commit 4c8a58f

File tree

4 files changed

+76
-17
lines changed

4 files changed

+76
-17
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [0.2.2] - 2025-12-16
4+
5+
* Add Handler::tick() method, indicates one turn of reactor
6+
37
## [0.2.1] - 2025-12-15
48

59
* Clear driver within runtime

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-neon"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "Async runtime for ntex"
55
categories = ["asynchronous"]
66
keywords = ["async", "runtime"]

src/driver/iour.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd};
22
use std::{cell::Cell, cell::RefCell, cmp, collections::VecDeque, io, mem, rc::Rc, sync::Arc};
33

4-
use io_uring::cqueue::{self, Entry as CEntry, more};
4+
use io_uring::cqueue::{self, more, Entry as CEntry};
55
use io_uring::opcode::{AsyncCancel, PollAdd};
66
use io_uring::squeue::{Entry as SEntry, SubmissionQueue};
7-
use io_uring::{IoUring, Probe, types::Fd};
7+
use io_uring::{types::Fd, IoUring, Probe};
88

99
use crate::pool::Dispatchable;
1010

@@ -17,6 +17,9 @@ pub trait Handler {
1717
/// Operation is canceled
1818
fn canceled(&mut self, id: usize);
1919

20+
/// Driver turn is completed
21+
fn tick(&mut self);
22+
2023
/// Cleanup before drop
2124
fn cleanup(&mut self);
2225
}
@@ -97,10 +100,25 @@ pub struct Driver {
97100
fd: RawFd,
98101
hid: Cell<u64>,
99102
notifier: Notifier,
100-
handlers: Cell<Option<Box<Vec<Box<dyn Handler>>>>>,
103+
#[allow(clippy::box_collection)]
104+
handlers: Cell<Option<Box<Vec<HandlerItem>>>>,
101105
inner: Rc<DriverInner>,
102106
}
103107

108+
struct HandlerItem {
109+
hnd: Box<dyn Handler>,
110+
modified: bool,
111+
}
112+
113+
impl HandlerItem {
114+
fn tick(&mut self) {
115+
if self.modified {
116+
self.modified = false;
117+
self.hnd.tick();
118+
}
119+
}
120+
}
121+
104122
bitflags::bitflags! {
105123
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
106124
struct Flags: u8 {
@@ -189,10 +207,13 @@ impl Driver {
189207
{
190208
let id = self.hid.get();
191209
let mut handlers = self.handlers.take().unwrap_or_default();
192-
handlers.push(f(DriverApi {
193-
batch: id << Self::BATCH,
194-
inner: self.inner.clone(),
195-
}));
210+
handlers.push(HandlerItem {
211+
hnd: f(DriverApi {
212+
batch: id << Self::BATCH,
213+
inner: self.inner.clone(),
214+
}),
215+
modified: false,
216+
});
196217
self.handlers.set(Some(handlers));
197218
self.hid.set(id + 1);
198219
}
@@ -300,18 +321,25 @@ impl Driver {
300321

301322
let result = entry.result();
302323
if result == -libc::ECANCELED {
303-
handlers[batch].canceled(user_data);
324+
handlers[batch].modified = true;
325+
handlers[batch].hnd.canceled(user_data);
304326
} else {
305327
let result = if result < 0 {
306328
Err(io::Error::from_raw_os_error(-result))
307329
} else {
308330
Ok(result as _)
309331
};
310-
handlers[batch].completed(user_data, entry.flags(), result);
332+
handlers[batch].modified = true;
333+
handlers[batch]
334+
.hnd
335+
.completed(user_data, entry.flags(), result);
311336
}
312337
}
313338
}
314339
}
340+
for h in handlers.iter_mut() {
341+
h.tick();
342+
}
315343
self.handlers.set(Some(handlers));
316344
}
317345
}
@@ -323,7 +351,7 @@ impl Driver {
323351

324352
pub(crate) fn clear(&self) {
325353
for mut h in self.handlers.take().unwrap().into_iter() {
326-
h.cleanup()
354+
h.hnd.cleanup()
327355
}
328356
}
329357
}

src/driver/poll.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub trait Handler {
1313
/// Operation submission has failed
1414
fn error(&mut self, id: usize, err: io::Error);
1515

16+
/// Driver turn is completed
17+
fn tick(&mut self);
18+
1619
/// Cleanup before drop
1720
fn cleanup(&mut self);
1821
}
@@ -108,7 +111,22 @@ pub struct Driver {
108111
capacity: usize,
109112
changes: Rc<UnsafeCell<VecDeque<Change>>>,
110113
hid: Cell<u64>,
111-
handlers: Cell<Option<Box<Vec<Box<dyn Handler>>>>>,
114+
#[allow(clippy::box_collection)]
115+
handlers: Cell<Option<Box<Vec<HandlerItem>>>>,
116+
}
117+
118+
struct HandlerItem {
119+
hnd: Box<dyn Handler>,
120+
modified: bool,
121+
}
122+
123+
impl HandlerItem {
124+
fn tick(&mut self) {
125+
if self.modified {
126+
self.modified = false;
127+
self.hnd.tick();
128+
}
129+
}
112130
}
113131

114132
impl Driver {
@@ -150,7 +168,10 @@ impl Driver {
150168
poll: self.poll.clone(),
151169
changes: self.changes.clone(),
152170
};
153-
handlers.push(f(api));
171+
handlers.push(HandlerItem {
172+
hnd: f(api),
173+
modified: false,
174+
});
154175
self.hid.set(id + 1);
155176
self.handlers.set(Some(handlers));
156177
}
@@ -187,21 +208,27 @@ impl Driver {
187208
for event in events.iter() {
188209
let key = event.key as u64;
189210
let batch = ((key & Self::BATCH_MASK) >> Self::BATCH) as usize;
190-
handlers[batch].event((key & Self::DATA_MASK) as usize, event)
211+
handlers[batch].modified = true;
212+
handlers[batch]
213+
.hnd
214+
.event((key & Self::DATA_MASK) as usize, event)
191215
}
192216
self.apply_changes(&mut handlers);
217+
for h in handlers.iter_mut() {
218+
h.tick();
219+
}
193220
self.handlers.set(Some(handlers));
194221
}
195222
}
196223

197-
fn apply_changes(&self, handlers: &mut [Box<dyn Handler>]) {
224+
fn apply_changes(&self, handlers: &mut [HandlerItem]) {
198225
while let Some(op) = unsafe { (*self.changes.get()).pop_front() } {
199226
match op {
200227
Change::Error {
201228
batch,
202229
user_data,
203230
error,
204-
} => handlers[batch].error(user_data as usize, error),
231+
} => handlers[batch].hnd.error(user_data as usize, error),
205232
Change::Blocking(f) => {
206233
let _ = crate::Runtime::with_current(|rt| rt.pool.dispatch(f));
207234
}
@@ -216,7 +243,7 @@ impl Driver {
216243

217244
pub(crate) fn clear(&self) {
218245
for mut h in self.handlers.take().unwrap().into_iter() {
219-
h.cleanup()
246+
h.hnd.cleanup()
220247
}
221248
}
222249
}

0 commit comments

Comments
 (0)