Fixed error in green threads example. See: https://github.com/cfsamson/example-greenthreads/issues/20. Ugh... But it works now!
This commit is contained in:
@@ -149,6 +149,7 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
|
||||
```rust, edition2018
|
||||
# #![feature(llvm_asm, naked_functions)]
|
||||
# use std::ptr;
|
||||
#
|
||||
# const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
|
||||
# const MAX_THREADS: usize = 4;
|
||||
@@ -171,6 +172,7 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# stack: Vec<u8>,
|
||||
# ctx: ThreadContext,
|
||||
# state: State,
|
||||
# task: Option<Box<dyn Fn()>>,
|
||||
# }
|
||||
#
|
||||
# #[derive(Debug, Default)]
|
||||
@@ -183,6 +185,7 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# r12: u64,
|
||||
# rbx: u64,
|
||||
# rbp: u64,
|
||||
# thread_ptr: u64,
|
||||
# }
|
||||
#
|
||||
# impl Thread {
|
||||
@@ -192,6 +195,7 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# stack: vec![0_u8; DEFAULT_STACK_SIZE],
|
||||
# ctx: ThreadContext::default(),
|
||||
# state: State::Available,
|
||||
# task: None,
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
@@ -203,9 +207,11 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# stack: vec![0_u8; DEFAULT_STACK_SIZE],
|
||||
# ctx: ThreadContext::default(),
|
||||
# state: State::Running,
|
||||
# task: None,
|
||||
# };
|
||||
#
|
||||
# let mut threads = vec![base_thread];
|
||||
# threads[0].ctx.thread_ptr = &threads[0] as *const Thread as u64;
|
||||
# let mut available_threads: Vec<Thread> = (1..MAX_THREADS).map(|i| Thread::new(i)).collect();
|
||||
# threads.append(&mut available_threads);
|
||||
#
|
||||
@@ -263,28 +269,39 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# );
|
||||
# switch();
|
||||
# }
|
||||
# self.threads.len() > 0
|
||||
# true
|
||||
# }
|
||||
#
|
||||
# pub fn spawn(&mut self, f: fn()) {
|
||||
# let available = self
|
||||
# pub fn spawn<F: Fn() + 'static>(f: F){
|
||||
# unsafe {
|
||||
# let rt_ptr = RUNTIME as *mut Runtime;
|
||||
# let available = (*rt_ptr)
|
||||
# .threads
|
||||
# .iter_mut()
|
||||
# .find(|t| t.state == State::Available)
|
||||
# .expect("no available thread.");
|
||||
#
|
||||
# let size = available.stack.len();
|
||||
# unsafe {
|
||||
# let s_ptr = available.stack.as_mut_ptr().offset(size as isize);
|
||||
# let s_ptr = (s_ptr as usize & !15) as *mut u8;
|
||||
# available.task = Some(Box::new(f));
|
||||
# available.ctx.thread_ptr = available as *const Thread as u64;
|
||||
# //ptr::write(s_ptr.offset((size - 8) as isize) as *mut u64, guard as u64);
|
||||
# std::ptr::write(s_ptr.offset(-16) as *mut u64, guard as u64);
|
||||
# std::ptr::write(s_ptr.offset(-24) as *mut u64, skip as u64);
|
||||
# std::ptr::write(s_ptr.offset(-32) as *mut u64, f as u64);
|
||||
# std::ptr::write(s_ptr.offset(-32) as *mut u64, call as u64);
|
||||
# available.ctx.rsp = s_ptr.offset(-32) as u64;
|
||||
# }
|
||||
# available.state = State::Ready;
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# fn call(thread: u64) {
|
||||
# let thread = unsafe { &*(thread as *const Thread) };
|
||||
# if let Some(f) = &thread.task {
|
||||
# f();
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# #[naked]
|
||||
# fn skip() { }
|
||||
@@ -292,7 +309,9 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# fn guard() {
|
||||
# unsafe {
|
||||
# let rt_ptr = RUNTIME as *mut Runtime;
|
||||
# (*rt_ptr).t_return();
|
||||
# let rt = &mut *rt_ptr;
|
||||
# println!("THREAD {} FINISHED.", rt.threads[rt.current].id);
|
||||
# rt.t_return();
|
||||
# };
|
||||
# }
|
||||
#
|
||||
@@ -322,6 +341,7 @@ _**Press the expand icon in the top right corner to show the example code.**_
|
||||
# mov 0x20(%rsi), %r12
|
||||
# mov 0x28(%rsi), %rbx
|
||||
# mov 0x30(%rsi), %rbp
|
||||
# mov 0x38(%rsi), %rdi
|
||||
# "
|
||||
# );
|
||||
# }
|
||||
|
||||
Reference in New Issue
Block a user