1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
//! Module with time related utilities. //! //! This module provides three types. //! //! - [`Timer`](Timer) is a stand-alone future that returns //! [`DeadlinePassed`](DeadlinePassed) once the deadline has passed. //! - [`Deadline`](Deadline) wraps another [`Future`] and checks the deadline //! each time it's polled. //! - [`Interval`](Interval) implements [`Stream`] which yields an item //! after the deadline has passed each interval. use std::future::Future; use std::mem::ManuallyDrop; use std::pin::Pin; use std::stream::Stream; use std::task::{self, Poll}; use std::time::{Duration, Instant}; use std::{io, ptr}; use crate::{actor, rt}; /// Type returned when the deadline has passed. /// /// Can be converted into [`io::ErrorKind::TimedOut`]. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct DeadlinePassed; impl From<DeadlinePassed> for io::Error { fn from(_: DeadlinePassed) -> io::Error { io::ErrorKind::TimedOut.into() } } /// A [`Future`] that represents a timer. /// /// If this future returns [`Poll::Ready`]`(`[`DeadlinePassed`]`)` it means that /// the deadline has passed. If it returns [`Poll::Pending`] it's not yet /// passed. /// /// # Examples /// /// ``` /// # #![feature(never_type)] /// # /// use std::time::Duration; /// # use std::time::Instant; /// /// use heph::actor; /// # use heph::supervisor::NoSupervisor; /// # use heph::{rt, ActorOptions, Runtime, RuntimeRef}; /// use heph::rt::ThreadLocal; /// use heph::timer::Timer; /// /// # fn main() -> Result<(), rt::Error> { /// # let mut runtime = Runtime::new()?; /// # runtime.run_on_workers(setup)?; /// # runtime.start() /// # } /// # /// # /// # fn setup(mut runtime_ref: RuntimeRef) -> Result<(), !> { /// # let actor = actor as fn(_) -> _; /// # let options = ActorOptions::default(); /// # runtime_ref.spawn_local(NoSupervisor, actor, (), options); /// # Ok(()) /// # } /// # /// async fn actor(mut ctx: actor::Context<!, ThreadLocal>) { /// # let start = Instant::now(); /// // Create a timer, this will be ready once the timeout has passed. /// let timeout = Timer::after(&mut ctx, Duration::from_millis(200)); /// # assert!(timeout.deadline() >= start + Duration::from_millis(200)); /// /// // Wait for the timer to pass. /// timeout.await; /// # assert!(Instant::now() >= start + Duration::from_millis(200)); /// println!("200 milliseconds have passed!"); /// } /// ``` #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct Timer<RT: rt::Access> { deadline: Instant, rt: RT, // NOTE: when adding fields also add to [`Timer::wrap`]. } impl<RT: rt::Access> Timer<RT> { /// Create a new `Timer`. pub fn at<M>(ctx: &mut actor::Context<M, RT>, deadline: Instant) -> Timer<RT> where RT: Clone, { let mut rt = ctx.runtime().clone(); rt.add_deadline(deadline); Timer { deadline, rt } } /// Create a new timer, based on a timeout. /// /// Same as calling `Timer::at(&mut ctx, Instant::now() + timeout)`. pub fn after<M>(ctx: &mut actor::Context<M, RT>, timeout: Duration) -> Timer<RT> where RT: Clone, { Timer::at(ctx, Instant::now() + timeout) } /// Returns the deadline set for this `Timer`. pub const fn deadline(&self) -> Instant { self.deadline } /// Returns `true` if the deadline has passed. pub fn has_passed(&self) -> bool { self.deadline <= Instant::now() } /// Wrap a future creating a new `Deadline`. pub fn wrap<Fut>(self, future: Fut) -> Deadline<Fut, RT> { // We don't want to run the destructor as that would remove the // deadline, which we need in `Deadline` as well. As a bonus we can // safetly move `RT` without having to clone it (which normally can't be // done with `Drop` types). // Safety: See [`ManuallyDrop::take`], rather then taking the entire // thing struct at once we read (move out of) value by value. let this = ManuallyDrop::new(self); let deadline = unsafe { ptr::addr_of!(this.deadline).read() }; let rt = unsafe { ptr::addr_of!(this.rt).read() }; Deadline { deadline, future, rt, } } } impl<RT: rt::Access> Future for Timer<RT> { type Output = DeadlinePassed; fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> { if self.has_passed() { Poll::Ready(DeadlinePassed) } else { Poll::Pending } } } impl<RT: rt::Access> Unpin for Timer<RT> {} impl<RT: rt::Access> actor::Bound<RT> for Timer<RT> { type Error = io::Error; fn bind_to<M>(&mut self, ctx: &mut actor::Context<M, RT>) -> io::Result<()> { let old_pid = self.rt.change_pid(ctx.runtime_ref().pid()); self.rt.change_deadline(old_pid, self.deadline); Ok(()) } } impl<RT: rt::Access> Drop for Timer<RT> { fn drop(&mut self) { self.rt.remove_deadline(self.deadline); } } /// A [`Future`] that wraps another future setting a deadline for it. /// /// When this future is polled it first checks if the deadline has passed, if so /// it returns [`Poll::Ready`]`(Err(`[`DeadlinePassed`]`.into()))`. Otherwise /// this will poll the future it wraps. /// /// # Notes /// /// This type can also be created using [`Timer::wrap`], this is useful when /// dealing with lifetime issue, e.g. when calling /// [`actor::Context::receive_next`] and wrapping that in a `Deadline`. /// /// # Examples /// /// Setting a timeout for a future. /// /// ``` /// use std::io; /// # use std::future::Future; /// # use std::pin::Pin; /// # use std::task::{self, Poll}; /// use std::time::Duration; /// # use std::time::Instant; /// /// use heph::actor; /// # use heph::supervisor::NoSupervisor; /// use heph::rt::ThreadSafe; /// # use heph::{rt, ActorOptions, Runtime}; /// use heph::timer::Deadline; /// /// # fn main() -> Result<(), rt::Error> { /// # let actor = actor as fn(_) -> _; /// # let options = ActorOptions::default(); /// # let mut rt = Runtime::new()?; /// # let _ = rt.spawn(NoSupervisor, actor, (), options); /// # rt.start() /// # } /// # /// # struct IoFuture; /// # /// # impl Future for IoFuture { /// # type Output = io::Result<()>; /// # fn poll(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Self::Output> { /// # Poll::Pending /// # } /// # } /// # /// async fn actor(mut ctx: actor::Context<String, ThreadSafe>) { /// // `OtherFuture` is a type that implements `Future`. /// let future = IoFuture; /// // Create our deadline. /// # let start = Instant::now(); /// let deadline_future = Deadline::after(&mut ctx, Duration::from_millis(100), future); /// # assert!(deadline_future.deadline() >= start + Duration::from_millis(100)); /// /// // Now we await the results. /// let result = deadline_future.await; /// # assert!(Instant::now() >= start + Duration::from_millis(100)); /// // However the other future is rather slow, so the timeout will pass. /// assert!(result.is_err()); /// assert_eq!(result.unwrap_err().kind(), io::ErrorKind::TimedOut); /// } /// ``` #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct Deadline<Fut, RT: rt::Access> { deadline: Instant, future: Fut, rt: RT, // NOTE: when adding fields also add to [`Deadline::into_inner`]. } impl<Fut, RT: rt::Access> Deadline<Fut, RT> { /// Create a new `Deadline`. pub fn at<M>( ctx: &mut actor::Context<M, RT>, deadline: Instant, future: Fut, ) -> Deadline<Fut, RT> where RT: Clone, { let mut rt = ctx.runtime().clone(); rt.add_deadline(deadline); Deadline { deadline, future, rt, } } /// Create a new deadline based on a timeout. /// /// Same as calling `Deadline::at(&mut ctx, Instant::now() + timeout, /// future)`. pub fn after<M>( ctx: &mut actor::Context<M, RT>, timeout: Duration, future: Fut, ) -> Deadline<Fut, RT> where RT: Clone, { Deadline::at(ctx, Instant::now() + timeout, future) } /// Returns the deadline set for this `Deadline`. pub const fn deadline(&self) -> Instant { self.deadline } /// Returns `true` if the deadline has passed. pub fn has_passed(&self) -> bool { self.deadline <= Instant::now() } /// Returns a reference to the wrapped future. pub const fn get_ref(&self) -> &Fut { &self.future } /// Returns a mutable reference to the wrapped future. pub fn get_mut(&mut self) -> &mut Fut { &mut self.future } /// Returns the wrapped future. pub fn into_inner(mut self) -> Fut { self.rt.remove_deadline(self.deadline); // Safety: See [`ManuallyDrop::take`], rather then taking the entire // thing struct at once we read (move out of) value by value. let mut this = ManuallyDrop::new(self); unsafe { ptr::addr_of_mut!(this.deadline).drop_in_place() } unsafe { ptr::addr_of_mut!(this.rt).drop_in_place() } unsafe { ptr::addr_of!(this.future).read() } } } /* TODO: add this once `specialization` feature is stabilised. impl<Fut> Future for Deadline<Fut> where Fut: Future, { type Output = Result<Fut::Output, DeadlinePassed>; fn poll(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> Poll<Self::Output> { if self.has_passed() { Poll::Ready(Err(DeadlinePassed)) } else { // Safety: this is safe because we're not moving the future. let future = unsafe { Pin::map_unchecked_mut(self, |this| &mut this.future) }; future.poll(ctx).map(Ok) } } } */ impl<Fut, RT: rt::Access, T, E> Future for Deadline<Fut, RT> where Fut: Future<Output = Result<T, E>>, E: From<DeadlinePassed>, { type Output = Result<T, E>; fn poll(self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> Poll<Self::Output> { if self.has_passed() { Poll::Ready(Err(DeadlinePassed.into())) } else { // Safety: this is safe because we're not moving the future. let future = unsafe { Pin::map_unchecked_mut(self, |this| &mut this.future) }; future.poll(ctx) } } } impl<Fut: Unpin, RT: rt::Access> Unpin for Deadline<Fut, RT> {} impl<Fut, RT: rt::Access> actor::Bound<RT> for Deadline<Fut, RT> { type Error = io::Error; fn bind_to<M>(&mut self, ctx: &mut actor::Context<M, RT>) -> io::Result<()> { let old_pid = self.rt.change_pid(ctx.runtime_ref().pid()); self.rt.change_deadline(old_pid, self.deadline); Ok(()) } } impl<Fut, RT: rt::Access> Drop for Deadline<Fut, RT> { fn drop(&mut self) { self.rt.remove_deadline(self.deadline); } } /// A [`Stream`] that yields an item after an interval has passed. /// /// This stream will never return `None`, it will always set another deadline /// and yield another item after the deadline has passed. /// /// # Notes /// /// The next deadline will always will be set after this returns `Poll::Ready`. /// This means that if the interval is very short and the stream is not polled /// often enough it's possible that the actual time between yielding two values /// can become bigger then the specified interval. /// /// # Examples /// /// The following example will print hello world (roughly) every 200 /// milliseconds. /// /// ``` /// # #![feature(never_type)] /// # /// use std::time::Duration; /// # use std::time::Instant; /// /// use heph::actor; /// # use heph::supervisor::NoSupervisor; /// # use heph::{rt, ActorOptions, Runtime, RuntimeRef}; /// use heph::rt::ThreadLocal; /// use heph::timer::Interval; /// use heph::util::next; /// # /// # fn main() -> Result<(), rt::Error> { /// # let mut runtime = Runtime::new()?; /// # runtime.run_on_workers(setup)?; /// # runtime.start() /// # } /// # /// # fn setup(mut runtime_ref: RuntimeRef) -> Result<(), !> { /// # let actor = actor as fn(_) -> _; /// # let options = ActorOptions::default(); /// # runtime_ref.spawn_local(NoSupervisor, actor, (), options); /// # Ok(()) /// # } /// /// async fn actor(mut ctx: actor::Context<!, ThreadLocal>) { /// # let start = Instant::now(); /// let mut interval = Interval::every(&mut ctx, Duration::from_millis(200)); /// # assert!(interval.next_deadline() >= start + Duration::from_millis(200)); /// loop { /// // Wait until the next timer expires. /// let _ = next(&mut interval).await; /// # assert!(start.elapsed() >= Duration::from_millis(200)); /// println!("Hello world"); /// # break; /// } /// } /// ``` #[derive(Debug)] #[must_use = "streams do nothing unless polled"] pub struct Interval<RT: rt::Access> { deadline: Instant, interval: Duration, rt: RT, } impl<RT: rt::Access> Interval<RT> { /// Create a new `Interval`. pub fn every<M>(ctx: &mut actor::Context<M, RT>, interval: Duration) -> Interval<RT> where RT: Clone, { let deadline = Instant::now() + interval; let mut rt = ctx.runtime().clone(); rt.add_deadline(deadline); Interval { deadline, interval, rt, } } /// Returns the next deadline for this `Interval`. pub const fn next_deadline(&self) -> Instant { self.deadline } } impl<RT: rt::Access> Stream for Interval<RT> { type Item = DeadlinePassed; fn poll_next(self: Pin<&mut Self>, _: &mut task::Context<'_>) -> Poll<Option<Self::Item>> { if self.deadline <= Instant::now() { // Determine the next deadline. let next_deadline = Instant::now() + self.interval; let this = Pin::get_mut(self); this.deadline = next_deadline; this.rt.add_deadline(next_deadline); Poll::Ready(Some(DeadlinePassed)) } else { Poll::Pending } } } impl<RT: rt::Access> Unpin for Interval<RT> {} impl<RT: rt::Access> actor::Bound<RT> for Interval<RT> { type Error = !; fn bind_to<M>(&mut self, ctx: &mut actor::Context<M, RT>) -> Result<(), !> { let old_pid = self.rt.change_pid(ctx.runtime_ref().pid()); self.rt.change_deadline(old_pid, self.deadline); Ok(()) } } impl<RT: rt::Access> Drop for Interval<RT> { fn drop(&mut self) { self.rt.remove_deadline(self.deadline); } }