Skip to content

Commit

Permalink
优化了虚拟机工厂
Browse files Browse the repository at this point in the history
  • Loading branch information
zuon committed Jun 5, 2018
1 parent c6527d3 commit 3186d1c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 93 deletions.
62 changes: 0 additions & 62 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ rand = "*"
magnetic = "*"
threadpool = "*"
lazy_static = "*"
kernel32-sys = "*"
pi_lib = { path = "../pi_lib" }
pi_db = { path = "../pi_db" }
kernel32-sys = "*"
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ extern crate lazy_static;
#[cfg(not(unix))]
extern crate kernel32;

extern crate pi_lib;
extern crate pi_db;

pub mod adapter;
pub mod util;
pub mod worker;
Expand Down
32 changes: 7 additions & 25 deletions src/pi_vm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ use magnetic::mpmc::*;
use magnetic::buffer::dynamic::DynamicBuffer;
use magnetic::{Producer, Consumer};

use pi_lib::atom::Atom;
use pi_db::mgr::Mgr;

use task::TaskType;
use task_pool::TaskPool;
use adapter::{JSStatus, JS, try_js_destroy, dukc_vm_status_check, dukc_vm_status_switch, dukc_vm_status_sub, dukc_wakeup, dukc_continue, js_reply_callback};
Expand Down Expand Up @@ -73,36 +70,28 @@ impl VMFactory {
}

//从虚拟机池中获取一个虚拟机,并调用指定的js全局函数
pub fn call(&self, uid: u32, mgr: Mgr, topic: Atom, payload: Arc<Vec<u8>>, info: &'static str) {
pub fn call(&self, uid: u32, args: Box<FnBox(JS) -> JS>, info: &'static str) {
match self.consumer.try_pop() {
Err(_) => {
//没有空闲虚拟机,则立即构建临时虚拟机
match self.new_vm() {
None => (),
Some(vm) => {
Some(mut vm) => {
let func = Box::new(move || {
//TODO js全局函数还未确定,且参数列表也未确定
vm.get_js_function("call".to_string());
vm.new_str((*topic).clone());
let array = vm.new_uint8_array(payload.len() as u32);
array.from_bytes(payload.as_slice());
vm.new_native_object(Arc::into_raw(Arc::new(mgr)) as usize);
vm.call(3);
vm = args(vm);
vm.call(4);
});
cast_task(TaskType::Sync, 5000000000 + uid as u64, func, info);
}
}
}
Ok(vm) => {
Ok(mut vm) => {
let producer = self.producer.clone();
let func = Box::new(move || {
//TODO js全局函数还未确定,且参数列表也未确定
vm.get_js_function("call".to_string());
vm.new_str((*topic).clone());
let array = vm.new_uint8_array(payload.len() as u32);
array.from_bytes(payload.as_slice());
vm.new_native_object(Arc::into_raw(Arc::new(mgr)) as usize);
vm.call(3);
vm = args(vm);
vm.call(4);
//调用完成后复用虚拟机
match producer.try_push(vm) {
Err(_) => (),
Expand Down Expand Up @@ -134,13 +123,6 @@ impl VMFactory {
}
}

/*
* Topic处理器
*/
pub struct TopicHandler {
len: AtomicUsize,
}

/*
* 线程安全的向任务池投递任务
*/
Expand Down

0 comments on commit 3186d1c

Please sign in to comment.