summaryrefslogtreecommitdiff
path: root/test/test_llthreads.lua
diff options
context:
space:
mode:
authormoteus <mimir@newmail.ru>2013-12-26 12:00:41 +0400
committermoteus <mimir@newmail.ru>2013-12-26 12:00:41 +0400
commitfebf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354 (patch)
tree735ef634f8ebeab2101aa21897fd4e4e2f826f32 /test/test_llthreads.lua
parent46ed59584e5407c49a02f1ea6bede6487259a92e (diff)
downloadlua-llthreads2-febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354.tar.gz
lua-llthreads2-febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354.tar.bz2
lua-llthreads2-febf9da1af8ba4cf0f8cc64b6af2adb0dcf9b354.zip
First commit.
Diffstat (limited to 'test/test_llthreads.lua')
-rw-r--r--test/test_llthreads.lua80
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
21local llthreads = require"llthreads"
22
23local sleep
24local status, socket = pcall(require,"socket")
25if status then
26 sleep = function(secs)
27 return socket.sleep(secs)
28 end
29end
30
31if 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
38end
39
40if not sleep then
41 sleep = function(secs)
42 os.execute("sleep " .. tonumber(secs))
43 end
44end
45
46local function detached_thread(...)
47 local thread = llthreads.new([[ print("print_detached_thread:", ...) ]], ...)
48 -- start detached thread
49 assert(thread:start(true))
50 return thread
51end
52
53local function print_thread(...)
54 local thread = llthreads.new([[ print("print_thread:", ...); ]], ...)
55 -- start joinable thread
56 assert(thread:start())
57 return thread
58end
59
60local function pass_through_thread(...)
61 local thread = llthreads.new([[ return "pass_thread:", ... ]], ...)
62 -- start joinable thread
63 assert(thread:start())
64 return thread
65end
66
67local thread1 = detached_thread("number:", 1234, "nil:", nil, "bool:", true)
68
69sleep(1)
70
71local thread2 = print_thread("number:", 1234, "nil:", nil, "bool:", true)
72print("thread2:join: results # = ", select('#', thread2:join()))
73
74sleep(1)
75
76local thread3 = pass_through_thread("number:", 1234, "nil:", nil, "bool:", true)
77print("thread3:join:", thread3:join())
78
79sleep(1)
80