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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines