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> |
… | |
… | |
39 | * once more silently being discarded. |
43 | * once more silently being discarded. |
40 | */ |
44 | */ |
41 | if (!nev) |
45 | if (!nev) |
42 | { |
46 | { |
43 | if (oev) |
47 | if (oev) |
44 | port_dissociate (port_fd, PORT_SOURCE_FD, fd); |
48 | port_dissociate (backend_fd, PORT_SOURCE_FD, fd); |
45 | } |
49 | } |
46 | else if (0 > |
50 | else if (0 > |
47 | port_associate ( |
51 | port_associate ( |
48 | port_fd, PORT_SOURCE_FD, fd, |
52 | backend_fd, PORT_SOURCE_FD, fd, |
49 | (nev & EV_READ ? POLLIN : 0) |
53 | (nev & EV_READ ? POLLIN : 0) |
50 | | (nev & EV_WRITE ? POLLOUT : 0), |
54 | | (nev & EV_WRITE ? POLLOUT : 0), |
51 | 0 |
55 | 0 |
52 | ) |
56 | ) |
53 | ) |
57 | ) |
… | |
… | |
66 | struct timespec ts; |
70 | struct timespec ts; |
67 | uint_t nget = 1; |
71 | uint_t nget = 1; |
68 | |
72 | |
69 | ts.tv_sec = (time_t)timeout; |
73 | ts.tv_sec = (time_t)timeout; |
70 | ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
74 | ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
71 | res = port_getn (port_fd, port_events, port_eventmax, &nget, &ts); |
75 | res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts); |
72 | |
76 | |
73 | if (res < 0) |
77 | if (res < 0) |
74 | { |
78 | { |
75 | if (errno != EINTR && errno != ETIME) |
79 | if (errno != EINTR && errno != ETIME) |
76 | syserr ("(libev) port_getn"); |
80 | syserr ("(libev) port_getn"); |
… | |
… | |
102 | port_eventmax = array_roundsize (port_event_t, port_eventmax << 1); |
106 | port_eventmax = array_roundsize (port_event_t, port_eventmax << 1); |
103 | port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
107 | port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
104 | } |
108 | } |
105 | } |
109 | } |
106 | |
110 | |
107 | static int |
111 | int inline_size |
108 | port_init (EV_P_ int flags) |
112 | port_init (EV_P_ int flags) |
109 | { |
113 | { |
110 | /* Initalize the kernel queue */ |
114 | /* Initalize the kernel queue */ |
111 | if ((port_fd = port_create ()) < 0) |
115 | if ((backend_fd = port_create ()) < 0) |
112 | return 0; |
116 | return 0; |
113 | |
117 | |
114 | fcntl (port_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
118 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
115 | |
119 | |
116 | method_fudge = 1e-3; /* needed to compensate for port_getn returning early */ |
120 | backend_fudge = 1e-3; /* needed to compensate for port_getn returning early */ |
117 | method_modify = port_modify; |
121 | backend_modify = port_modify; |
118 | method_poll = port_poll; |
122 | backend_poll = port_poll; |
119 | |
123 | |
120 | port_eventmax = 64; /* intiial number of events receivable per poll */ |
124 | port_eventmax = 64; /* intiial number of events receivable per poll */ |
121 | port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
125 | port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax); |
122 | |
126 | |
123 | return EVMETHOD_PORT; |
127 | return EVBACKEND_PORT; |
124 | } |
128 | } |
125 | |
129 | |
126 | static void |
130 | void inline_size |
127 | port_destroy (EV_P) |
131 | port_destroy (EV_P) |
128 | { |
132 | { |
129 | close (port_fd); |
|
|
130 | |
|
|
131 | ev_free (port_events); |
133 | ev_free (port_events); |
132 | } |
134 | } |
133 | |
135 | |
134 | static void |
136 | void inline_size |
135 | port_fork (EV_P) |
137 | port_fork (EV_P) |
136 | { |
138 | { |
137 | close (port_fd); |
139 | close (backend_fd); |
138 | |
140 | |
139 | while ((port_fd = port_create ()) < 0) |
141 | while ((backend_fd = port_create ()) < 0) |
140 | syserr ("(libev) port"); |
142 | syserr ("(libev) port"); |
141 | |
143 | |
142 | fcntl (port_fd, F_SETFD, FD_CLOEXEC); |
144 | fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
143 | |
145 | |
144 | /* re-register interest in fds */ |
146 | /* re-register interest in fds */ |
145 | fd_rearm_all (EV_A); |
147 | fd_rearm_all (EV_A); |
146 | } |
148 | } |
147 | |
149 | |