thread.rs 4.33 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
#![allow(dead_code)]
qinsoon's avatar
qinsoon committed
2 3

use ast::ir::*;
qinsoon's avatar
qinsoon committed
4 5 6 7 8 9
use ast::ptr::*;
use ast::types::*;
use vm::VM;
use runtime::RuntimeValue;
use runtime::gc;

qinsoon's avatar
qinsoon committed
10
use utils::ByteSize;
qinsoon's avatar
qinsoon committed
11 12 13 14 15 16 17
use utils::Address;
use utils::mem::memmap;
use utils::mem::memsec;

use std::sync::Arc;
use std::thread;
use std::thread::JoinHandle;
qinsoon's avatar
qinsoon committed
18 19 20 21 22 23

pub const STACK_SIZE : ByteSize = (4 << 20); // 4mb

#[cfg(target_arch = "x86_64")]
pub const PAGE_SIZE  : ByteSize = (4 << 10); // 4kb

qinsoon's avatar
qinsoon committed
24 25 26
impl_mu_entity!(MuThread);
impl_mu_entity!(MuStack);

qinsoon's avatar
qinsoon committed
27
pub struct MuStack {
qinsoon's avatar
qinsoon committed
28
    pub hdr: MuEntityHeader,
qinsoon's avatar
qinsoon committed
29 30 31
    
    func_id: MuID,
    
qinsoon's avatar
qinsoon committed
32
    state: MuStackState,
qinsoon's avatar
qinsoon committed
33
    
qinsoon's avatar
qinsoon committed
34
    size: ByteSize,
qinsoon's avatar
qinsoon committed
35 36 37 38 39 40 41 42 43 44
    //    lo addr                                                    hi addr
    //     | overflow guard page | actual stack ..................... | underflow guard page|
    //     |                     |                                    |                     |
    // overflowGuard           lowerBound                           upperBound
    //                                                              underflowGuard    
    overflow_guard : Address,
    lower_bound    : Address,
    upper_bound    : Address,
    underflow_guard: Address,
    
qinsoon's avatar
qinsoon committed
45 46 47 48 49
    // this frame pointers should only be used when stack is not active
    sp : Address,
    bp : Address,
    ip : Address,
    
qinsoon's avatar
qinsoon committed
50 51 52 53 54 55 56
    exception_obj  : Option<Address>,
    
    #[allow(dead_code)]
    mmap           : memmap::Mmap
}

impl MuStack {
qinsoon's avatar
qinsoon committed
57
    pub fn new(id: MuID, func: &MuFunction) -> MuStack {
qinsoon's avatar
qinsoon committed
58 59 60 61 62 63 64
        let total_size = PAGE_SIZE * 2 + STACK_SIZE;
        
        let anon_mmap = match memmap::Mmap::anonymous(total_size, memmap::Protection::ReadWrite) {
            Ok(m) => m,
            Err(_) => panic!("failed to mmap for a stack"),
        };
        
qinsoon's avatar
qinsoon committed
65 66 67 68 69 70 71 72 73 74 75 76
        let mmap_start = Address::from_ptr(anon_mmap.ptr());
        debug_assert!(mmap_start.is_aligned_to(PAGE_SIZE));
        
        let overflow_guard = mmap_start;
        let lower_bound = mmap_start.plus(PAGE_SIZE);
        let upper_bound = lower_bound.plus(STACK_SIZE);
        let underflow_guard = upper_bound;
        
        unsafe {
            memsec::mprotect(overflow_guard.to_ptr_mut::<u8>(),  PAGE_SIZE, memsec::Prot::NoAccess);
            memsec::mprotect(underflow_guard.to_ptr_mut::<u8>(), PAGE_SIZE, memsec::Prot::NoAccess);
        }
qinsoon's avatar
qinsoon committed
77
        
qinsoon's avatar
qinsoon committed
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
        debug!("creating stack {} with entry func {:?}", id, func);
        debug!("overflow_guard : {}", overflow_guard);
        debug!("lower_bound    : {}", lower_bound);
        debug!("upper_bound    : {}", upper_bound);
        debug!("underflow_guard: {}", underflow_guard);
        
        MuStack {
            hdr: MuEntityHeader::unnamed(id),
            func_id: func.id(),
            
            state: MuStackState::Ready(func.sig.arg_tys.clone()),
            
            size: STACK_SIZE,
            overflow_guard: overflow_guard,
            lower_bound: lower_bound,
            upper_bound: upper_bound,
            underflow_guard: upper_bound,
            
            sp: upper_bound,
            bp: upper_bound,
            ip: unsafe {Address::zero()},
            
            exception_obj: None,
            
            mmap: anon_mmap
        }
qinsoon's avatar
qinsoon committed
104 105 106
    }
}

qinsoon's avatar
qinsoon committed
107 108 109 110 111 112
pub enum MuStackState {
    Ready(Vec<P<MuType>>), // ready to resume when values of given types are supplied (can be empty)
    Active,
    Dead
}

qinsoon's avatar
qinsoon committed
113
pub struct MuThread {
qinsoon's avatar
qinsoon committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    pub hdr: MuEntityHeader,
    allocator: Box<gc::Mutator>,
    stack: Option<Box<MuStack>>,
    
    user_tls: Option<Address>
}

impl MuThread {
    pub fn new(id: MuID, allocator: Box<gc::Mutator>, stack: Box<MuStack>, user_tls: Option<Address>) -> MuThread {
        MuThread {
            hdr: MuEntityHeader::unnamed(id),
            allocator: allocator,
            stack: Some(stack),
            user_tls: user_tls
        }
    }
qinsoon's avatar
qinsoon committed
130
    
qinsoon's avatar
qinsoon committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    pub fn launch(id: MuID, stack: Box<MuStack>, user_tls: Option<Address>, vals: Vec<RuntimeValue>, vm: &VM) -> JoinHandle<()> {
        match thread::Builder::new().name(format!("Mu Thread #{}", id)).spawn(move || {
            let mut muthread = Box::new(MuThread::new(id, gc::new_mutator(), stack, user_tls));
            MuThread::thread_entry(muthread, vm);
        }) {
            Ok(handle) => handle,
            Err(_) => panic!("failed to create a thread")
        }
    }
    
    /// entry function for launching a mu thread
    pub fn thread_entry(mu_thread: Box<MuThread>, vm: &VM) -> ! {
        
    }
qinsoon's avatar
qinsoon committed
145
}