about summary refs log tree commit diff stats
path: root/src/task.lua
blob: e8bf842704b6de11b59baeeb815436e40a1dc6fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
----------------------------------------------------------------------------
-- Go style Channels for Lua
-- https://github.com/majek/lua-channels
--
-- 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 (we're using unbuffered channel here):
--
-- local task = require('task')
--
-- 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()
--
--
-- This module 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.
----------------------------------------------------------------------------

local _M = {}

-- Constants
local RECV = 0x1
local SEND = 0x2
local NOP  = 0x3

-- Global objects for scheduler
local tasks_runnable = {}       -- list of coroutines ready to be resumed


----------------------------------------------------------------------------
--- Helpers

local function random_choice(arr)
   if #arr > 1 then
      return arr[math.random(#arr)]
   else
      return arr[1]
   end
end

-- Specialised Set data structure (with random element selection)
local Set = {
   new = function(self)
      local o = {a = {}, l = {}}; setmetatable(o, self); self.__index = self
      return o
   end,

   add = function(self, v)
      local a, l = self.a, self.l
      if a[v] == nil then
         table.insert(l, v)
         a[v] = #l
         return true
      end
   end,

   remove = function(self, v)
      local a, l = self.a, self.l
      local i = a[v]
      if i > 0 then
         local t = l[#l]
         a[t], l[i] = i, t
         a[i], l[#l] = nil, nil
         return true
      end
   end,

   random = function(self, v)
      return random_choice(self.l)
   end,

   len = function(self)
      return #self.l
   end,
}

-- Circular Buffer data structure
local CircularBuffer = {
   new = function(self, size)
      local o = {b = {}, slots = size + 1, size = size, l = 0, r = 0}
      setmetatable(o, self); self.__index = self
      return o
   end,

   len = function(self)
      return (self.r - self.l) % self.slots
   end,

   pop = function(self)
      assert(self.l ~= self.r)
      local v = self.b[self.l]
      self.l = (self.l + 1) % self.slots
      return v
   end,

   push = function(self, v)
      self.b[self.r] = v
      self.r = (self.r + 1) % self.slots
      assert(self.l ~= self.r)
   end,
}

----------------------------------------------------------------------------
-- Scheduling
--
-- Tasks ready to be run are placed on a stack and it's possible to
-- starve a coroutine.
local function scheduler()
   local self_coro, is_main = coroutine.running()

   -- We actually don't care if scheduler is run from the main
   -- coroutine. But we do need to make sure that user doesn't do
   -- blocking operation from it, as it can't yield.

   -- Be compatible with 5.1 and 5.2
   assert(not(self_coro ~= nil and is_main ~= true),
          "Scheduler must be run from the main coroutine.")

   local i = 0
   while #tasks_runnable > 0 do
      local co = table.remove(tasks_runnable)
      local okay, emsg = coroutine.resume(co)
      if not okay then
         error(emsg)
      end
      i = i + 1
   end
   return i
end

local function task_ready(co)
   table.insert(tasks_runnable, co)
end

local function spawn(fun, ...)
   local args = {...}

   local f = function()
      fun(unpack(args))
   end
   local co = coroutine.create(f)
   task_ready(co)
end

----------------------------------------------------------------------------
-- Channels - chanalt and helpers

-- Given two Alts from a single channel exchange data between
-- them. It's implied that one is RECV and another is SEND. Channel
-- may be buffered.
local function altcopy(a, b)
   local r, s, c = a, b, a.c
   if r.op == SEND then
      r, s = s, r
   end

   assert(s == nil or s.op == SEND)
   assert(r == nil or r.op == RECV)

   -- Channel is empty or unbuffered, copy directly
   if s ~= nil and r and c._buf:len() == 0 then
      r.alt_array.value = s.p
      return
   end

   -- Otherwise it's always okay to receive and then send.
   if r ~= nil then
      r.alt_array.value = c._buf:pop()
   end
   if s ~= nil then
      c._buf:push(s.p)
   end
end

-- Given enqueued alt_array from a chanalt statement remove all alts
-- from the associated channels.
local function altalldequeue(alt_array)
   for i = 1, #alt_array do
      local a = alt_array[i]
      if a.op == RECV or a.op == SEND then
         a.c:_get_alts(a.op):remove(a)
      end
   end
end

-- Can this Alt be execed without blocking?
local function altcanexec(a)
   local c, op = a.c, a.op
   if c._buf.size == 0 then
      if op ~= NOP then
         return c:_get_other_alts(op):len() > 0
      end
   else
      if op == SEND then
         return c._buf:len() < c._buf.size
      elseif op == RECV then
         return c._buf:len() > 0
      end
   end
end

-- Alt can be execed so find a counterpart Alt and exec it!
local function altexec(a)
   local c, op = a.c, a.op
   local other_alts = c:_get_other_alts(op)
   local other_a = other_alts:random()
   -- other_a may be nil
   altcopy(a, other_a)
   if other_a ~= nil then
      -- Disengage from channels used by the other Alt and make it ready.
      altalldequeue(other_a.alt_array)
      other_a.alt_array.resolved = other_a.alt_index
      task_ready(other_a.alt_array.task)
   end
end

function ischannel(x)
    return type(x) == "table" and x.__index == _M.Channel
end

-- The main entry point. Call it `alt` or `select` or just a
-- multiplexing statement. This is user facing function so make sure
-- the parameters passed are sane.
local function chanalt(alt_array, canblock)
   assert(#alt_array)

   local list_of_canexec_i = {}
   for i = 1, #alt_array do
      local a = alt_array[i]
      a.alt_array = alt_array
      a.alt_index = i
      assert(type(a.op) == "number" and
                (a.op == RECV or a.op == SEND or a.op == NOP),
             "op field must be RECV, SEND or NOP in alt")
      assert(ischannel(a.c),
             "pass valid channel to a c field of alt")
      if altcanexec(a) == true then
         table.insert(list_of_canexec_i, i)
      end
   end

   if #list_of_canexec_i > 0 then
      local i = random_choice(list_of_canexec_i)
      altexec(alt_array[i])
      return i, alt_array.value
   end

   if canblock ~= true then
      return nil
   end

   local self_coro, is_main = coroutine.running()
   alt_array.task = self_coro
   assert(self_coro ~= nil and is_main ~= true,
          "Unable to block from the main thread, run scheduler.")

   for i = 1, #alt_array do
      local a = alt_array[i]
      if a.op ~= NOP then
         a.c:_get_alts(a.op):add(a)
      end
   end

   -- Make sure we're not woken by someone who is not the scheduler.
   alt_array.resolved = nil
   coroutine.yield()
   assert(alt_array.resolved > 0)

   local r = alt_array.resolved
   return r, alt_array.value
end


----------------------------------------------------------------------------
-- Channel object

local Channel = {
   new = function(self, buf_size)
      local o = {}; setmetatable(o, self); self.__index = self
      o._buf = CircularBuffer:new(buf_size or 0)
      o._recv_alts, o._send_alts = Set:new(), Set:new()
      return o
   end,

   send = function(self, msg)
      assert(chanalt({{c = self, op = SEND, p = msg}}, true) == 1)
      return true
   end,

   recv = function(self)
      local alts = {{c = self, op = RECV}}
      local s, msg = chanalt(alts, true)
      assert(s == 1)
      return msg
   end,

   nbsend = function(self, msg)
      local s = chanalt({{c = self, op = SEND, p = msg}}, false)
      return s == 1
   end,

   nbrecv = function(self)
      local s, msg = chanalt({{c = self, op = RECV}}, false)
      return s == 1, msg
   end,

   _get_alts = function(self, op)
      return (op == RECV) and self._recv_alts or self._send_alts
   end,

   _get_other_alts = function(self, op)
      return (op == SEND) and self._recv_alts or self._send_alts
   end,

   __tostring = function(self)
      return string.format("<Channel size=%i/%i send_alt=%i recv_alt=%i>",
                           self._buf:len(), self._buf.size, self._send_alts:len(),
                           self._recv_alts:len())
   end,

   __call = function(self)
      local function f(s, v)
         return true, self:recv()
      end
      return f, nil, nil
   end,
}

----------------------------------------------------------------------------
-- Public interface

_M.scheduler = scheduler
_M.spawn     = spawn
_M.Channel   = Channel
_M.chanalt   = chanalt
_M.RECV      = RECV
_M.SEND      = SEND
_M.NOP       = NOP

----------------------------------------------------------------------------
----------------------------------------------------------------------------
-- Tests

local task = _M

function test_counter()
   local done
   local function counter(c)
      local i = 1
      while true do
         c:send(i)
         i = i + 1
      end
   end
   local function main()
      local c = task.Channel:new()
      task.spawn(counter, c)
      assert(c:recv() == 1)
      assert(c:recv() == 2)
      assert(c:recv() == 3)
      assert(c:recv() == 4)
      assert(c:recv() == 5)
      done = true
   end
   task.spawn(main)
   task.scheduler()
   assert(done)
end

function test_nonblocking_channel()
   local done
   local function main()
      local b = task.Channel:new()
      assert(b:nbsend(1) == false)
      assert(b:nbrecv() == false)

      local c = task.Channel:new(1)
      assert(c:nbrecv() == false)
      assert(c:nbsend(1) == true)
      assert(c:nbsend(1) == false)
      local r, v = c:nbrecv()
      assert(r == true)
      assert(v == 1)
      assert(c:nbrecv() == false)
      done = true
   end
   task.spawn(main)
   task.scheduler()
   assert(done)
end

function test_concurrent_send_and_recv()
   local l = {}
   local function a(c, name)
      -- Blocking send and recv from the same process
      local alt = {{c = c, op = task.SEND, p = 1},
                   {c = c, op = task.RECV}}
      local i, v = task.chanalt(alt, true)
      local k = string.format('%s %s', name, i == 1 and "send" or "recv")
      l[k] = (l[k] or 0) + 1
   end

   for i = 0, 1000 do
      -- On Mac OS X in lua 5.1 initializing seed with a
      -- predictable value makes no sense. For all seeds from 1 to
      -- 1000 the result of math.random(1,3) is _exactly_ the same!
      -- So beware, when seeding!
      -- math.randomseed(i)
      local c = task.Channel:new()
      task.spawn(a, c, "a")
      task.spawn(a, c, "b")
      task.scheduler()
   end

   -- Make sure we have randomness, that is: events occur in both
   -- orders in 1000 runs
   assert(l['a recv'] > 0)
   assert(l['a send'] > 0)
   assert(l['b recv'] > 0)
   assert(l['b send'] > 0)
end

function test_channels_from_a_coroutine()
   local done
   local c = task.Channel:new()
   local function a()
      for i = 1, 100 do
         c:send(i)
      end
   end
   local function b()
      assert(c:recv() == 1)
      assert(c:recv() == 2)
      assert(c:recv() == 3)
      assert(c:recv() == 4)
      assert(c:recv() == 5)
      done = true
   end
   local a_co = coroutine.create(a)
   local b_co = coroutine.create(b)
   coroutine.resume(a_co)
   coroutine.resume(b_co)
   task.scheduler()
   assert(done)
end

function test_fibonacci()
   local done
   local function fib(c)
      local x, y = 0, 1
      while true do
         c:send(x)
         x, y = y, x + y
      end
   end
   local function main(c)
      assert(c:recv() == 0)
      assert(c:recv() == 1)
      assert(c:recv() == 1)
      assert(c:recv() == 2)
      assert(c:recv() == 3)
      assert(c:recv() == 5)
      assert(c:recv() == 8)
      assert(c:recv() == 13)
      assert(c:recv() == 21)
      assert(c:recv() == 34)
      done = true
   end

   local c = task.Channel:new()
   task.spawn(fib, c)
   task.spawn(main, c)
   task.scheduler()
   assert(done)
end

function test_non_blocking_chanalt()
   local done
   local function main()
      local c = task.Channel:new()
      local alts = {{c = c, op = task.RECV},
                    {c = c, op = task.NOP},
                    {c = c, op = task.SEND, p = 1}}
      assert(task.chanalt(alts, false) == nil)

      local c = task.Channel:new(1)
      local alts = {{c = c, op = task.RECV},
                    {c = c, op = task.NOP},
                    {c = c, op = task.SEND, p = 1}}
      assert(task.chanalt(alts, false) == 3)
      assert(task.chanalt(alts, false) == 1)

      local alts = {{c = c, op = task.NOP}}
      assert(task.chanalt(alts, false) == nil)

      done = true
   end
   task.spawn(main)
   task.scheduler()
   assert(done)
end

-- Apparently it's not really a Sieve of Eratosthenes:
--   http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
function test_eratosthenes_sieve()
   local done
   local function counter(c)
      local i = 2
      while true do
         c:send(i)
         i = i + 1
      end
   end

   local function filter(p, recv_ch, send_ch)
      while true do
         local i = recv_ch:recv()
         if i % p ~= 0 then
            send_ch:send(i)
         end
      end
   end

   local function sieve(primes_ch)
      local c = task.Channel:new()
      task.spawn(counter, c)
      while true do
         local p, newc = c:recv(), task.Channel:new()
         primes_ch:send(p)
         task.spawn(filter, p, c, newc)
         c = newc
      end
   end

   local function main()
      local primes = task.Channel:new()
      task.spawn(sieve, primes)
      assert(primes:recv() == 2)
      assert(primes:recv() == 3)
      assert(primes:recv() == 5)
      assert(primes:recv() == 7)
      assert(primes:recv() == 11)
      assert(primes:recv() == 13)
      done = true
   end

   task.spawn(main)
   task.scheduler()
   assert(done)
end

function test_channel_as_iterator()
   local done
   local function counter(c)
      local i = 2
      while true do
         c:send(i)
         i = i + 1
      end
   end

   local function main()
      local numbers = task.Channel:new()
      task.spawn(counter, numbers)
      for _, j in numbers() do
         if j == 100 then
            break
         end
         done = true
      end
   end
   if _VERSION == "Lua 5.1" then
      -- sorry, this test doesn't work in 5.1
      print('S')
      done = true
   else
      task.spawn(main)
      task.scheduler()
   end
   assert(done)
end

return _M