diff options
Diffstat (limited to 'tests/fifo.lua')
| -rw-r--r-- | tests/fifo.lua | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/fifo.lua b/tests/fifo.lua new file mode 100644 index 0000000..898b04d --- /dev/null +++ b/tests/fifo.lua | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | -- | ||
| 2 | -- FIFO.LUA | ||
| 3 | -- | ||
| 4 | -- Sample program for Lua Lanes | ||
| 5 | -- | ||
| 6 | |||
| 7 | require "lanes" | ||
| 8 | |||
| 9 | local linda= lanes.linda() | ||
| 10 | local atomic_inc= lanes.genatomic( linda, "FIFO_n" ) | ||
| 11 | |||
| 12 | assert( atomic_inc()==1 ) | ||
| 13 | assert( atomic_inc()==2 ) | ||
| 14 | |||
| 15 | local function FIFO() | ||
| 16 | local my_channel= "FIFO"..atomic_inc() | ||
| 17 | |||
| 18 | return { | ||
| 19 | -- Giving explicit 'nil' timeout allows numbers to be used as 'my_channel' | ||
| 20 | -- | ||
| 21 | send= function(...) linda:send( nil, my_channel, ... ) end, | ||
| 22 | receive= function(timeout) linda:receive( timeout, my_channel ) end | ||
| 23 | } | ||
| 24 | end | ||
| 25 | |||
| 26 | local A= FIFO() | ||
| 27 | local B= FIFO() | ||
| 28 | |||
| 29 | print "Sending to A.." | ||
| 30 | A:send( 1,2,3,4,5 ) | ||
| 31 | |||
| 32 | print "Sending to B.." | ||
| 33 | B:send( 'a','b','c' ) | ||
| 34 | |||
| 35 | print "Reading A.." | ||
| 36 | print( A:receive( 1.0 ) ) | ||
| 37 | |||
| 38 | print "Reading B.." | ||
| 39 | print( B:receive( 2.0 ) ) | ||
| 40 | |||
| 41 | -- Note: A and B can be passed between threads, or used as upvalues | ||
| 42 | -- by multiple threads (other parts will be copied but the 'linda' | ||
| 43 | -- handle is shared userdata and will thus point to the single place) | ||
