From 3de8f797d0e235efd20fdc9c55c8068893f4fd03 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 25 Jun 2014 10:11:17 +0500 Subject: Add. started/detached/joinable methods to thread object. --- .travis.yml | 1 + lakefile | 1 + src/llthread.c | 25 +++++++++++++++++++++++-- src/lua/llthreads2/ex.lua | 18 ++++++++++++++++++ test/test_threads_attr.lua | 21 +++++++++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test/test_threads_attr.lua diff --git a/.travis.yml b/.travis.yml index febe50b..f7842a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,6 +37,7 @@ script: - lua$LUA_SFX test_threads_ex.lua 10 - lua$LUA_SFX test_threads_ex_arg.lua - lua$LUA_SFX test_threads_ex_opt.lua + - lua$LUA_SFX test_threads_attr.lua notifications: email: diff --git a/lakefile b/lakefile index c6db75b..9df24ac 100644 --- a/lakefile +++ b/lakefile @@ -41,6 +41,7 @@ target('test', install, function() run_test('test_threads_ex.lua 10') run_test('test_threads_ex_arg.lua') run_test('test_threads_ex_opt.lua') + run_test('test_threads_attr.lua') if not test_summary() then diff --git a/src/llthread.c b/src/llthread.c index 0ffa727..33eab4f 100644 --- a/src/llthread.c +++ b/src/llthread.c @@ -4,8 +4,8 @@ #define LLTHREAD_VERSION_MAJOR 0 #define LLTHREAD_VERSION_MINOR 1 -#define LLTHREAD_VERSION_PATCH 0 -#define LLTHREAD_VERSION_COMMENT "" +#define LLTHREAD_VERSION_PATCH 2 +#define LLTHREAD_VERSION_COMMENT "dev" #ifndef USE_PTHREAD # include @@ -660,6 +660,24 @@ static int l_llthread_alive(lua_State *L) { } +static int l_llthread_started(lua_State *L) { + llthread_t *this = l_llthread_at(L, 1); + lua_pushboolean(L, IS(this, STARTED)?1:0); + return 1; +} + +static int l_llthread_detached(lua_State *L) { + llthread_t *this = l_llthread_at(L, 1); + lua_pushboolean(L, IS(this, DETACHED)?1:0); + return 1; +} + +static int l_llthread_joinable(lua_State *L) { + llthread_t *this = l_llthread_at(L, 1); + lua_pushboolean(L, IS(this, JOINABLE)?1:0); + return 1; +} + static int l_llthread_new(lua_State *L) { size_t lua_code_len; const char *lua_code = luaL_checklstring(L, 1, &lua_code_len); llthread_t **this = lutil_newudatap(L, llthread_t*, LLTHREAD_TAG); @@ -674,6 +692,9 @@ static const struct luaL_Reg l_llthread_meth[] = { {"start", l_llthread_start }, {"join", l_llthread_join }, {"alive", l_llthread_alive }, + {"started", l_llthread_started }, + {"detached", l_llthread_detached }, + {"joinable", l_llthread_joinable }, {"__gc", l_llthread_delete }, {NULL, NULL} diff --git a/src/lua/llthreads2/ex.lua b/src/lua/llthreads2/ex.lua index 06af3a5..77f8d5e 100644 --- a/src/lua/llthreads2/ex.lua +++ b/src/lua/llthreads2/ex.lua @@ -123,6 +123,24 @@ function thread_mt:alive() return self.thread:alive() end +--- Check if thread was started. +-- +function thread_mt:started() + return self.thread:started() +end + +--- Check if thread is detached. +-- This function returns valid value only for started thread. +function thread_mt:detached() + return self.thread:detached() +end + +--- Check if thread is joinable. +-- This function returns valid value only for started thread. +function thread_mt:joinable() + return self.thread:joinable() +end + end ------------------------------------------------------------------------------- diff --git a/test/test_threads_attr.lua b/test/test_threads_attr.lua new file mode 100644 index 0000000..fbcb036 --- /dev/null +++ b/test/test_threads_attr.lua @@ -0,0 +1,21 @@ +local llthreads = require"llthreads.ex" + +local thread = llthreads.new(function() return 1 end) + +assert(not thread:started()) + +-- thread is not started so this is not valid values +assert(not thread:detached()) +assert(not thread:joinable()) + +assert(thread:start(true, true)) + +assert(thread:detached()) +assert(thread:joinable()) + +assert(thread:join()) + +assert(thread:started()) +assert(not thread:alive()) + +print("done!") \ No newline at end of file -- cgit v1.2.3-55-g6feb