aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Melnichuk <mimir@newmail.ru>2014-06-23 09:40:01 +0500
committerAlexey Melnichuk <mimir@newmail.ru>2014-06-23 09:40:01 +0500
commitf0a6754cd15912de529b9662e3e08bfaadd6bf1e (patch)
tree49da84a4d154dfd4771093cfd9aaacc47036ca78
parented9ed9a1c794c0d49f83cb00a557ae6085bf47e4 (diff)
downloadlua-llthreads2-f0a6754cd15912de529b9662e3e08bfaadd6bf1e.tar.gz
lua-llthreads2-f0a6754cd15912de529b9662e3e08bfaadd6bf1e.tar.bz2
lua-llthreads2-f0a6754cd15912de529b9662e3e08bfaadd6bf1e.zip
Add. `llthreads2.ex` module.
-rw-r--r--.travis.yml5
-rw-r--r--lakefile6
-rw-r--r--rockspecs/lua-llthreads2-compat-scm-0.rockspec3
-rw-r--r--rockspecs/lua-llthreads2-scm-0.rockspec3
-rw-r--r--src/lua/llthreads2/ex.lua152
-rw-r--r--test/test_threads_ex.lua64
-rw-r--r--test/test_threads_ex_arg.lua26
-rw-r--r--test/test_threads_ex_opt.lua31
8 files changed, 286 insertions, 4 deletions
diff --git a/.travis.yml b/.travis.yml
index 5b72267..2b95fdd 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -25,7 +25,7 @@ script:
25 - cd test 25 - cd test
26 - lua$LUA_SFX test_table_copy.lua 26 - lua$LUA_SFX test_table_copy.lua
27 - lua$LUA_SFX test_threads.lua 27 - lua$LUA_SFX test_threads.lua
28 - lua$LUA_SFX test_llthreads.lua 28 - lua$LUA_SFX test_llthreads.lua 10
29 # - lua$LUA_SFX test_register_llthreads.lua 29 # - lua$LUA_SFX test_register_llthreads.lua
30 - lua$LUA_SFX test_join_timeout.lua 30 - lua$LUA_SFX test_join_timeout.lua
31 - lua$LUA_SFX test_join_detach.lua 31 - lua$LUA_SFX test_join_detach.lua
@@ -35,6 +35,9 @@ script:
35 - lua$LUA_SFX test_pass_cfunction.lua 35 - lua$LUA_SFX test_pass_cfunction.lua
36 - lua$LUA_SFX test_load_llthreads2.lua 36 - lua$LUA_SFX test_load_llthreads2.lua
37 - lua$LUA_SFX test_alive.lua 37 - lua$LUA_SFX test_alive.lua
38 - lua$LUA_SFX test_threads_ex.lua 10
39 - lua$LUA_SFX test_threads_ex_arg.lua
40 - lua$LUA_SFX test_threads_ex_opt.lua
38 41
39notifications: 42notifications:
40 email: 43 email:
diff --git a/lakefile b/lakefile
index 64bd3fa..1f54996 100644
--- a/lakefile
+++ b/lakefile
@@ -29,7 +29,7 @@ target('test', install, function()
29 run_test('test_join_timeout.lua') 29 run_test('test_join_timeout.lua')
30 run_test('test_llthreads.lua') 30 run_test('test_llthreads.lua')
31 run_test('test_table_copy.lua') 31 run_test('test_table_copy.lua')
32 run_test('test_threads.lua') 32 run_test('test_threads.lua 10')
33 run_test('test_join_timeout.lua') 33 run_test('test_join_timeout.lua')
34 run_test('test_join_detach.lua') 34 run_test('test_join_detach.lua')
35 run_test('test_join_error.lua') 35 run_test('test_join_error.lua')
@@ -37,6 +37,10 @@ target('test', install, function()
37 run_test('test_logger.lua') 37 run_test('test_logger.lua')
38 run_test('test_pass_cfunction.lua') 38 run_test('test_pass_cfunction.lua')
39 run_test('test_alive.lua') 39 run_test('test_alive.lua')
40 run_test('test_threads_ex.lua 10')
41 run_test('test_threads_ex_arg.lua')
42 run_test('test_threads_ex_opt.lua')
43
40 44
41 if not test_summary() then 45 if not test_summary() then
42 quit("test fail") 46 quit("test fail")
diff --git a/rockspecs/lua-llthreads2-compat-scm-0.rockspec b/rockspecs/lua-llthreads2-compat-scm-0.rockspec
index ac81a15..67a062d 100644
--- a/rockspecs/lua-llthreads2-compat-scm-0.rockspec
+++ b/rockspecs/lua-llthreads2-compat-scm-0.rockspec
@@ -39,6 +39,7 @@ build = {
39 llthreads = { 39 llthreads = {
40 sources = { "src/l52util.c", "src/llthread.c" }, 40 sources = { "src/l52util.c", "src/llthread.c" },
41 defines = { "LLTHREAD_MODULE_NAME=llthreads" }, 41 defines = { "LLTHREAD_MODULE_NAME=llthreads" },
42 } 42 },
43 ["llthreads2.ex"] = "src/lua/llthreads2/ex.lua",
43 } 44 }
44} \ No newline at end of file 45} \ No newline at end of file
diff --git a/rockspecs/lua-llthreads2-scm-0.rockspec b/rockspecs/lua-llthreads2-scm-0.rockspec
index 87f9b15..0795eca 100644
--- a/rockspecs/lua-llthreads2-scm-0.rockspec
+++ b/rockspecs/lua-llthreads2-scm-0.rockspec
@@ -39,6 +39,7 @@ build = {
39 llthreads2 = { 39 llthreads2 = {
40 sources = { "src/l52util.c", "src/llthread.c" }, 40 sources = { "src/l52util.c", "src/llthread.c" },
41 defines = { "LLTHREAD_MODULE_NAME=llthreads2" }, 41 defines = { "LLTHREAD_MODULE_NAME=llthreads2" },
42 } 42 },
43 ["llthreads2.ex"] = "src/lua/llthreads2/ex.lua",
43 } 44 }
44} \ No newline at end of file 45} \ No newline at end of file
diff --git a/src/lua/llthreads2/ex.lua b/src/lua/llthreads2/ex.lua
new file mode 100644
index 0000000..26e1a46
--- /dev/null
+++ b/src/lua/llthreads2/ex.lua
@@ -0,0 +1,152 @@
1--
2-- wraps the low-level threads object.
3--
4
5--
6-- Note! Define this function prior all `local` definitions
7-- to prevent use upvalue by accident
8--
9local bootstrap_code = require"string".dump(function(lua_init, prelude, code, ...)
10 local loadstring = loadstring or load
11 local unpack = table.unpack or unpack
12
13 local function load_src(str)
14 local f, n
15 if str:sub(1,1) == '@' then
16 n = str:sub(2)
17 f = assert(loadfile(n))
18 else
19 n = '=(loadstring)'
20 f = assert(loadstring(str))
21 end
22 return f, n
23 end
24
25 local function pack_n(...)
26 return { n = select("#", ...), ... }
27 end
28
29 local function unpack_n(t)
30 return unpack(t, 1, t.n)
31 end
32
33 if lua_init and #lua_init > 0 then
34 local init = load_src(lua_init)
35 init()
36 end
37
38 local args
39
40 if prelude and #prelude > 0 then
41 prelude = load_src(prelude)
42 args = pack_n(prelude(...))
43 else
44 args = pack_n(...)
45 end
46
47 local func
48 func, args[0] = load_src(code)
49
50 _G.arg = args
51 arg = args
52
53 return func(unpack_n(args))
54end)
55
56local ok, llthreads = pcall(require, "llthreads2")
57if not ok then llthreads = require"llthreads" end
58
59local os = require"os"
60local string = require"string"
61local table = require"table"
62
63local setmetatable, tonumber, assert = setmetatable, tonumber, assert
64
65-------------------------------------------------------------------------------
66local LUA_INIT = "LUA_INIT" do
67
68local lua_version_t
69local function lua_version()
70 if not lua_version_t then
71 local version = assert(_G._VERSION)
72 local maj,min = version:match("^Lua (%d+)%.(%d+)$")
73 if maj then lua_version_t = {tonumber(maj),tonumber(min)}
74 elseif not math.mod then lua_version_t = {5,2}
75 elseif table.pack and not pack then lua_version_t = {5,2}
76 else lua_version_t = {5,2} end
77 end
78 return lua_version_t[1], lua_version_t[2]
79end
80
81local LUA_MAJOR, LUA_MINOR = lua_version()
82local IS_LUA_51 = (LUA_MAJOR == 5) and (LUA_MINOR == 1)
83
84local LUA_INIT_VER
85if not IS_LUA_51 then
86 LUA_INIT_VER = LUA_INIT .. "_" .. LUA_MAJOR .. "_" .. LUA_MINOR
87end
88
89LUA_INIT = LUA_INIT_VER and os.getenv( LUA_INIT_VER ) or os.getenv( LUA_INIT ) or ""
90
91end
92-------------------------------------------------------------------------------
93
94-------------------------------------------------------------------------------
95local thread_mt = {} do
96thread_mt.__index = thread_mt
97
98function thread_mt:start(...)
99 local ok, err = self.thread:start(...)
100 if not ok then return nil, err end
101 return self
102end
103
104function thread_mt:join(...)
105 return self.thread:join(...)
106end
107
108function thread_mt:alive()
109 return self.thread:alive()
110end
111
112end
113-------------------------------------------------------------------------------
114
115-------------------------------------------------------------------------------
116local threads = {} do
117
118local function new_thread(prelude, lua_init, code, ...)
119 if type(lua_init) == "function" then
120 lua_init = string.dump(lua_init)
121 end
122
123 if type(prelude) == "function" then
124 prelude = string.dump(prelude)
125 end
126
127 if type(code) == "function" then
128 code = string.dump(code)
129 end
130
131 local thread = llthreads.new(bootstrap_code, lua_init, prelude, code, ...)
132 return setmetatable({
133 thread = thread,
134 }, thread_mt)
135end
136
137threads.new = function (code, ...)
138 assert(code)
139
140 if type(code) == "table" then
141 local source = assert(code.source or code[1])
142 local init = (code.lua_init == nil) and LUA_INIT or code.lua_init
143 return new_thread(code.prelude, init, source, ...)
144 end
145
146 return new_thread(nil, LUA_INIT, code, ...)
147end
148
149end
150-------------------------------------------------------------------------------
151
152return threads \ No newline at end of file
diff --git a/test/test_threads_ex.lua b/test/test_threads_ex.lua
new file mode 100644
index 0000000..3a6cc41
--- /dev/null
+++ b/test/test_threads_ex.lua
@@ -0,0 +1,64 @@
1-- Copyright (c) 2011 by Ross Anderson <ross_j_anderson@yahoo.com>
2--
3-- Permission is hereby granted, free of charge, to any person obtaining a copy
4-- of this software and associated documentation files (the "Software"), to deal
5-- in the Software without restriction, including without limitation the rights
6-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7-- copies of the Software, and to permit persons to whom the Software is
8-- furnished to do so, subject to the following conditions:
9--
10-- The above copyright notice and this permission notice shall be included in
11-- all copies or substantial portions of the Software.
12--
13-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19-- THE SOFTWARE.
20
21-- Sub-thread processing example in Lua using llthreads - 1,000 quick sub-thread execution
22
23-- luajit sub_threads.lua
24
25-- level 0 string literal enclosure [[ ]] of child execution code
26local thread_code = function(num_threads, ...)
27 print("CHILD: received from ROOT params:", num_threads, ...)
28 local llthreads = require"llthreads.ex" -- need to re-declare this under this scope
29 local t = {} -- thread storage table
30
31 -- create a new child sub-thread execution code - it requires level 1 literal string [=[ ]=] enclosures, level 2 would be [==[ ]==]
32 local executed_child_code = function(...)
33 return "Hello from child sub-thread, new input params:", ...
34 end
35
36 -- create 1000 sub-threads - which creates an incremental 30% / 20% utilization spike on the two AMD cpu cores
37 print("CHILD: Create sub threads:", num_threads)
38 for i=1,num_threads do
39 -- create child sub-thread with code to execute and the input parmeters
40 local thread = llthreads.new(executed_child_code , "number:", 1000 + i, "nil:", nil, "bool:", true)
41 assert(thread:start()) -- start new child sub-thread
42 table.insert(t, thread) -- append the thread at the end of the thread table
43 end
44
45 -- wait (block) for all child sub-threads to complete before returning to ROOT
46 while true do
47 -- always wait on the first element, since order is not important
48 print("CHILD: sub-thread returned: ", t[1]:join())
49 table.remove(t,1) -- always remove the first element
50 if (#t == 0) then break end
51 end
52 return ... -- return the parents' input params back to the root
53end
54
55local llthreads = require"llthreads.ex"
56
57local num_threads = tonumber(arg[1] or 1000)
58
59-- create child thread.
60local thread = llthreads.new(thread_code, num_threads, "number:", 1000, "nil:", nil, "bool:", true)
61-- start joinable child thread.
62assert(thread:start())
63-- wait for all child and child sub-threads to finish
64print("ROOT: child returned: ", thread:join())
diff --git a/test/test_threads_ex_arg.lua b/test/test_threads_ex_arg.lua
new file mode 100644
index 0000000..44d3dd8
--- /dev/null
+++ b/test/test_threads_ex_arg.lua
@@ -0,0 +1,26 @@
1local thread_code = function(...)
2 local function assert_equal(name, a, b, ...)
3 if a == b then return b, ... end
4 print(name .. " Fail! Expected `" .. tostring(a) .. "` got `" .. tostring(b) .. "`")
5 os.exit(1)
6 end
7
8 local a,b,c,d,e,f = ...
9 assert_equal("1:", 1 , a )
10 assert_equal("2:", nil , b )
11 assert_equal("3:", 'hello' , c )
12 assert_equal("4:", nil , d )
13 assert_equal("5:", 2 , e )
14 assert_equal("6:", nil , f )
15 assert_equal("#:", 6 , select("#", ...))
16end
17
18local llthreads = require"llthreads.ex"
19
20local thread = llthreads.new(thread_code, 1, nil, 'hello', nil, 2, nil)
21
22assert(thread:start())
23
24assert(thread:join())
25
26print("done!") \ No newline at end of file
diff --git a/test/test_threads_ex_opt.lua b/test/test_threads_ex_opt.lua
new file mode 100644
index 0000000..2e1d715
--- /dev/null
+++ b/test/test_threads_ex_opt.lua
@@ -0,0 +1,31 @@
1local thread_code = function(...)
2 local function assert_equal(name, a, b, ...)
3 if a == b then return b, ... end
4 print(name .. " Fail! Expected `" .. tostring(a) .. "` got `" .. tostring(b) .. "`")
5 os.exit(1)
6 end
7
8 local a,b,c,d,e,f = ...
9 assert_equal("1:", 1 , a )
10 assert_equal("2:", nil , b )
11 assert_equal("3:", 'hello' , c )
12 assert_equal("4:", nil , d )
13 assert_equal("5:", 2 , e )
14 assert_equal("6:", nil , f )
15 assert_equal("#:", 6 , select("#", ...))
16end
17
18local llthreads = require"llthreads.ex"
19
20-- pass `prelude` function that change thread arguments
21local thread = llthreads.new({thread_code, prelude = function(...)
22 return 1, nil, 'hello', ...
23end}, nil, 2, nil)
24
25local a = assert(thread:start())
26
27assert(thread:join())
28
29assert(a == thread)
30
31print("done!") \ No newline at end of file