aboutsummaryrefslogtreecommitdiff
path: root/tests/fifo.lua
diff options
context:
space:
mode:
authorPeter Drahoš <drahosp@gmail.com>2010-10-01 03:22:32 +0200
committerPeter Drahoš <drahosp@gmail.com>2010-10-01 03:22:32 +0200
commit89d9c98af1ac352ba4d49d660e61b0853d6e1a86 (patch)
tree15c56d2ce66b4ab147171c0f674cdb4a435ff13f /tests/fifo.lua
downloadlanes-89d9c98af1ac352ba4d49d660e61b0853d6e1a86.tar.gz
lanes-89d9c98af1ac352ba4d49d660e61b0853d6e1a86.tar.bz2
lanes-89d9c98af1ac352ba4d49d660e61b0853d6e1a86.zip
Import to git
Diffstat (limited to 'tests/fifo.lua')
-rw-r--r--tests/fifo.lua43
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
7require "lanes"
8
9local linda= lanes.linda()
10local atomic_inc= lanes.genatomic( linda, "FIFO_n" )
11
12assert( atomic_inc()==1 )
13assert( atomic_inc()==2 )
14
15local 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 }
24end
25
26local A= FIFO()
27local B= FIFO()
28
29print "Sending to A.."
30A:send( 1,2,3,4,5 )
31
32print "Sending to B.."
33B:send( 'a','b','c' )
34
35print "Reading A.."
36print( A:receive( 1.0 ) )
37
38print "Reading B.."
39print( 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)