Skip to content

Commit 47997d4

Browse files
kchibisovvberger
authored andcommitted
Use platform specific error type
1 parent 0bcc2b8 commit 47997d4

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

src/sources/timer/mod.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ use std::time::{Duration, Instant};
1414

1515
use crate::{EventSource, Poll, PostAction, Readiness, Token, TokenFactory};
1616

17-
#[cfg(target_os = "linux")]
18-
mod timerfd;
19-
#[cfg(target_os = "linux")]
20-
use timerfd::{TimerScheduler, TimerSource};
21-
2217
#[cfg(any(
2318
target_os = "dragonfly",
2419
target_os = "freebsd",
@@ -27,6 +22,8 @@ use timerfd::{TimerScheduler, TimerSource};
2722
target_os = "macos"
2823
))]
2924
mod threaded;
25+
#[cfg(target_os = "linux")]
26+
mod timerfd;
3027

3128
#[cfg(any(
3229
target_os = "dragonfly",
@@ -36,11 +33,23 @@ mod threaded;
3633
target_os = "macos"
3734
))]
3835
use threaded::{TimerScheduler, TimerSource};
36+
#[cfg(target_os = "linux")]
37+
use timerfd::{TimerScheduler, TimerSource};
3938

4039
/// An error arising from processing events for a timer.
4140
#[derive(thiserror::Error, Debug)]
42-
#[error(transparent)]
43-
pub struct TimerError(Box<dyn std::error::Error + Sync + Send>);
41+
#[error("{0}")]
42+
pub struct TimerError(
43+
#[cfg(target_os = "linux")] std::io::Error,
44+
#[cfg(any(
45+
target_os = "dragonfly",
46+
target_os = "freebsd",
47+
target_os = "netbsd",
48+
target_os = "openbsd",
49+
target_os = "macos"
50+
))]
51+
crate::ping::PingError,
52+
);
4453

4554
/// A Timer event source
4655
///
@@ -146,21 +155,23 @@ impl<T> EventSource for Timer<T> {
146155
inner: self.inner.clone(),
147156
};
148157
let inner = &self.inner;
149-
self.source.process_events(readiness, token, |(), &mut ()| {
150-
loop {
151-
let next_expired: Option<T> = {
152-
let mut guard = inner.lock().unwrap();
153-
guard.next_expired()
154-
};
155-
if let Some(val) = next_expired {
156-
callback(val, &mut handle);
157-
} else {
158-
break;
158+
self.source
159+
.process_events(readiness, token, |(), &mut ()| {
160+
loop {
161+
let next_expired: Option<T> = {
162+
let mut guard = inner.lock().unwrap();
163+
guard.next_expired()
164+
};
165+
if let Some(val) = next_expired {
166+
callback(val, &mut handle);
167+
} else {
168+
break;
169+
}
159170
}
160-
}
161-
// now compute the next timeout and signal if necessary
162-
inner.lock().unwrap().reschedule();
163-
})
171+
// now compute the next timeout and signal if necessary
172+
inner.lock().unwrap().reschedule();
173+
})
174+
.map_err(TimerError)
164175
}
165176

166177
fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> {

src/sources/timer/threaded.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ use std::sync::atomic::{AtomicBool, Ordering};
44
use std::sync::{Arc, Mutex};
55
use std::time::Instant;
66

7-
use crate::ping::{make_ping, Ping, PingSource};
7+
use crate::ping::{make_ping, Ping, PingError, PingSource};
88
use crate::{EventSource, Poll, PostAction, Readiness, Token, TokenFactory};
99

10-
use super::TimerError;
11-
1210
#[derive(Debug)]
1311
pub struct TimerSource {
1412
source: PingSource,
@@ -25,7 +23,7 @@ impl EventSource for TimerSource {
2523
type Event = ();
2624
type Metadata = ();
2725
type Ret = ();
28-
type Error = TimerError;
26+
type Error = PingError;
2927

3028
fn process_events<C>(
3129
&mut self,
@@ -36,11 +34,9 @@ impl EventSource for TimerSource {
3634
where
3735
C: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
3836
{
39-
self.source
40-
.process_events(readiness, token, |_, &mut _| {
41-
callback((), &mut ());
42-
})
43-
.map_err(|err| TimerError(err.into()))
37+
self.source.process_events(readiness, token, |_, &mut _| {
38+
callback((), &mut ());
39+
})
4440
}
4541

4642
fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> {

src/sources/timer/timerfd.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ use std::os::unix::io::{AsRawFd, RawFd};
44
use std::time::Instant;
55

66
use nix::sys::time::TimeSpec;
7-
use nix::sys::timerfd::{self, Expiration, TimerFd, TimerSetTimeFlags};
7+
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags};
88

99
use crate::generic::Generic;
1010
use crate::{EventSource, Poll, PostAction, Readiness, Token, TokenFactory};
1111

12-
use super::TimerError;
13-
1412
#[derive(Debug)]
1513
pub struct TimerScheduler {
1614
current_deadline: Option<Instant>,
@@ -20,8 +18,8 @@ pub struct TimerScheduler {
2018
impl TimerScheduler {
2119
pub fn new() -> crate::Result<(Self, TimerSource)> {
2220
let timerfd = TimerFd::new(
23-
timerfd::ClockId::CLOCK_MONOTONIC,
24-
timerfd::TimerFlags::TFD_CLOEXEC,
21+
ClockId::CLOCK_MONOTONIC,
22+
TimerFlags::TFD_CLOEXEC | TimerFlags::TFD_NONBLOCK,
2523
)?;
2624

2725
let source = TimerSource::new(&timerfd);
@@ -79,7 +77,7 @@ impl EventSource for TimerSource {
7977
type Event = ();
8078
type Metadata = ();
8179
type Ret = ();
82-
type Error = TimerError;
80+
type Error = std::io::Error;
8381

8482
fn process_events<C>(
8583
&mut self,
@@ -90,12 +88,10 @@ impl EventSource for TimerSource {
9088
where
9189
C: FnMut(Self::Event, &mut Self::Metadata) -> Self::Ret,
9290
{
93-
self.source
94-
.process_events(readiness, token, |_, &mut _| {
95-
callback((), &mut ());
96-
Ok(PostAction::Continue)
97-
})
98-
.map_err(|err| TimerError(err.into()))
91+
self.source.process_events(readiness, token, |_, &mut _| {
92+
callback((), &mut ());
93+
Ok(PostAction::Continue)
94+
})
9995
}
10096

10197
fn register(&mut self, poll: &mut Poll, token_factory: &mut TokenFactory) -> crate::Result<()> {

0 commit comments

Comments
 (0)