• Stars
    star
    139
  • Rank 261,418 (Top 6 %)
  • Language
    Lua
  • License
    Other
  • Created about 11 years ago
  • Updated about 11 years ago

Reviews

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

Repository Details

Go style channels in pure Lua

Build Status

Lua-Channels

Go style Channels for Lua

This code is derived from libtask library by Russ Cox, mainly from channel.c. Semantically channels as implemented here are quite similar to channels from the Go language.

Usage

This is an example of using an unbuffered channel:

local task = require('lua-channels')

local function counter(channel)
   local i = 1
   while true do
       channel:send(i)
       i = i + 1
   end
end

local function main()
    local channel = task.Channel:new()
    task.spawn(counter, channel)
    assert(channel:recv() == 1)
    assert(channel:recv() == 2)
    assert(channel:recv() == 3)
end

task.spawn(main)
task.scheduler()

lua-channels exposes:

  • task.spawn(fun, [...]) - run fun as a coroutine with given parameters. You should use this instead of coroutine.create()

  • task.scheduler() - can be run only from the main thread, executes all the stuff, resumes the coroutines that are blocked on channels that became available. You can only do non-blocking sends / receives from the main thread.

  • task.Channel:new([buffer size]) - create a new channel with given size

  • task.chanalt(alts, can_block) - run alt / select / multiplex over the alts structure. For example:

  • task.chanalt({{c = channel_1, op = task.RECV}, {c = channel_2, op = task.SEND, p = "hello"}}, true)

This will block current coroutine until it's possible to receive from channel_1 or send to channel_2. chanalt returns a number of statement from alts that succeeded (1 or 2 here) and a received value if executed statement was RECV.

Finally, if two alt statements can be fulfilled at the same time, we use math.random() to decide which one should go first. So it makes sense to initialize seed with something random. If you don't have access to an entropy source you can do:

  math.randomseed(os.time())

but beware, the results of random() will predictable to a attacker.

Installing

You may simply require src/lua-channels.lua from the source, or install lua-channels from luarocks.

More Repositories

1

fluxcapacitor

The engine that powers DeLorean!
C
819
star
2

dump

Unfinished projects and snippets
PHP
485
star
3

openonload

git import of openonload.org https://gist.github.com/majek/ae188ae72e63470652c9
C
228
star
4

puka

Puka - the opinionated RabbitMQ client
Python
180
star
5

goplayground

Go
55
star
6

csiphash

SipHash in C
C
47
star
7

pysiphash

SipHash in Python
Python
38
star
8

inet-tool

inet-tool - the manager for BPF_PROG_TYPE_INET_LOOKUP eBPF programs
C
25
star
9

ixgbe

git clone of intel-wired-lan ixgbe drivers https://gist.github.com/majek/9d4910c24ccbbe237025
C
19
star
10

libtask

Shameless copy of http://swtch.com/libtask/ and/or http://code.google.com/p/libtask/
C
16
star
11

rats

A clone of NATS using RabbitMQ.
Ruby
11
star
12

vx32example

Simplistic example of vx32 lib usage
C
8
star
13

baby-steps-in-assembly

Assembly
8
star
14

sockjs-client

Repo migrated: https://github.com/sockjs/sockjs-client
8
star
15

django-sockjs

Simplistic example of SockJS-tornado used with Django
Python
7
star
16

mmuniq

Streaming tool to filter out duplicate lines. "uniq" implemented with bloom filter.
C
7
star
17

avs

Advanced Visualisation Studio - unofficial git repo
C++
6
star
18

systrace

Unofficial mirror of systrace project
C
5
star
19

openssl

openssl repo clone
C
5
star
20

smalltable

Historical copy of http://code.google.com/p/smalltable
C
4
star
21

tornadopuka

Python
4
star
22

ydb

YDB, why not?
C
3
star
23

uclibc-vx32

C
3
star
24

libmsock

C
3
star
25

rons

RonS - simple pub/sub Redis client for Tornado
Python
3
star
26

rabbitmq-x-presence

RabbitMQ x-presence exchange (outdated)
Erlang
3
star
27

flying-squirrel-node

Flying-Squirrel server implementation using Node.js
JavaScript
2
star
28

ydb-old

Historical copy of http://code.google.com/p/ydb
C
2
star
29

sicp

Python
2
star
30

pats

Python
1
star
31

luasocket-vx32

C
1
star
32

sockjs-protocol

Repo migrated - https://github.com/sockjs/sockjs-protocol
1
star
33

wdl

http://cockos.com/wdl/ - unofficial git clone
C
1
star
34

p0f.popcount.org

Python
1
star
35

derponk5000

CoffeeScript
1
star
36

mdx

Simple markdown extensions
Ruby
1
star
37

popcount.org

1
star
38

codejam-practice

practice for codejam
Python
1
star
39

puka_pool

Python
1
star
40

rabbitmq-heroku-example

Ruby
1
star
41

rfc6455-client-erlang

WebSocket client for Erlang
Erlang
1
star
42

sockjs-node

Repo migrated: https://github.com/sockjs/sockjs-node
1
star
43

idea

Blog source: Marek's idea of the day
Python
1
star
44

ziutek

A historical clone of http://code.google.com/p/ziutek
Python
1
star
45

rmqstress

CoffeeScript
1
star