Skip to content

Commit 717965d

Browse files
committed
Implement exec_nowait() to make totally async executions possible. Add stop_handler() for terminating the whole object & its thread.
1 parent aad33b7 commit 717965d

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lua_actor"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
license = "MIT"
55
authors = ["JunYi JohnTeee Lee <johnteee@gmail.com>"]
66
include = ["src/**/*.rs", "Cargo.toml"]

src/actor.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,26 @@ impl Default for Actor {
2727
impl Actor {
2828
pub fn new() -> Actor {
2929
let actor: Actor = Default::default();
30-
actor.start();
30+
actor.start_handler();
3131
actor
3232
}
3333
pub fn new_with_handler(handler: Option<Arc<Mutex<HandlerThread>>>) -> Actor {
3434
let mut actor: Actor = Default::default();
3535
actor.handler = handler;
36-
actor.start();
36+
actor.start_handler();
3737
actor
3838
}
3939

4040
pub fn lua(&self) -> Arc<Mutex<Lua>> {
4141
self.lua.clone()
4242
}
43-
fn start(&self) {
43+
pub fn stop_handler(&self) {
44+
match self.handler {
45+
Some(ref _h) => _h.lock().unwrap().stop(),
46+
None => {},
47+
}
48+
}
49+
fn start_handler(&self) {
4450
match self.handler {
4551
Some(ref _h) => _h.lock().unwrap().start(),
4652
None => {},
@@ -155,6 +161,21 @@ impl Actor {
155161
}
156162
}
157163
}
164+
pub fn exec_nowait(&self, source: &'static str, name: Option<&'static str>) -> Result<(), Error> {
165+
match self.handler.clone() {
166+
Some(_handler) => {
167+
let lua = self.lua.clone();
168+
_handler.lock().unwrap().post(RawFunc::new(move ||{
169+
let _ = Self::_exec(lua.clone(), source.clone(), name.clone());
170+
}));
171+
Ok(())
172+
},
173+
None => {
174+
Self::_exec(self.lua.clone(), source, name)?;
175+
Ok(())
176+
}
177+
}
178+
}
158179
fn _exec(lua: Arc<Mutex<Lua>>, source: &str, name: Option<&str>) -> Result<LuaMessage, Error> {
159180
let vm = lua.lock().unwrap();
160181
Ok(vm.exec(source, name)?)

0 commit comments

Comments
 (0)