diff options
Diffstat (limited to 'llthreads2/README.md')
-rw-r--r-- | llthreads2/README.md | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/llthreads2/README.md b/llthreads2/README.md new file mode 100644 index 0000000..22f2025 --- /dev/null +++ b/llthreads2/README.md | |||
@@ -0,0 +1,113 @@ | |||
1 | lua-llthreads2 | ||
2 | ============== | ||
3 | [](https://travis-ci.org/moteus/lua-llthreads2) | ||
4 | [](https://buildhive.cloudbees.com/job/moteus/job/lua-llthreads2/) | ||
5 | [](https://moteus.ci.cloudbees.com/job/lua-llthreads2/) | ||
6 | |||
7 | This is full dropin replacement for [llthreads](https://github.com/Neopallium/lua-llthreads) library. | ||
8 | |||
9 | ##Incompatibility list with origin llthreads library | ||
10 | * does not support Lua 5.0 | ||
11 | * does not support ffi interface (use Lua C API for LuaJIT) | ||
12 | * returns nil instead of false on error | ||
13 | * start method returns self instead of true on success | ||
14 | |||
15 | ##Additional | ||
16 | * thread:join() method support zero timeout to check if thread alive (does not work on Windows with pthreads) | ||
17 | * thread:join() method support arbitrary timeout on Windows threads | ||
18 | * thread:alive() method return whether the thread is alive (does not work on Windows with pthreads) | ||
19 | * set_logger function allow logging errors (crash Lua VM) in current llthread's threads | ||
20 | * thread:start() has additional parameter which control in which thread child Lua VM will be destroyed | ||
21 | * allow pass cfunctions to child thread (e.g. to initialize Lua state) | ||
22 | |||
23 | ##Usage | ||
24 | |||
25 | ### Use custom logger | ||
26 | In this example I use [lua-log](https://github.com/moteus/lua-log) library. | ||
27 | ``` Lua | ||
28 | -- This is child thread. | ||
29 | local llthreads = require "llthreads" | ||
30 | -- Send logs using ZMQ | ||
31 | local LOG = require"log".new( | ||
32 | require "log.writer.net.zmq".new("tcp://127.0.0.1:5555") | ||
33 | ) | ||
34 | llthread.set_logger(function(msg) LOG.error(msg) end) | ||
35 | -- This error with traceback will be passed to logger | ||
36 | error("SOME ERROR") | ||
37 | ``` | ||
38 | |||
39 | ### Start attached thread collectd in child thread | ||
40 | ``` Lua | ||
41 | -- This is main thread. | ||
42 | local thread = require "llthreads".new[[ | ||
43 | require "utils".sleep(5) | ||
44 | ]] | ||
45 | |||
46 | -- We tell that we start attached thread but child Lua State shuld be close in child thread. | ||
47 | -- If `thread` became garbage in main thread then finallizer calls thread:join() | ||
48 | -- and main thread may hungup. | ||
49 | thread:start(false, false) | ||
50 | |||
51 | -- We can call join. | ||
52 | -- Because of Lua state destroys in child thread we can not get | ||
53 | -- returned Lua vaules so we just returns `true`. | ||
54 | thread:join() | ||
55 | ``` | ||
56 | |||
57 | ### Start detached joinable thread | ||
58 | ``` Lua | ||
59 | -- This is main thread. | ||
60 | local thread = require "llthreads".new[[ | ||
61 | require "utils".sleep(5) | ||
62 | ]] | ||
63 | |||
64 | -- We tell that we start detached joinable thread. In fact we start attached | ||
65 | -- thread but if `thread` became garbage in main thread then finallizer just | ||
66 | -- detach child thread and main thread may not hungup. | ||
67 | thread:start(true, true) | ||
68 | |||
69 | -- We can call join. | ||
70 | -- Because of Lua state destroys in child thread we can not get | ||
71 | -- returned Lua vaules so we just returns `true`. | ||
72 | thread:join() | ||
73 | ``` | ||
74 | |||
75 | ### Pass to child thread host application`s library loader | ||
76 | If you close parent Lua state then some dynamic library may be unloaded | ||
77 | and cfunction in child Lua state (thread) became invalid. | ||
78 | |||
79 | ``` Lua | ||
80 | -- `myhost.XXX` modules is built-in modules in host application | ||
81 | -- host application registers cfunction as module loader | ||
82 | local preload = {} | ||
83 | preload[ 'myhost.logger' ] = package.preload[ 'myhost.logger' ] | ||
84 | preload[ 'myhost.config' ] = package.preload[ 'myhost.config' ] | ||
85 | llthreads.new([[ | ||
86 | -- registers preload | ||
87 | local preload = ... | ||
88 | for name, fn in pairs(preload) do package.preload[name] = fn end | ||
89 | |||
90 | local log = require 'myhost.logger' | ||
91 | |||
92 | ]], preload):start(true) | ||
93 | ``` | ||
94 | |||
95 | ### Wait while thread is alive | ||
96 | ``` Lua | ||
97 | local thread = require "llthreads".new[[ | ||
98 | require "utils".sleep(5) | ||
99 | return 1 | ||
100 | ]] | ||
101 | thread:start() | ||
102 | |||
103 | -- we can not use `thread:join(0)` because we can not call it twice | ||
104 | -- so all returned vaules will be lost | ||
105 | while thread:alive() do | ||
106 | -- do some work | ||
107 | end | ||
108 | |||
109 | local ok, ret = thread:join() -- true, 1 | ||
110 | ``` | ||
111 | |||
112 | [](https://bitdeli.com/free "Bitdeli Badge") | ||
113 | |||