• Stars
    star
    73
  • Rank 418,317 (Top 9 %)
  • Language
    Lua
  • License
    MIT License
  • Created over 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

`llthreads` library rewritten without `LuaNativeObjects` code generator

lua-llthreads2

Licence Build Status Build status

This is full dropin replacement for llthreads library.

Incompatibility list with origin llthreads library

  • does not support Lua 5.0
  • does not support ffi interface (use Lua C API for LuaJIT)
  • returns nil instead of false on error
  • start method returns self instead of true on success

Additional

  • thread:join() method support zero timeout to check if thread alive (does not work on Windows with pthreads)
  • thread:join() method support arbitrary timeout on Windows threads
  • thread:alive() method return whether the thread is alive (does not work on Windows with pthreads)
  • set_logger function allow logging errors (crash Lua VM) in current llthread's threads
  • thread:start() has additional parameter which control in which thread child Lua VM will be destroyed
  • allow pass cfunctions to child thread (e.g. to initialize Lua state)

Thread start arguments

detached joinable join returns child state closes by gc calls detach on
false false true child join <NEVER>
  false(*) true(*)  Lua values           parent       join * <NEVER>
  true     false(*) raise error         child         <NONE>   start  
true true true child detach gc

Usage

Use custom logger

In this example I use lua-log library.

-- This is child thread.
local llthreads = require "llthreads2"
-- Send logs using ZMQ
local LOG = require"log".new(
  require "log.writer.net.zmq".new("tcp://127.0.0.1:5555")
)
llthread.set_logger(function(msg) LOG.error(msg) end)
-- This error with traceback will be passed to logger
error("SOME ERROR")

Start attached thread collectd in child thread

-- This is main thread.
local thread = require "llthreads2".new[[
  require "utils".sleep(5)
]]

-- We tell that we start attached thread but child Lua State shuld be close in child thread. 
-- If `thread` became garbage in main thread then finallizer calls thread:join() 
-- and main thread may hungup. But because of child state closes in child thread all objects
-- in this state can be destroyed and we can prevent deadlock.
thread:start(false, false)

-- We can call join.
-- Because of Lua state destroys in child thread we can not get 
-- returned Lua values so we just returns `true`.
thread:join()

Start detached joinable thread

-- This is main thread.
local thread = require "llthreads2".new[[
  require "utils".sleep(5)
]]

-- We tell that we start detached joinable thread. In fact we start attached 
-- thread but if `thread` became garbage in main thread then finallizer just 
-- detach child thread and main thread may not hungup.
thread:start(true, true)

-- We can call join.
-- Because of Lua state destroys in child thread we can not get 
-- returned Lua values so we just returns `true`.
thread:join()

