Guest User

Untitled

a guest
Jun 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. use std::thread::{sleep, spawn};
  2. use std::time::Duration;
  3.  
  4. extern crate futures; // 0.2.1
  5. use futures::channel::mpsc;
  6. use futures::executor::LocalPool;
  7. use futures::task::Context;
  8. use futures::{Async, Future, Never};
  9. use futures::stream::StreamFuture;
  10. use futures::StreamExt;
  11.  
  12. struct Notify {
  13. irq: StreamFuture<mpsc::Receiver<()>>
  14. }
  15.  
  16. impl Future for Notify {
  17. type Item = ();
  18. type Error = Never;
  19. fn poll(&mut self, _ctx: &mut Context) -> Result<Async<Self::Item>, Self::Error> {
  20. match self.irq.poll(_ctx) {
  21. Ok(Async::Ready((item, stream))) => {
  22. if item.is_none() { return Ok(Async::Ready(())) } //irq_tx dropped
  23. self.irq = stream.next();
  24. _ctx.waker().wake();
  25. println!("Irq receive: {:?}", item);
  26. }
  27. Err((err, stream)) => {
  28. self.irq = stream.next();
  29. println!("Irq receive error: {:?}", err);
  30. }
  31. _ => {}
  32. }
  33. Ok(Async::Pending)
  34. }
  35. }
  36.  
  37. // ожидание прерывания от ядра
  38. fn epoll_wait() -> std::io::Result<()> {
  39. sleep(Duration::from_millis(100));
  40. Ok(())
  41. }
  42.  
  43. fn main() {
  44. let (mut irq_tx, irq_rx) = mpsc::channel(1);
  45.  
  46. let mut pool = LocalPool::new();
  47. let mut executor = pool.executor();
  48. let notify = Notify { irq: irq_rx.next() };
  49.  
  50. spawn(move || {
  51. while let Ok(_) = epoll_wait() {
  52. // Возможно ли здесь разместить Waker от Notify?
  53. // что бы будить ее
  54. if let Err(err) = irq_tx.try_send(()) {
  55. println!("Send irq to notify error: {}", err);
  56. } else {
  57. println!("Send irq to notify successfully");
  58. }
  59. }
  60. });
  61.  
  62. executor.spawn_local(notify).unwrap();
  63. pool.run(&mut executor);
  64. }
Add Comment
Please, Sign In to add comment