Commit 27886d4d authored by qinsoon's avatar qinsoon

[wip] weird rwlock problem

parent bcc804b9
......@@ -1119,6 +1119,7 @@ pub fn emit_context(vm: &VM) {
use std::path;
use std::fs::File;
use std::io::prelude::*;
use rustc_serialize::json;
debug!("---Emit VM Context---");
create_emit_directory();
......@@ -1132,32 +1133,36 @@ pub fn emit_context(vm: &VM) {
Ok(file) => file
};
// put globals into bss section
file.write_fmt(format_args!("\t.bss\n")).unwrap();
let globals = vm.globals().read().unwrap();
for global in globals.values() {
debug!("emit global: {}", global);
let (size, align) = {
let alloc_ty = {
match global.v {
Value_::Global(ref ty) => ty,
_ => panic!("expected a global")
}
{
// put globals into bss section
file.write_fmt(format_args!("\t.bss\n")).unwrap();
let globals = vm.globals().read().unwrap();
for global in globals.values() {
debug!("emit global: {}", global);
let (size, align) = {
let alloc_ty = {
match global.v {
Value_::Global(ref ty) => ty,
_ => panic!("expected a global")
}
};
debug!("getting type: {:?}", alloc_ty);
let ty_info = vm.get_backend_type_info(alloc_ty.id());
(ty_info.size, ty_info.alignment)
};
debug!("getting type: {:?}", alloc_ty);
let ty_info = vm.get_backend_type_info(alloc_ty.id());
(ty_info.size, ty_info.alignment)
};
file.write_fmt(format_args!("\t{}\n", directive_globl(symbol(global.name().unwrap())))).unwrap();
file.write_fmt(format_args!("\t{}\n", directive_comm(symbol(global.name().unwrap()), size, align))).unwrap();
file.write("\n".as_bytes()).unwrap();
file.write_fmt(format_args!("\t{}\n", directive_globl(symbol(global.name().unwrap())))).unwrap();
file.write_fmt(format_args!("\t{}\n", directive_comm(symbol(global.name().unwrap()), size, align))).unwrap();
file.write("\n".as_bytes()).unwrap();
}
}
// serialize vm
// unimplemented!();
file.write("\t.data\n".as_bytes()).unwrap();
file.write_fmt(format_args!("vm: .asciiz \"{}\"", json::encode(&vm).unwrap())).unwrap();
file.write("\n".as_bytes()).unwrap();
// main_thread
// let primordial = vm.primordial.read().unwrap();
......
......@@ -29,4 +29,8 @@ impl ValueLocation {
&ValueLocation::Relocatable(_, _) => panic!("expect a runtime value")
}
}
pub fn from_constant(c: Constant) -> ValueLocation {
unimplemented!()
}
}
\ No newline at end of file
......@@ -52,9 +52,6 @@ pub struct VM {
// partially serialize
// 12
compiled_funcs: RwLock<HashMap<MuID, RwLock<CompiledFunction>>>,
// no serialize
threads: RwLock<Vec<JoinHandle<()>>>,
}
const VM_SERIALIZE_FIELDS : usize = 12;
......@@ -252,8 +249,6 @@ impl Decodable for VM {
// not serialized
compiled_funcs: RwLock::new(HashMap::new()),
threads: RwLock::new(vec![])
};
vm.next_id.store(next_id, Ordering::SeqCst);
......@@ -285,7 +280,6 @@ impl <'a> VM {
funcs: RwLock::new(HashMap::new()),
compiled_funcs: RwLock::new(HashMap::new()),
threads: RwLock::new(vec!()),
primordial: RwLock::new(None)
};
......@@ -479,8 +473,7 @@ impl <'a> VM {
*guard = Some(MuPrimordialThread{func_id: func_id, args: args});
}
#[deprecated]
pub fn new_thread_normal(&self, mut stack: Box<MuStack>, threadlocal: Address, vals: Vec<ValueLocation>) {
pub fn new_thread_normal(&self, mut stack: Box<MuStack>, threadlocal: Address, vals: Vec<ValueLocation>) -> JoinHandle<()> {
let user_tls = {
if threadlocal.is_zero() {
None
......@@ -492,10 +485,7 @@ impl <'a> VM {
// set up arguments on stack
stack.runtime_load_args(vals);
let handle = MuThread::mu_thread_launch(self.next_id(), stack, user_tls, self);
let mut threads = self.threads.write().unwrap();
threads.push(handle);
MuThread::mu_thread_launch(self.next_id(), stack, user_tls, self)
}
#[allow(unused_variables)]
......@@ -508,12 +498,28 @@ impl <'a> VM {
}
#[no_mangle]
pub extern fn mu_primorial_main(serialized_vm : *const c_char, len: usize) {
pub extern fn mu_main(serialized_vm : *const c_char, len: usize) {
use rustc_serialize::json;
let str_vm = unsafe {String::from_raw_parts(serialized_vm as *mut u8, len, len)};
let vm : Arc<VM> = Arc::new(json::decode(&str_vm).unwrap());
unimplemented!()
let primordial = vm.primordial.read().unwrap();
if primordial.is_none() {
panic!("no primordial thread/stack/function. Client should provide an entry point");
} else {
let primordial = primordial.as_ref().unwrap();
// create mu stack
let stack = vm.new_stack(primordial.func_id);
let args : Vec<ValueLocation> = primordial.args.iter().map(|arg| ValueLocation::from_constant(arg.clone())).collect();
// FIXME: currently assumes no user defined thread local
// will need to fix this after we can serialize heap object
let thread = vm.new_thread_normal(stack, unsafe{Address::zero()}, args);
thread.join().unwrap();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment