ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_port.c
(Generate patch)

Comparing libev/ev_port.c (file contents):
Revision 1.1 by root, Fri Nov 16 01:33:54 2007 UTC vs.
Revision 1.9 by root, Fri Dec 14 18:04:31 2007 UTC

1/* 1/*
2 * libev solaris event port backend
3 *
2 * Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved.
3 * 6 *
4 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions are
6 * are met: 9 * met:
7 * 10 *
8 * 1. Redistributions of source code must retain the above copyright 11 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
10 * 2. 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.
13 * 3. 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 * 13 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * * Redistributions in binary form must reproduce the above
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * copyright notice, this list of conditions and the following
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * disclaimer in the documentation and/or other materials provided
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 30 */
27 31
28#include <sys/types.h> 32#include <sys/types.h>
29#include <sys/time.h> 33#include <sys/time.h>
30#include <poll.h> 34#include <poll.h>
31#include <port.h> 35#include <port.h>
32#include <string.h> 36#include <string.h>
33#include <errno.h> 37#include <errno.h>
38
39void inline_speed
40port_associate_and_check (EV_P_ int fd, int ev)
41{
42 if (0 >
43 port_associate (
44 backend_fd, PORT_SOURCE_FD, fd,
45 (ev & EV_READ ? POLLIN : 0)
46 | (ev & EV_WRITE ? POLLOUT : 0),
47 0
48 )
49 )
50 {
51 if (errno == EBADFD)
52 fd_kill (EV_A_ fd);
53 else
54 syserr ("(libev) port_associate");
55 }
56}
34 57
35static void 58static void
36port_modify (EV_P_ int fd, int oev, int nev) 59port_modify (EV_P_ int fd, int oev, int nev)
37{ 60{
38 /* we need to reassociate no matter what, as closes are 61 /* we need to reassociate no matter what, as closes are
39 * once more silently being discarded. 62 * once more silently being discarded.
40 */ 63 */
41 if (!nev) 64 if (!nev)
42 { 65 {
43 if (oev) 66 if (oev)
44 port_dissociate (port_fd, PORT_SOURCE_FD, fd); 67 port_dissociate (backend_fd, PORT_SOURCE_FD, fd);
45 } 68 }
46 else if (0 >
47 port_associate (
48 port_fd, PORT_SOURCE_FD, fd,
49 (nev & EV_READ ? POLLIN : 0)
50 | (nev & EV_WRITE ? POLLOUT : 0),
51 0
52 )
53 )
54 {
55 if (errno == EBADFD)
56 fd_kill (EV_A_ fd);
57 else 69 else
58 syserr ("(libev) port_associate"); 70 port_associate_and_check (EV_A_ fd, nev);
59 }
60} 71}
61 72
62static void 73static void
63port_poll (EV_P_ ev_tstamp timeout) 74port_poll (EV_P_ ev_tstamp timeout)
64{ 75{
66 struct timespec ts; 77 struct timespec ts;
67 uint_t nget = 1; 78 uint_t nget = 1;
68 79
69 ts.tv_sec = (time_t)timeout; 80 ts.tv_sec = (time_t)timeout;
70 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; 81 ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9;
71 res = port_getn (port_fd, port_events, port_eventmax, &nget, &ts); 82 res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
72 83
73 if (res < 0) 84 if (res < 0)
74 { 85 {
75 if (errno != EINTR && errno != ETIME) 86 if (errno != EINTR && errno != ETIME)
76 syserr ("(libev) port_getn"); 87 syserr ("(libev) port_getn");
89 fd, 100 fd,
90 (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) 101 (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
91 | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) 102 | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
92 ); 103 );
93 104
94 anfds [fd].events = 0; /* event received == disassociated */ 105 port_associate_and_check (EV_A_ fd, anfds [fd].events);
95 fd_change (EV_A_ fd); /* need to reify later */
96 } 106 }
97 } 107 }
98 108
99 if (expect_false (nget == port_eventmax)) 109 if (expect_false (nget == port_eventmax))
100 { 110 {
101 ev_free (port_events); 111 ev_free (port_events);
102 port_eventmax = array_roundsize (port_event_t, port_eventmax << 1); 112 port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1);
103 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); 113 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
104 } 114 }
105} 115}
106 116
107static int 117int inline_size
108port_init (EV_P_ int flags) 118port_init (EV_P_ int flags)
109{ 119{
110 /* Initalize the kernel queue */ 120 /* Initalize the kernel queue */
111 if ((port_fd = port_create ()) < 0) 121 if ((backend_fd = port_create ()) < 0)
112 return 0; 122 return 0;
113 123
114 fcntl (port_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ 124 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
115 125
116 method_fudge = 1e-3; /* needed to compensate for port_getn returning early */ 126 backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */
117 method_modify = port_modify; 127 backend_modify = port_modify;
118 method_poll = port_poll; 128 backend_poll = port_poll;
119 129
120 port_eventmax = 64; /* intiial number of events receivable per poll */ 130 port_eventmax = 64; /* intiial number of events receivable per poll */
121 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); 131 port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
122 132
123 return EVMETHOD_PORT; 133 return EVBACKEND_PORT;
124} 134}
125 135
126static void 136void inline_size
127port_destroy (EV_P) 137port_destroy (EV_P)
128{ 138{
129 close (port_fd);
130
131 ev_free (port_events); 139 ev_free (port_events);
132} 140}
133 141
134static void 142void inline_size
135port_fork (EV_P) 143port_fork (EV_P)
136{ 144{
137 close (port_fd); 145 close (backend_fd);
138 146
139 while ((port_fd = port_create ()) < 0) 147 while ((backend_fd = port_create ()) < 0)
140 syserr ("(libev) port"); 148 syserr ("(libev) port");
141 149
142 fcntl (port_fd, F_SETFD, FD_CLOEXEC); 150 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
143 151
144 /* re-register interest in fds */ 152 /* re-register interest in fds */
145 fd_rearm_all (EV_A); 153 fd_rearm_all (EV_A);
146} 154}
147 155

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines