diff options
Diffstat (limited to 'test/test_llthreads.lua')
| -rw-r--r-- | test/test_llthreads.lua | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/test/test_llthreads.lua b/test/test_llthreads.lua new file mode 100644 index 0000000..3474b9b --- /dev/null +++ b/test/test_llthreads.lua | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | -- Copyright (c) 2011 by Robert G. Jakabosky <bobby@sharedrealm.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 | local llthreads = require"llthreads" | ||
| 22 | |||
| 23 | local sleep | ||
| 24 | local status, socket = pcall(require,"socket") | ||
| 25 | if status then | ||
| 26 | sleep = function(secs) | ||
| 27 | return socket.sleep(secs) | ||
| 28 | end | ||
| 29 | end | ||
| 30 | |||
| 31 | if not sleep then | ||
| 32 | local status, ztimer = pcall(require, "lzmq.timer") | ||
| 33 | if status then | ||
| 34 | sleep = function(secs) | ||
| 35 | ztimer.sleep(secs * 1000) | ||
| 36 | end | ||
| 37 | end | ||
| 38 | end | ||
| 39 | |||
| 40 | if not sleep then | ||
| 41 | sleep = function(secs) | ||
| 42 | os.execute("sleep " .. tonumber(secs)) | ||
| 43 | end | ||
| 44 | end | ||
| 45 | |||
| 46 | local function detached_thread(...) | ||
| 47 | local thread = llthreads.new([[ print("print_detached_thread:", ...) ]], ...) | ||
| 48 | -- start detached thread | ||
| 49 | assert(thread:start(true)) | ||
| 50 | return thread | ||
| 51 | end | ||
| 52 | |||
| 53 | local function print_thread(...) | ||
| 54 | local thread = llthreads.new([[ print("print_thread:", ...); ]], ...) | ||
| 55 | -- start joinable thread | ||
| 56 | assert(thread:start()) | ||
| 57 | return thread | ||
| 58 | end | ||
| 59 | |||
| 60 | local function pass_through_thread(...) | ||
| 61 | local thread = llthreads.new([[ return "pass_thread:", ... ]], ...) | ||
| 62 | -- start joinable thread | ||
| 63 | assert(thread:start()) | ||
| 64 | return thread | ||
| 65 | end | ||
| 66 | |||
| 67 | local thread1 = detached_thread("number:", 1234, "nil:", nil, "bool:", true) | ||
| 68 | |||
| 69 | sleep(1) | ||
| 70 | |||
| 71 | local thread2 = print_thread("number:", 1234, "nil:", nil, "bool:", true) | ||
| 72 | print("thread2:join: results # = ", select('#', thread2:join())) | ||
| 73 | |||
| 74 | sleep(1) | ||
| 75 | |||
| 76 | local thread3 = pass_through_thread("number:", 1234, "nil:", nil, "bool:", true) | ||
| 77 | print("thread3:join:", thread3:join()) | ||
| 78 | |||
| 79 | sleep(1) | ||
| 80 | |||
