source: branches/home/psmith/restructure/src/event/epoll.lisp

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

Print out interruptions

File size: 3.9 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  (defun error-event-p (event)
44    (not (eql (logand event +epoll-error+) 0)))
45
46  (defun hup-event-p (event)
47    (not (eql (logand event +epoll-hup+) 0)))
48
49  (defun add-fd (event-queue fd mode &key (trigger :edge))
50    (with-foreign-object (ev 'epoll-event)
51      (memzero ev +epoll-event-size+)
52      (let ((actual-mode (logior (if (eql :read mode) +epoll-in+ 0) 
53                                 (if (eql :write mode) +epoll-out+ 0)
54                                 (if (eql :read-write mode) (logior +epoll-in+ +epoll-out+) 0)
55                                 (if (eql trigger :edge) +epoll-et+ 0))))
56        #+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)
57       
58        (setf (foreign-slot-value ev 'epoll-event 'fd)  fd
59              (foreign-slot-value ev 'epoll-event 'events)
60              actual-mode))
61     
62      (if (eql (%epoll-ctl event-queue +epoll-ctl-add+ fd ev) -1)
63          (progn
64            (format t "add-fd (epoll_ctl) error occurred: ~A~%" (get-errno))
65            ;;      (error 'poll-error)
66            ))))
67 
68  (defun remove-fd (event-queue fd mode)
69    (with-foreign-object (ev 'epoll-event)
70      (memzero ev +epoll-event-size+)
71
72      (setf (foreign-slot-value ev 'epoll-event 'fd) fd)
73
74      (%epoll-ctl event-queue +epoll-ctl-del+ fd ev)))
75
76
77  (define-condition poll-error (error) ())
78
79  (defun poll-events (event-queue)
80#+nio-debug2    (format t "poll-events called with :event-queue ~A~%" event-queue)
81    (with-foreign-object (events 'epoll-event +epoll-size+)
82      (memzero events (* +epoll-event-size+ +epoll-size+))
83      (loop for res = (%epoll-wait event-queue events +epoll-size+ 100)
84           
85           do
86           (progn
87#+nio-debug2         (format t "poll-events - dealing with ~A~%" res)
88             (case res
89               (-1 
90                (let ((errno (get-errno)))
91                  (if (eql errno 4) ;EINTR - interrupted by a system call
92                      (progn
93                        (format t "epoll-wait interrupted~%")
94                        (return nil))
95                      (progn
96                        (format t "-1 returned from epoll-wait, errno:")
97                        (perror)
98                        (error 'poll-error)))))
99               (return nil)
100               (t 
101                (let ((idents nil))
102                  (loop for i from 0 below res do
103                       (push (cons (foreign-slot-value 
104                                    (mem-aref events 'epoll-event i) 
105                                    'epoll-event 'fd)
106                                   (foreign-slot-value 
107                                    (mem-aref events 'epoll-event i) 
108                                    'epoll-event 'events)) 
109                             idents))
110                  (return idents))))))))
111)
Note: See TracBrowser for help on using the repository browser.