Software /
code /
prosody
Annotate
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 |
rev | line source |
---|---|
6514
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
1 -- Prosody IM |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2014 Daurnimator |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
3 -- |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
6 -- |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
7 -- This module allows you to use cqueues with a net.server mainloop |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
8 -- |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
9 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
10 local server = require "net.server"; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
11 local cqueues = require "cqueues"; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
12 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
13 -- Create a single top level cqueue |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
14 local cq; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
15 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
16 if server.cq then -- server provides cqueues object |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
17 cq = server.cq; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
18 elseif server.get_backend() == "select" and server._addtimer then -- server_select |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
19 cq = cqueues.new(); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
20 local function step() |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
21 assert(cq:loop(0)); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
22 end |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
23 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
24 -- Use wrapclient (as wrapconnection isn't exported) to get server_select to watch cq fd |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
25 local handler = server.wrapclient({ |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
26 getfd = function() return cq:pollfd(); end; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
27 settimeout = function() end; -- Method just needs to exist |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
28 close = function() end; -- Need close method for 'closeall' |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
29 }, nil, nil, {}); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
30 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
31 -- Only need to listen for readable; cqueues handles everything under the hood |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
32 -- readbuffer is called when `select` notes an fd as readable |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
33 handler.readbuffer = step; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
34 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
35 -- Use server_select low lever timer facility, |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
36 -- this callback gets called *every* time there is a timeout in the main loop |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
37 server._addtimer(function(current_time) |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
38 -- This may end up in extra step()'s, but cqueues handles it for us. |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
39 step(); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
40 return cq:timeout(); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
41 end); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
42 elseif server.event and server.base then -- server_event |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
43 cq = cqueues.new(); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
44 -- Only need to listen for readable; cqueues handles everything under the hood |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
45 local EV_READ = server.event.EV_READ; |
6536
605a87e90e27
net.cqueues: Add workaround for luaevent callback getting collected
daurnimator <quae@daurnimator.com>
parents:
6514
diff
changeset
|
46 local event_handle; |
605a87e90e27
net.cqueues: Add workaround for luaevent callback getting collected
daurnimator <quae@daurnimator.com>
parents:
6514
diff
changeset
|
47 event_handle = server.base:addevent(cq:pollfd(), EV_READ, function(e) |
605a87e90e27
net.cqueues: Add workaround for luaevent callback getting collected
daurnimator <quae@daurnimator.com>
parents:
6514
diff
changeset
|
48 -- Need to reference event_handle or this callback will get collected |
605a87e90e27
net.cqueues: Add workaround for luaevent callback getting collected
daurnimator <quae@daurnimator.com>
parents:
6514
diff
changeset
|
49 -- This creates a circular reference that can only be broken if event_handle is manually :close()'d |
605a87e90e27
net.cqueues: Add workaround for luaevent callback getting collected
daurnimator <quae@daurnimator.com>
parents:
6514
diff
changeset
|
50 local _ = event_handle; |
6514
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
51 assert(cq:loop(0)); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
52 -- Convert a cq timeout to an acceptable timeout for luaevent |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
53 local t = cq:timeout(); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
54 if t == 0 then -- if you give luaevent 0, it won't call this callback again |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
55 t = 0.000001; -- 1 microsecond is the smallest that works (goes into a `struct timeval`) |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
56 elseif t == nil then -- you always need to give a timeout, pick something big if we don't have one |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
57 t = 0x7FFFFFFF; -- largest 32bit int |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
58 end |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
59 return EV_READ, t; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
60 end, |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
61 -- Schedule the callback to fire on first tick to ensure any cq:wrap calls that happen during start-up are serviced. |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
62 0.000001); |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
63 else |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
64 error "NYI" |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
65 end |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
66 |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
67 return { |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
68 cq = cq; |
d425fc41e59f
net.cqueues: Add module that allows use of cqueues while still using net.server as main loop
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
69 } |