diff options
author | osch <oliver at luced de> | 2018-08-21 20:44:03 +0200 |
---|---|---|
committer | osch <oliver at luced de> | 2018-08-21 20:46:45 +0200 |
commit | 535e00626b7e93b32d5a99638d784ede66313cf6 (patch) | |
tree | c801d12d27117ca1fee9295df33b5b05a21cd5ee | |
parent | e33999a890c8bfdb0c1f753820e4261dabb67faa (diff) | |
download | lua-llthreads2-535e00626b7e93b32d5a99638d784ede66313cf6.tar.gz lua-llthreads2-535e00626b7e93b32d5a99638d784ede66313cf6.tar.bz2 lua-llthreads2-535e00626b7e93b32d5a99638d784ede66313cf6.zip |
new method thread:interrupt()
-rw-r--r-- | src/llthread.c | 27 | ||||
-rw-r--r-- | src/lua/llthreads2/ex.lua | 9 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/llthread.c b/src/llthread.c index 3155113..c78d766 100644 --- a/src/llthread.c +++ b/src/llthread.c | |||
@@ -694,6 +694,32 @@ static int l_llthread_new(lua_State *L) { | |||
694 | return 1; | 694 | return 1; |
695 | } | 695 | } |
696 | 696 | ||
697 | static void llthread_interrupt1(lua_State *L, lua_Debug *ar) { | ||
698 | (void)ar; /* unused arg. */ | ||
699 | lua_sethook(L, NULL, 0, 0); /* reset hook */ | ||
700 | luaL_error(L, "interrupted!"); | ||
701 | } | ||
702 | static void llthread_interrupt2(lua_State *L, lua_Debug *ar) { | ||
703 | (void)ar; /* unused arg. */ | ||
704 | luaL_error(L, "interrupted!"); | ||
705 | } | ||
706 | |||
707 | static int l_llthread_interrupt(lua_State *L) { | ||
708 | llthread_t *this = l_llthread_at(L, 1); | ||
709 | llthread_child_t *child = this->child; | ||
710 | lua_Hook hook = llthread_interrupt1; | ||
711 | if (!lua_isnoneornil(L, 2)) | ||
712 | hook = lua_toboolean(L, 2) ? llthread_interrupt2 : NULL; | ||
713 | if (child) { | ||
714 | if (hook) | ||
715 | lua_sethook(child->L, hook, LUA_MASKCALL|LUA_MASKRET|LUA_MASKCOUNT, 1); | ||
716 | else | ||
717 | lua_sethook(child->L, NULL, 0, 0); /* reset hook */ | ||
718 | } | ||
719 | return 0; | ||
720 | } | ||
721 | |||
722 | |||
697 | static const struct luaL_Reg l_llthread_meth[] = { | 723 | static const struct luaL_Reg l_llthread_meth[] = { |
698 | {"start", l_llthread_start }, | 724 | {"start", l_llthread_start }, |
699 | {"join", l_llthread_join }, | 725 | {"join", l_llthread_join }, |
@@ -701,6 +727,7 @@ static const struct luaL_Reg l_llthread_meth[] = { | |||
701 | {"started", l_llthread_started }, | 727 | {"started", l_llthread_started }, |
702 | {"detached", l_llthread_detached }, | 728 | {"detached", l_llthread_detached }, |
703 | {"joinable", l_llthread_joinable }, | 729 | {"joinable", l_llthread_joinable }, |
730 | {"interrupt", l_llthread_interrupt }, | ||
704 | {"__gc", l_llthread_delete }, | 731 | {"__gc", l_llthread_delete }, |
705 | 732 | ||
706 | {NULL, NULL} | 733 | {NULL, NULL} |
diff --git a/src/lua/llthreads2/ex.lua b/src/lua/llthreads2/ex.lua index f0c27d9..b943f5b 100644 --- a/src/lua/llthreads2/ex.lua +++ b/src/lua/llthreads2/ex.lua | |||
@@ -141,6 +141,15 @@ function thread_mt:joinable() | |||
141 | return self.thread:joinable() | 141 | return self.thread:joinable() |
142 | end | 142 | end |
143 | 143 | ||
144 | --- Interrupt thread | ||
145 | -- The thread is interrupted by installing a debug hook that | ||
146 | -- creates an error. | ||
147 | -- @tparam ?boolean if not given, interrupt only once, | ||
148 | -- otherwise this arg sets or unsets permanent interrupt. | ||
149 | function thread_mt:interrupt(arg) | ||
150 | return self.thread:interrupt(arg) | ||
151 | end | ||
152 | |||
144 | end | 153 | end |
145 | ------------------------------------------------------------------------------- | 154 | ------------------------------------------------------------------------------- |
146 | 155 | ||