Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(const_fn)]
- type Link = Option<Box<Thread>>;
- #[derive(Debug)]
- struct Thread {
- next: Link,
- name: u32,
- }
- impl Thread {
- pub fn new(name: u32) -> Thread {
- Thread {
- next: None,
- name: name,
- }
- }
- }
- #[derive(Debug)]
- struct Scheduler {
- head: Link,
- }
- impl Scheduler {
- pub const fn new() -> Scheduler{
- Scheduler{
- head: None,
- }
- }
- pub fn push_thread(&mut self, mut thread: Box<Thread>) {
- let previous_head = self.head.take();
- if let Some(node) = previous_head {
- thread.next = Some(node);
- }
- self.head = Some(thread);
- }
- pub fn pop_thread(&mut self) -> Option<Box<Thread>> {
- //let previous_head = std::mem::replace(&mut self.head, None);
- let previous_head = self.head.take();
- if let Some(mut node) = previous_head {
- self.head = node.next.take();
- Some(node)
- } else {
- None
- }
- }
- }
- use spin::Mutex;
- static SCHED: Mutex<Scheduler> = Mutex::new(Scheduler::new());
- fn test_static_scope_push() {
- let mut s = SCHED.lock();
- let t1 = Box::new(Thread::new(1));
- let t2 = Box::new(Thread::new(2));
- s.push_thread(t1);
- s.push_thread(t2);
- }
- fn test_static_scope_pop() {
- let mut s = SCHED.lock();
- println!("{:?}", s.pop_thread());
- println!("{:?}", s.pop_thread());
- println!("{:?}", s.pop_thread());
- }
- fn main() {
- test_static_scope_push();
- test_static_scope_pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement