Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::thread::{sleep, spawn};
- use std::time::Duration;
- extern crate futures; // 0.2.1
- use futures::channel::mpsc;
- use futures::executor::LocalPool;
- use futures::task::Context;
- use futures::{Async, Future, Never};
- use futures::stream::StreamFuture;
- use futures::StreamExt;
- struct Notify {
- irq: StreamFuture<mpsc::Receiver<()>>
- }
- impl Future for Notify {
- type Item = ();
- type Error = Never;
- fn poll(&mut self, _ctx: &mut Context) -> Result<Async<Self::Item>, Self::Error> {
- match self.irq.poll(_ctx) {
- Ok(Async::Ready((item, stream))) => {
- if item.is_none() { return Ok(Async::Ready(())) } //irq_tx dropped
- self.irq = stream.next();
- _ctx.waker().wake();
- println!("Irq receive: {:?}", item);
- }
- Err((err, stream)) => {
- self.irq = stream.next();
- println!("Irq receive error: {:?}", err);
- }
- _ => {}
- }
- Ok(Async::Pending)
- }
- }
- // ожидание прерывания от ядра
- fn epoll_wait() -> std::io::Result<()> {
- sleep(Duration::from_millis(100));
- Ok(())
- }
- fn main() {
- let (mut irq_tx, irq_rx) = mpsc::channel(1);
- let mut pool = LocalPool::new();
- let mut executor = pool.executor();
- let notify = Notify { irq: irq_rx.next() };
- spawn(move || {
- while let Ok(_) = epoll_wait() {
- // Возможно ли здесь разместить Waker от Notify?
- // что бы будить ее
- if let Err(err) = irq_tx.try_send(()) {
- println!("Send irq to notify error: {}", err);
- } else {
- println!("Send irq to notify successfully");
- }
- }
- });
- executor.spawn_local(notify).unwrap();
- pool.run(&mut executor);
- }
Add Comment
Please, Sign In to add comment