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
//! An interrupt service routine tasks chain.
//!
//! See [`Routine`] for more details.
//!
//! [`Routine`]: struct.Routine.html

use collections::LinkedList;
use core::ops::Generator;
use core::ops::GeneratorState::*;
use prelude::*;

/// An interrupt service routine tasks chain.
///
/// A lock-free data-structure to associate a task with an interrupt.
pub struct Routine {
  list: LinkedList<Node>,
}

struct Node {
  thread: Box<Generator<Yield = (), Return = ()>>,
}

impl<T> From<T> for Node
where
  T: Generator<Yield = (), Return = ()>,
  T: Send + 'static,
{
  #[inline]
  fn from(generator: T) -> Self {
    let thread = Box::new(generator);
    Self { thread }
  }
}

impl Routine {
  /// Creates an empty `Routine`.
  #[inline]
  pub const fn new() -> Self {
    Self {
      list: LinkedList::new(),
    }
  }

  /// The method is invoked by hardware to run the routine chain.
  ///
  /// # Safety
  ///
  /// Must not be called concurrently.
  pub unsafe fn invoke(&mut self) {
    self
      .list
      .drain_filter(|node| match node.thread.resume() {
        Yielded(()) => false,
        Complete(()) => true,
      })
      .for_each(|_| {});
  }

  /// Adds a generator first in the chain.
  pub fn push<G>(&mut self, g: G)
  where
    G: Generator<Yield = (), Return = ()>,
    G: Send + 'static,
  {
    self.list.push(g.into());
  }

  /// Adds a closure first in the chain.
  pub fn push_callback<F>(&mut self, f: F)
  where
    F: FnOnce(),
    F: Send + 'static,
  {
    self.push(|| {
      if false {
        yield;
      }
      f()
    });
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use alloc::arc::Arc;
  use core::cell::Cell;
  use core::mem::size_of;

  struct Counter(Cell<i8>);

  struct Wrapper(Arc<Counter>);

  unsafe impl Sync for Counter {}

  impl Drop for Wrapper {
    fn drop(&mut self) {
      (self.0).0.set(-(self.0).0.get());
    }
  }

  #[test]
  fn size_of_routine() {
    assert_eq!(size_of::<Routine>(), size_of::<usize>());
  }

  #[test]
  fn generator() {
    let mut routine = Routine::new();
    let counter = Arc::new(Counter(Cell::new(0)));
    let wrapper = Wrapper(Arc::clone(&counter));
    routine.push(move || loop {
      {
        (wrapper.0).0.set((wrapper.0).0.get() + 1);
        if (wrapper.0).0.get() == 2 {
          break;
        }
      }
      yield;
    });
    assert_eq!(counter.0.get(), 0);
    unsafe {
      routine.invoke();
    }
    assert_eq!(counter.0.get(), 1);
    unsafe {
      routine.invoke();
    }
    assert_eq!(counter.0.get(), -2);
    unsafe {
      routine.invoke();
    }
    assert_eq!(counter.0.get(), -2);
  }

  #[test]
  fn callback() {
    let mut routine = Routine::new();
    let counter = Arc::new(Counter(Cell::new(0)));
    let wrapper = Wrapper(Arc::clone(&counter));
    routine.push_callback(move || {
      (wrapper.0).0.set((wrapper.0).0.get() + 1);
    });
    assert_eq!(counter.0.get(), 0);
    unsafe {
      routine.invoke();
    }
    assert_eq!(counter.0.get(), -1);
    unsafe {
      routine.invoke();
    }
    assert_eq!(counter.0.get(), -1);
  }
}