Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #![feature(const_fn)]
  2.  
  3. type Link = Option<Box<Thread>>;
  4.  
  5. #[derive(Debug)]
  6. struct Thread {
  7. next: Link,
  8. name: u32,
  9. }
  10.  
  11. impl Thread {
  12. pub fn new(name: u32) -> Thread {
  13. Thread {
  14. next: None,
  15. name: name,
  16. }
  17. }
  18.  
  19. }
  20.  
  21. #[derive(Debug)]
  22. struct Scheduler {
  23. head: Link,
  24. }
  25.  
  26. impl Scheduler {
  27. pub const fn new() -> Scheduler{
  28. Scheduler{
  29. head: None,
  30. }
  31. }
  32.  
  33. pub fn push_thread(&mut self, mut thread: Box<Thread>) {
  34.  
  35. let previous_head = self.head.take();
  36.  
  37. if let Some(node) = previous_head {
  38. thread.next = Some(node);
  39. }
  40. self.head = Some(thread);
  41. }
  42.  
  43. pub fn pop_thread(&mut self) -> Option<Box<Thread>> {
  44. //let previous_head = std::mem::replace(&mut self.head, None);
  45. let previous_head = self.head.take();
  46. if let Some(mut node) = previous_head {
  47. self.head = node.next.take();
  48. Some(node)
  49. } else {
  50. None
  51. }
  52. }
  53. }
  54.  
  55. use spin::Mutex;
  56.  
  57. static SCHED: Mutex<Scheduler> = Mutex::new(Scheduler::new());
  58.  
  59. fn test_static_scope_push() {
  60.  
  61. let mut s = SCHED.lock();
  62. let t1 = Box::new(Thread::new(1));
  63. let t2 = Box::new(Thread::new(2));
  64.  
  65. s.push_thread(t1);
  66. s.push_thread(t2);
  67. }
  68.  
  69. fn test_static_scope_pop() {
  70. let mut s = SCHED.lock();
  71.  
  72. println!("{:?}", s.pop_thread());
  73. println!("{:?}", s.pop_thread());
  74. println!("{:?}", s.pop_thread());
  75. }
  76.  
  77.  
  78. fn main() {
  79.  
  80. test_static_scope_push();
  81. test_static_scope_pop();
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement