aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Melnichuk <mimir@newmail.ru>2014-06-23 11:20:46 +0500
committerAlexey Melnichuk <mimir@newmail.ru>2014-06-23 11:20:46 +0500
commit161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2 (patch)
tree1e936a80d88d0e764dba93c646f3a80ecdc57d85
parent6cae0b69e644b7e6395bd8774dfbb838f985c439 (diff)
downloadlua-llthreads2-161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2.tar.gz
lua-llthreads2-161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2.tar.bz2
lua-llthreads2-161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2.zip
Update doc
-rw-r--r--README.md28
-rw-r--r--src/lua/llthreads2/ex.lua38
2 files changed, 61 insertions, 5 deletions
diff --git a/README.md b/README.md
index 58e78eb..3d723da 100644
--- a/README.md
+++ b/README.md
@@ -119,5 +119,33 @@ end
119local ok, ret = thread:join() -- true, 1 119local ok, ret = thread:join() -- true, 1
120``` 120```
121 121
122### Use `ex` module
123``` Lua
124local Threads = require "llthreads.ex"
125
126local ok, v = Threads.new(function()
127 return 1
128end):start():join()
129assert(v == 1)
130
131local thread = Threads.new({
132 -- this is thread code gets changed arguments
133 function(a, b)
134 assert(1 == a)
135 assert(2 == b)
136 print("Done")
137 end;
138
139 -- prelude can change thread arguments
140 prelude = function(a, b)
141 assert("1" == a)
142 assert(nil == b)
143 return tonumber(a), 2
144 end;
145}, "1")
146
147thread:start():join()
148```
149
122[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-llthreads2/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 150[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-llthreads2/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
123 151
diff --git a/src/lua/llthreads2/ex.lua b/src/lua/llthreads2/ex.lua
index 26e1a46..bd8888d 100644
--- a/src/lua/llthreads2/ex.lua
+++ b/src/lua/llthreads2/ex.lua
@@ -1,6 +1,6 @@
1--- Wraps the low-level threads object.
1-- 2--
2-- wraps the low-level threads object. 3-- @module llthreads2.ex
3--
4 4
5-- 5--
6-- Note! Define this function prior all `local` definitions 6-- Note! Define this function prior all `local` definitions
@@ -95,16 +95,30 @@ end
95local thread_mt = {} do 95local thread_mt = {} do
96thread_mt.__index = thread_mt 96thread_mt.__index = thread_mt
97 97
98--- Thread object.
99--
100-- @type thread
101
102--- Start thread.
103--
104-- @tparam ?boolean detached
105-- @tparam ?boolean joinable
106-- @return self
98function thread_mt:start(...) 107function thread_mt:start(...)
99 local ok, err = self.thread:start(...) 108 local ok, err = self.thread:start(...)
100 if not ok then return nil, err end 109 if not ok then return nil, err end
101 return self 110 return self
102end 111end
103 112
113--- Join thread.
114--
115-- @tparam ?number timeout Windows suppurts arbitrary value, but POSIX supports only 0
104function thread_mt:join(...) 116function thread_mt:join(...)
105 return self.thread:join(...) 117 return self.thread:join(...)
106end 118end
107 119
120--- Check if thread still working.
121-- You can call `join` to get returned values if thiread is not alive.
108function thread_mt:alive() 122function thread_mt:alive()
109 return self.thread:alive() 123 return self.thread:alive()
110end 124end
@@ -115,7 +129,7 @@ end
115------------------------------------------------------------------------------- 129-------------------------------------------------------------------------------
116local threads = {} do 130local threads = {} do
117 131
118local function new_thread(prelude, lua_init, code, ...) 132local function new_thread(lua_init, prelude, code, ...)
119 if type(lua_init) == "function" then 133 if type(lua_init) == "function" then
120 lua_init = string.dump(lua_init) 134 lua_init = string.dump(lua_init)
121 end 135 end
@@ -134,19 +148,33 @@ local function new_thread(prelude, lua_init, code, ...)
134 }, thread_mt) 148 }, thread_mt)
135end 149end
136 150
151--- Create new thread object
152--
153-- @tparam string|function|THREAD_OPTIONS source thread source code.
154--
137threads.new = function (code, ...) 155threads.new = function (code, ...)
138 assert(code) 156 assert(code)
139 157
140 if type(code) == "table" then 158 if type(code) == "table" then
141 local source = assert(code.source or code[1]) 159 local source = assert(code.source or code[1])
142 local init = (code.lua_init == nil) and LUA_INIT or code.lua_init 160 local init = (code.lua_init == nil) and LUA_INIT or code.lua_init
143 return new_thread(code.prelude, init, source, ...) 161 return new_thread(init, code.prelude, source, ...)
144 end 162 end
145 163
146 return new_thread(nil, LUA_INIT, code, ...) 164 return new_thread(LUA_INIT, nil, code, ...)
147end 165end
148 166
149end 167end
150------------------------------------------------------------------------------- 168-------------------------------------------------------------------------------
151 169
170--- A table describe threads constructor options.
171--
172-- @tfield string|function source thread source code (or first value of table)
173-- @tfield ?string|function prelude thread prelude code. This code can change thread arguments.
174-- e.g. it can remove some values or change their type.
175-- @lua_init ?string|function|false by default child lua state try use LUA_INIT environment variable
176-- just like regular lua interpretator.
177--
178-- @table THREAD_OPTIONS
179
152return threads \ No newline at end of file 180return threads \ No newline at end of file