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