diff options
| author | Alexey Melnichuk <mimir@newmail.ru> | 2014-06-23 11:20:46 +0500 |
|---|---|---|
| committer | Alexey Melnichuk <mimir@newmail.ru> | 2014-06-23 11:20:46 +0500 |
| commit | 161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2 (patch) | |
| tree | 1e936a80d88d0e764dba93c646f3a80ecdc57d85 | |
| parent | 6cae0b69e644b7e6395bd8774dfbb838f985c439 (diff) | |
| download | lua-llthreads2-161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2.tar.gz lua-llthreads2-161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2.tar.bz2 lua-llthreads2-161ec7edd08ab4f0ffffefa81c75e37c1a89ecd2.zip | |
Update doc
| -rw-r--r-- | README.md | 28 | ||||
| -rw-r--r-- | src/lua/llthreads2/ex.lua | 38 |
2 files changed, 61 insertions, 5 deletions
| @@ -119,5 +119,33 @@ end | |||
| 119 | local ok, ret = thread:join() -- true, 1 | 119 | local ok, ret = thread:join() -- true, 1 |
| 120 | ``` | 120 | ``` |
| 121 | 121 | ||
| 122 | ### Use `ex` module | ||
| 123 | ``` Lua | ||
| 124 | local Threads = require "llthreads.ex" | ||
| 125 | |||
| 126 | local ok, v = Threads.new(function() | ||
| 127 | return 1 | ||
| 128 | end):start():join() | ||
| 129 | assert(v == 1) | ||
| 130 | |||
| 131 | local 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 | |||
| 147 | thread:start():join() | ||
| 148 | ``` | ||
| 149 | |||
| 122 | [](https://bitdeli.com/free "Bitdeli Badge") | 150 | [](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 | |||
| 95 | local thread_mt = {} do | 95 | local thread_mt = {} do |
| 96 | thread_mt.__index = thread_mt | 96 | thread_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 | ||
| 98 | function thread_mt:start(...) | 107 | function 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 |
| 102 | end | 111 | end |
| 103 | 112 | ||
| 113 | --- Join thread. | ||
| 114 | -- | ||
| 115 | -- @tparam ?number timeout Windows suppurts arbitrary value, but POSIX supports only 0 | ||
| 104 | function thread_mt:join(...) | 116 | function thread_mt:join(...) |
| 105 | return self.thread:join(...) | 117 | return self.thread:join(...) |
| 106 | end | 118 | end |
| 107 | 119 | ||
| 120 | --- Check if thread still working. | ||
| 121 | -- You can call `join` to get returned values if thiread is not alive. | ||
| 108 | function thread_mt:alive() | 122 | function thread_mt:alive() |
| 109 | return self.thread:alive() | 123 | return self.thread:alive() |
| 110 | end | 124 | end |
| @@ -115,7 +129,7 @@ end | |||
| 115 | ------------------------------------------------------------------------------- | 129 | ------------------------------------------------------------------------------- |
| 116 | local threads = {} do | 130 | local threads = {} do |
| 117 | 131 | ||
| 118 | local function new_thread(prelude, lua_init, code, ...) | 132 | local 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) |
| 135 | end | 149 | end |
| 136 | 150 | ||
| 151 | --- Create new thread object | ||
| 152 | -- | ||
| 153 | -- @tparam string|function|THREAD_OPTIONS source thread source code. | ||
| 154 | -- | ||
| 137 | threads.new = function (code, ...) | 155 | threads.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, ...) |
| 147 | end | 165 | end |
| 148 | 166 | ||
| 149 | end | 167 | end |
| 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 | |||
| 152 | return threads \ No newline at end of file | 180 | return threads \ No newline at end of file |
