Comparison

net/cqueues.lua @ 6536:605a87e90e27

net.cqueues: Add workaround for luaevent callback getting collected
author daurnimator <quae@daurnimator.com>
date Tue, 06 Jan 2015 20:01:59 -0500
parent 6514:d425fc41e59f
child 6537:e4d443d05626
comparison
equal deleted inserted replaced
6535:0f940a7ba489 6536:605a87e90e27
41 end); 41 end);
42 elseif server.event and server.base then -- server_event 42 elseif server.event and server.base then -- server_event
43 cq = cqueues.new(); 43 cq = cqueues.new();
44 -- Only need to listen for readable; cqueues handles everything under the hood 44 -- Only need to listen for readable; cqueues handles everything under the hood
45 local EV_READ = server.event.EV_READ; 45 local EV_READ = server.event.EV_READ;
46 server.base:addevent(cq:pollfd(), EV_READ, function(e) 46 local event_handle;
47 event_handle = server.base:addevent(cq:pollfd(), EV_READ, function(e)
48 -- Need to reference event_handle or this callback will get collected
49 -- This creates a circular reference that can only be broken if event_handle is manually :close()'d
50 local _ = event_handle;
47 assert(cq:loop(0)); 51 assert(cq:loop(0));
48 -- Convert a cq timeout to an acceptable timeout for luaevent 52 -- Convert a cq timeout to an acceptable timeout for luaevent
49 local t = cq:timeout(); 53 local t = cq:timeout();
50 if t == 0 then -- if you give luaevent 0, it won't call this callback again 54 if t == 0 then -- if you give luaevent 0, it won't call this callback again
51 t = 0.000001; -- 1 microsecond is the smallest that works (goes into a `struct timeval`) 55 t = 0.000001; -- 1 microsecond is the smallest that works (goes into a `struct timeval`)