diff options
| author | Alexey Melnichuk <mimir@newmail.ru> | 2014-06-23 09:40:01 +0500 |
|---|---|---|
| committer | Alexey Melnichuk <mimir@newmail.ru> | 2014-06-23 09:40:01 +0500 |
| commit | f0a6754cd15912de529b9662e3e08bfaadd6bf1e (patch) | |
| tree | 49da84a4d154dfd4771093cfd9aaacc47036ca78 /test | |
| parent | ed9ed9a1c794c0d49f83cb00a557ae6085bf47e4 (diff) | |
| download | lua-llthreads2-f0a6754cd15912de529b9662e3e08bfaadd6bf1e.tar.gz lua-llthreads2-f0a6754cd15912de529b9662e3e08bfaadd6bf1e.tar.bz2 lua-llthreads2-f0a6754cd15912de529b9662e3e08bfaadd6bf1e.zip | |
Add. `llthreads2.ex` module.
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_threads_ex.lua | 64 | ||||
| -rw-r--r-- | test/test_threads_ex_arg.lua | 26 | ||||
| -rw-r--r-- | test/test_threads_ex_opt.lua | 31 |
3 files changed, 121 insertions, 0 deletions
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 | ||
| 26 | local 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 | ||
| 53 | end | ||
| 54 | |||
| 55 | local llthreads = require"llthreads.ex" | ||
| 56 | |||
| 57 | local num_threads = tonumber(arg[1] or 1000) | ||
| 58 | |||
| 59 | -- create child thread. | ||
| 60 | local thread = llthreads.new(thread_code, num_threads, "number:", 1000, "nil:", nil, "bool:", true) | ||
| 61 | -- start joinable child thread. | ||
| 62 | assert(thread:start()) | ||
| 63 | -- wait for all child and child sub-threads to finish | ||
| 64 | print("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 @@ | |||
| 1 | local 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("#", ...)) | ||
| 16 | end | ||
| 17 | |||
| 18 | local llthreads = require"llthreads.ex" | ||
| 19 | |||
| 20 | local thread = llthreads.new(thread_code, 1, nil, 'hello', nil, 2, nil) | ||
| 21 | |||
| 22 | assert(thread:start()) | ||
| 23 | |||
| 24 | assert(thread:join()) | ||
| 25 | |||
| 26 | print("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 @@ | |||
| 1 | local 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("#", ...)) | ||
| 16 | end | ||
| 17 | |||
| 18 | local llthreads = require"llthreads.ex" | ||
| 19 | |||
| 20 | -- pass `prelude` function that change thread arguments | ||
| 21 | local thread = llthreads.new({thread_code, prelude = function(...) | ||
| 22 | return 1, nil, 'hello', ... | ||
| 23 | end}, nil, 2, nil) | ||
| 24 | |||
| 25 | local a = assert(thread:start()) | ||
| 26 | |||
| 27 | assert(thread:join()) | ||
| 28 | |||
| 29 | assert(a == thread) | ||
| 30 | |||
| 31 | print("done!") \ No newline at end of file | ||
