source: branches/home/psmith/stress-mods/src/epoll.lisp

Last change on this file was 10, checked in by psmith, 18 years ago

Moved to event driven SM:

Server socket in level triggered mode - (TODO: why does this improve performance?)
Put accepted connections fd's in nonblocking, RW notification and left as ET (as suggested in 'man epoll')
Increased accept backlog to 1k (TODO: put on config)
Included code review suggestions from Risto

File size: 3.5 KB
Line 
1#|
2Copyright (c) 2006 Risto Laakso
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright
11   notice, this list of conditions and the following disclaimer in the
12   documentation and/or other materials provided with the distribution.
133. The name of the author may not be used to endorse or promote products
14   derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26|#
27(in-package :event-notification)
28
29#+linux
30(progn
31
32  (defconstant +epoll-size+ 1000)
33
34  (defun make-event-queue ()
35    (%epoll-create +epoll-size+))
36
37  (defun read-event-p (event)
38    (not (eql (logand event +epoll-in+) 0)))
39
40  (defun write-event-p (event)
41    (not (eql (logand event +epoll-out+) 0)))
42
43
44
45  (defun add-fd (event-queue fd mode &key (trigger :edge))
46    (with-foreign-object (ev 'epoll-event)
47      (memzero ev +epoll-event-size+)
48      (let ((actual-mode (logior (if (eql :read mode) +epoll-in+ 0) 
49                                 (if (eql :write mode) +epoll-out+ 0)
50                                 (if (eql :read-write mode) (logior +epoll-in+ +epoll-out+) 0)
51                                 (if (eql trigger :edge) +epoll-et+ 0))))
52        #+nio-debug     (format t "Add-fd called with :fd ~A :event-queue ~A :mode ~A :trigger ~A :actual-mode ~A~%" fd event-queue mode trigger actual-mode)
53       
54        (setf (foreign-slot-value ev 'epoll-event 'fd)  fd
55              (foreign-slot-value ev 'epoll-event 'events)
56              actual-mode))
57     
58      (if (eql (%epoll-ctl event-queue +epoll-ctl-add+ fd ev) -1)
59          (progn
60            (format t "add-fd (epoll_ctl) error occurred: ~A~%" (get-errno))
61            ;;      (error 'poll-error)
62            ))))
63 
64  (defun remove-fd (event-queue fd mode)
65    (with-foreign-object (ev 'epoll-event)
66      (memzero ev +epoll-event-size+)
67
68      (setf (foreign-slot-value ev 'epoll-event 'fd) fd)
69
70      (%epoll-ctl event-queue +epoll-ctl-del+ fd ev)))
71
72
73  (define-condition poll-error (error) ())
74
75  (defun poll-events (event-queue)
76#+nio-debug    (format t "poll-events called with :event-queue ~A~%" event-queue)
77    (with-foreign-object (events 'epoll-event +epoll-size+)
78      (memzero events (* +epoll-event-size+ +epoll-size+))
79      (loop for res = (%epoll-wait event-queue events +epoll-size+ -1)
80           
81           do
82           (progn
83#+nio-debug          (format t "poll-events - dealing with ~A~%" res)
84             (case res
85               (-1 (error 'poll-error))
86               (0 nil)
87               (t 
88                (let ((idents nil))
89                  (loop for i from 0 below res do
90                       (push (cons (foreign-slot-value 
91                                    (mem-aref events 'epoll-event i) 
92                                    'epoll-event 'fd)
93                                   (foreign-slot-value 
94                                    (mem-aref events 'epoll-event i) 
95                                    'epoll-event 'events)) 
96                             idents))
97                  (return idents))))))))
98)
Note: See TracBrowser for help on using the repository browser.