diff options
author | Alexey Melnichuk <mimir@newmail.ru> | 2014-06-25 10:11:17 +0500 |
---|---|---|
committer | Alexey Melnichuk <mimir@newmail.ru> | 2014-06-25 10:11:17 +0500 |
commit | 3de8f797d0e235efd20fdc9c55c8068893f4fd03 (patch) | |
tree | e995e196db486bab6edfd6023c1e41bd1875caf8 | |
parent | 04dce92542c727041ae042ede83a94e0e5e5a99f (diff) | |
download | lua-llthreads2-3de8f797d0e235efd20fdc9c55c8068893f4fd03.tar.gz lua-llthreads2-3de8f797d0e235efd20fdc9c55c8068893f4fd03.tar.bz2 lua-llthreads2-3de8f797d0e235efd20fdc9c55c8068893f4fd03.zip |
Add. started/detached/joinable methods to thread object.
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | lakefile | 1 | ||||
-rw-r--r-- | src/llthread.c | 25 | ||||
-rw-r--r-- | src/lua/llthreads2/ex.lua | 18 | ||||
-rw-r--r-- | test/test_threads_attr.lua | 21 |
5 files changed, 64 insertions, 2 deletions
diff --git a/.travis.yml b/.travis.yml index febe50b..f7842a6 100644 --- a/.travis.yml +++ b/.travis.yml | |||
@@ -37,6 +37,7 @@ script: | |||
37 | - lua$LUA_SFX test_threads_ex.lua 10 | 37 | - lua$LUA_SFX test_threads_ex.lua 10 |
38 | - lua$LUA_SFX test_threads_ex_arg.lua | 38 | - lua$LUA_SFX test_threads_ex_arg.lua |
39 | - lua$LUA_SFX test_threads_ex_opt.lua | 39 | - lua$LUA_SFX test_threads_ex_opt.lua |
40 | - lua$LUA_SFX test_threads_attr.lua | ||
40 | 41 | ||
41 | notifications: | 42 | notifications: |
42 | email: | 43 | email: |
@@ -41,6 +41,7 @@ target('test', install, function() | |||
41 | run_test('test_threads_ex.lua 10') | 41 | run_test('test_threads_ex.lua 10') |
42 | run_test('test_threads_ex_arg.lua') | 42 | run_test('test_threads_ex_arg.lua') |
43 | run_test('test_threads_ex_opt.lua') | 43 | run_test('test_threads_ex_opt.lua') |
44 | run_test('test_threads_attr.lua') | ||
44 | 45 | ||
45 | 46 | ||
46 | if not test_summary() then | 47 | 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 @@ | |||
4 | 4 | ||
5 | #define LLTHREAD_VERSION_MAJOR 0 | 5 | #define LLTHREAD_VERSION_MAJOR 0 |
6 | #define LLTHREAD_VERSION_MINOR 1 | 6 | #define LLTHREAD_VERSION_MINOR 1 |
7 | #define LLTHREAD_VERSION_PATCH 0 | 7 | #define LLTHREAD_VERSION_PATCH 2 |
8 | #define LLTHREAD_VERSION_COMMENT "" | 8 | #define LLTHREAD_VERSION_COMMENT "dev" |
9 | 9 | ||
10 | #ifndef USE_PTHREAD | 10 | #ifndef USE_PTHREAD |
11 | # include <windows.h> | 11 | # include <windows.h> |
@@ -660,6 +660,24 @@ static int l_llthread_alive(lua_State *L) { | |||
660 | 660 | ||
661 | } | 661 | } |
662 | 662 | ||
663 | static int l_llthread_started(lua_State *L) { | ||
664 | llthread_t *this = l_llthread_at(L, 1); | ||
665 | lua_pushboolean(L, IS(this, STARTED)?1:0); | ||
666 | return 1; | ||
667 | } | ||
668 | |||
669 | static int l_llthread_detached(lua_State *L) { | ||
670 | llthread_t *this = l_llthread_at(L, 1); | ||
671 | lua_pushboolean(L, IS(this, DETACHED)?1:0); | ||
672 | return 1; | ||
673 | } | ||
674 | |||
675 | static int l_llthread_joinable(lua_State *L) { | ||
676 | llthread_t *this = l_llthread_at(L, 1); | ||
677 | lua_pushboolean(L, IS(this, JOINABLE)?1:0); | ||
678 | return 1; | ||
679 | } | ||
680 | |||
663 | static int l_llthread_new(lua_State *L) { | 681 | static int l_llthread_new(lua_State *L) { |
664 | size_t lua_code_len; const char *lua_code = luaL_checklstring(L, 1, &lua_code_len); | 682 | size_t lua_code_len; const char *lua_code = luaL_checklstring(L, 1, &lua_code_len); |
665 | llthread_t **this = lutil_newudatap(L, llthread_t*, LLTHREAD_TAG); | 683 | llthread_t **this = lutil_newudatap(L, llthread_t*, LLTHREAD_TAG); |
@@ -674,6 +692,9 @@ static const struct luaL_Reg l_llthread_meth[] = { | |||
674 | {"start", l_llthread_start }, | 692 | {"start", l_llthread_start }, |
675 | {"join", l_llthread_join }, | 693 | {"join", l_llthread_join }, |
676 | {"alive", l_llthread_alive }, | 694 | {"alive", l_llthread_alive }, |
695 | {"started", l_llthread_started }, | ||
696 | {"detached", l_llthread_detached }, | ||
697 | {"joinable", l_llthread_joinable }, | ||
677 | {"__gc", l_llthread_delete }, | 698 | {"__gc", l_llthread_delete }, |
678 | 699 | ||
679 | {NULL, NULL} | 700 | {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() | |||
123 | return self.thread:alive() | 123 | return self.thread:alive() |
124 | end | 124 | end |
125 | 125 | ||
126 | --- Check if thread was started. | ||
127 | -- | ||
128 | function thread_mt:started() | ||
129 | return self.thread:started() | ||
130 | end | ||
131 | |||
132 | --- Check if thread is detached. | ||
133 | -- This function returns valid value only for started thread. | ||
134 | function thread_mt:detached() | ||
135 | return self.thread:detached() | ||
136 | end | ||
137 | |||
138 | --- Check if thread is joinable. | ||
139 | -- This function returns valid value only for started thread. | ||
140 | function thread_mt:joinable() | ||
141 | return self.thread:joinable() | ||
142 | end | ||
143 | |||
126 | end | 144 | end |
127 | ------------------------------------------------------------------------------- | 145 | ------------------------------------------------------------------------------- |
128 | 146 | ||
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 @@ | |||
1 | local llthreads = require"llthreads.ex" | ||
2 | |||
3 | local thread = llthreads.new(function() return 1 end) | ||
4 | |||
5 | assert(not thread:started()) | ||
6 | |||
7 | -- thread is not started so this is not valid values | ||
8 | assert(not thread:detached()) | ||
9 | assert(not thread:joinable()) | ||
10 | |||
11 | assert(thread:start(true, true)) | ||
12 | |||
13 | assert(thread:detached()) | ||
14 | assert(thread:joinable()) | ||
15 | |||
16 | assert(thread:join()) | ||
17 | |||
18 | assert(thread:started()) | ||
19 | assert(not thread:alive()) | ||
20 | |||
21 | print("done!") \ No newline at end of file | ||