diff options
Diffstat (limited to 'src/lua/llthreads2/ex.lua')
| -rw-r--r-- | src/lua/llthreads2/ex.lua | 38 |
1 files changed, 33 insertions, 5 deletions
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 |