Pass to child thread host application`s library loader

If you close parent Lua state then some dynamic library may be unloaded and cfunction in child Lua state (thread) became invalid.

-- `myhost.XXX` modules is built-in modules in host application
-- host application registers cfunction as module loader
local preload = {}
preload[ 'myhost.logger' ] = package.preload[ 'myhost.logger' ]
preload[ 'myhost.config' ] = package.preload[ 'myhost.config' ]
llthreads.new([[
  -- registers preload
  local preload  = ...
  for name, fn in pairs(preload) do package.preload[name] = fn end

  local log = require 'myhost.logger'

]], preload):start(true)

Wait while thread is alive

local thread = require "llthreads2".new[[
  require "utils".sleep(5)
  return 1
]]
thread:start()

-- we can not use `thread:join(0)` because we can not call it twice
-- so all returned values will be lost
while thread:alive() do 
  -- do some work
end

local ok, ret = thread:join() -- true, 1

Use ex module

local Threads = require "llthreads2.ex"

local ok, v = Threads.new(function()
  return 1
end):start():join()
assert(v == 1)

local thread = Threads.new({
  -- this thread code gets changed arguments
  function(a, b)
    assert(1 == a)
    assert(2 == b)
    print("Done")
  end;
  
  -- prelude can change thread arguments
  prelude = function(a, b)
    assert("1" == a)
    assert(nil == b)
    return tonumber(a), 2
  end;
}, "1")

thread:start():join()

Bitdeli Badge

More Repositories

1

lua-log

Asynchronous logging library for Lua
Lua
107
star
2

lua-path

File system path manipulation library
Lua
72
star
3

lua-lluv

Lua binding to libuv
C
66
star
4

lua-travis-example

For experiments with travis-ci
Shell
54
star
5

luacov-coveralls

LuaCov reporter for coveralls.io service
Lua
48
star
6

lua-sendmail

Simple wrapper around luasoket smtp.send
CMake
30
star
7

lua-windows-environment

Windows Lua binaries with LuaRocks and some external dependencies
C
29
star
8

lua-vararg

Library for manipulation of variable arguements of functions
Lua
25
star
9

lua-lluv-curl

Make asyncronus requests using libuv and libcurl
Lua
20
star
10

lua-lluv-websocket

Websocket for lluv library
Lua
19
star
11

ZipWriter

Library for creating ZIP archive for Lua
Lua
18
star
12

lua-odbc

ODBC Library for lua
C
18
star
13

lua-AesFileEncrypt

A simple file encryption library
C
17
star
14

lua-socket-async

Asyncronus wrapper around LuaSocket library
Lua
17
star
15

vscode-mobdebug

MobDebug Debug Adapter for Visual Studio Code
Lua
16
star
16

lua-pop3

POP3 client library for Lua
Lua
15
star
17

lua-split

Implement functions to split strings
Lua
14
star
18

lzmq-zguide

Examples from ZeroMQ Guide
Lua
13
star
19

lua-lluv-gsmmodem

Control GSM modem connected to serial port using AT commands
Lua
12
star
20

lua-spylog

Execute actions based on log records
Lua
12
star
21

lua-sqlite3

fork from http://www.nessie.de/mroth/lua-sqlite3/index.html
Lua
11
star
22

lua-lluv-pegasus

Simple server based on pegasus.lua library
Lua
9
star
23

lua-winreg

Lua binary module to Access Microsoft(R) Windows(R) Registry
C
8
star
24

lua-EventEmitter

Implement EventEmitter on Lua
Lua
8
star
25

lua-voip

Lua
8
star
26

lua-lluv-rs232

Serial port communication library for lluv library
Lua
8
star
27

lua-pegasus-router

Router plugin for Pegasus server
Lua
8
star
28

lua-environ

Manipulate with environment variables
Lua
7
star
29

NginxWinService

Service wrappers for `nginx` and `php-cgi` for Windows
Lua
7
star
30

lua-tpdu

SMS TPDU encoder/decoder
Lua
6
star
31

lua-MultiRequests

Make multiple requests from different coroutines in parallel
Lua
6
star
32

lua-lluv-ssl

SSL/TLS sockets for lluv library
Lua
6
star
33

lua-lluv-ftp

FTP client for lluv library
Lua
6
star
34

lua-ftp

Simple wrapper around LuaSocket ftp
Lua
6
star
35

lua-null

Provide special value `null` for Lua
C
6
star
36

zbs-package

Packages for ZeroBraneStudio
Lua
6
star
37

lua-Parallel

Lua
5
star
38

lua-lluv-esl

FreeSWITCH ESL implementation for lluv library
Lua
5
star
39

lua-lluv-redis

Redis client for lua-lluv library
Lua
5
star
40

luasqlite3

fork from http://lua.sqlite.org
C
5
star
41

lua-lluv-pg

PostgreSQL client based on lluv library
Lua
5
star
42

lua-lzmq-zmq

Wrapper around lzmq library to be compatiable with lua-zmq library
Lua
4
star
43

lua-pdh

Lua binding to Microsoft Performance Data Helper (PDH) library.
C
4
star
44

lzmq-pool

ZMQ socket pool
Lua
4
star
45

lua-pegasus-websocket

WebSocket plugin for Pegasus http server
Lua
4
star
46

lzmq-auth

Implementaion of czmq zauth class
Lua
4
star
47

lua-try

Simple exception support based on LuaSocket
Lua
4
star
48

lua-bgcrypto-sha

C
4
star
49

lua-luq

Light userdata queue
CMake
4
star
50

lua-prefix_tree

Lua
4
star
51

lua-websockets-permessage-deflate

Implement permessage-deflate extension
Lua
3
star
52

lua-OpenHardwareMonitor

Access to OpenHardwareMonitor WMI interface
Lua
3
star
53

lua-lluv-poll-zmq

ZMQ poller for lluv library
Lua
3
star
54

lua-WindowsFirewall

Windows Firewall configuration library
Lua
3
star
55

libmemcached-win32

Fork of https://code.launchpad.net/~mattn/libmemcached/libmemcached-win32
C
3
star
56

lua-avro

Lua
3
star
57

lua-lluv-memcached

Memcached client for lua-lluv library
Lua
3
star
58

lua-lluv-qless

Lua binding for qless - queue / pipeline management system
Lua
3
star
59

luarocks-external

External deps to be able install some binary rocks on Windows.
C
2
star
60

lua-gntp

Implementation of Growl Notify Transport Protocol (GNTP) for Lua
Lua
2
star
61

lua-odbc-pool

ODBC connections pool
Lua
2
star
62

lzmq-monitor

Implementaion of czmq zsock_monitor class
Lua
2
star
63

lbase64

Unofficial repositary of lbase64
Shell
2
star
64

lua-bgcrypto-aes

C
2
star
65

lakefiles

my lakefile for some projects
Lua
1
star
66

moteus.github.com

HTML
1
star
67

lua-lluv-busted

Support async tests for busted with lluv library
Lua
1
star
68

lua-env

Lua library to manipulate environment variables
Lua
1
star
69

lua-odbc-dbi

ODBC Driver for Lua-DBI library
Lua
1
star
70

fusionpbx-app-messenger

Application for FusionPBX to send/receive SMS messages
PHP
1
star
71

lua-websockets-extensions

WebSocket extensions manager
Lua
1
star
72

mod_h323_deps

Build deps for FreeSWITCH mod_h323 on Windows
C++
1
star