Blame view

publish/skynet/lualib/skynet/queue.lua 801 Bytes
4d6f285d   zhouhaihai   增加发布功能
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
  local skynet = require "skynet"
  local coroutine = coroutine
  local xpcall = xpcall
  local traceback = debug.traceback
  local table = table
  
  function skynet.queue()
  	local current_thread
  	local ref = 0
  	local thread_queue = {}
  
  	local function xpcall_ret(ok, ...)
  		ref = ref - 1
  		if ref == 0 then
  			current_thread = table.remove(thread_queue,1)
  			if current_thread then
  				skynet.wakeup(current_thread)
  			end
  		end
  		assert(ok, (...))
  		return ...
  	end
  
  	return function(f, ...)
  		local thread = coroutine.running()
  		if current_thread and current_thread ~= thread then
  			table.insert(thread_queue, thread)
  			skynet.wait()
  			assert(ref == 0)	-- current_thread == thread
  		end
  		current_thread = thread
  
  		ref = ref + 1
  		return xpcall_ret(xpcall(f, traceback, ...))
  	end
  end
  
  return skynet.queue