| 1 |
/* |
| 2 |
* Copyright (c) 2012 William Pitcock <[email protected]>. |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 9 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 10 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 11 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 12 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 13 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 14 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 15 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 16 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 17 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 18 |
* POSSIBILITY OF SUCH DAMAGE. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "mowgli.h" |
| 22 |
#include "eventloop/eventloop_internal.h" |
| 23 |
|
| 24 |
#ifdef HAVE_PORT_CREATE |
| 25 |
|
| 26 |
# include <port.h> |
| 27 |
|
| 28 |
typedef struct |
| 29 |
{ |
| 30 |
int port_fd; |
| 31 |
int pfd_size; |
| 32 |
port_event_t *pfd; |
| 33 |
} mowgli_ports_eventloop_private_t; |
| 34 |
|
| 35 |
static void |
| 36 |
mowgli_ports_eventloop_pollsetup(mowgli_eventloop_t *eventloop) |
| 37 |
{ |
| 38 |
mowgli_ports_eventloop_private_t *priv; |
| 39 |
|
| 40 |
priv = mowgli_alloc(sizeof *priv); |
| 41 |
eventloop->poller = priv; |
| 42 |
|
| 43 |
priv->pfd_size = getdtablesize(); |
| 44 |
priv->port_fd = port_create(); |
| 45 |
priv->pfd = mowgli_alloc(sizeof(port_event_t) * priv->pfd_size); |
| 46 |
|
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
mowgli_ports_eventloop_pollshutdown(mowgli_eventloop_t *eventloop) |
| 52 |
{ |
| 53 |
mowgli_ports_eventloop_private_t *priv; |
| 54 |
|
| 55 |
return_if_fail(eventloop != NULL); |
| 56 |
|
| 57 |
priv = eventloop->poller; |
| 58 |
|
| 59 |
close(priv->port_fd); |
| 60 |
|
| 61 |
mowgli_free(priv->pfd); |
| 62 |
mowgli_free(priv); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
mowgli_ports_eventloop_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable) |
| 68 |
{ |
| 69 |
mowgli_ports_eventloop_private_t *priv; |
| 70 |
|
| 71 |
return_if_fail(eventloop != NULL); |
| 72 |
return_if_fail(pollable != NULL); |
| 73 |
|
| 74 |
priv = eventloop->poller; |
| 75 |
pollable->slot = 0; |
| 76 |
|
| 77 |
if (port_dissociate(priv->port_fd, PORT_SOURCE_FD, (uintptr_t) pollable->fd) < 0) |
| 78 |
{ |
| 79 |
if (mowgli_eventloop_ignore_errno(errno)) |
| 80 |
return; |
| 81 |
|
| 82 |
mowgli_log("mowgli_ports_eventloop_destroy(): port_dissociate failed: %d (%s)", errno, strerror(errno)); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
mowgli_ports_eventloop_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function) |
| 88 |
{ |
| 89 |
mowgli_ports_eventloop_private_t *priv; |
| 90 |
unsigned int old_flags; |
| 91 |
|
| 92 |
return_if_fail(eventloop != NULL); |
| 93 |
return_if_fail(pollable != NULL); |
| 94 |
|
| 95 |
priv = eventloop->poller; |
| 96 |
old_flags = pollable->slot; |
| 97 |
|
| 98 |
# ifdef DEBUG |
| 99 |
mowgli_log("setselect %p fd %d func %p", pollable, pollable->fd, event_function); |
| 100 |
# endif |
| 101 |
|
| 102 |
switch (dir) |
| 103 |
{ |
| 104 |
case MOWGLI_EVENTLOOP_IO_READ: |
| 105 |
pollable->read_function = event_function; |
| 106 |
pollable->slot |= POLLIN; |
| 107 |
break; |
| 108 |
case MOWGLI_EVENTLOOP_IO_WRITE: |
| 109 |
pollable->write_function = event_function; |
| 110 |
pollable->slot |= POLLOUT; |
| 111 |
break; |
| 112 |
default: |
| 113 |
mowgli_log("unhandled pollable direction %d", dir); |
| 114 |
break; |
| 115 |
} |
| 116 |
|
| 117 |
# ifdef DEBUG |
| 118 |
mowgli_log("%p -> read %p : write %p", pollable, pollable->read_function, pollable->write_function); |
| 119 |
# endif |
| 120 |
|
| 121 |
if (pollable->read_function == NULL) |
| 122 |
pollable->slot &= ~POLLIN; |
| 123 |
|
| 124 |
if (pollable->write_function == NULL) |
| 125 |
pollable->slot &= ~POLLOUT; |
| 126 |
|
| 127 |
if ((old_flags == 0) && (pollable->slot == 0)) |
| 128 |
{ |
| 129 |
return; |
| 130 |
} |
| 131 |
else if (pollable->slot == 0) |
| 132 |
{ |
| 133 |
port_dissociate(priv->port_fd, PORT_SOURCE_FD, (uintptr_t) pollable->fd); |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
if (port_associate(priv->port_fd, PORT_SOURCE_FD, (uintptr_t) pollable->fd, pollable->slot, pollable) < 0) |
| 138 |
{ |
| 139 |
if (mowgli_eventloop_ignore_errno(errno)) |
| 140 |
return; |
| 141 |
|
| 142 |
mowgli_log("mowgli_ports_eventloop_setselect(): port_associate failed: %d (%s)", errno, strerror(errno)); |
| 143 |
} |
| 144 |
|
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
mowgli_ports_eventloop_select(mowgli_eventloop_t *eventloop, int delay) |
| 150 |
{ |
| 151 |
mowgli_ports_eventloop_private_t *priv; |
| 152 |
int i, ret, o_errno, nget = 1; |
| 153 |
|
| 154 |
return_if_fail(eventloop != NULL); |
| 155 |
|
| 156 |
priv = eventloop->poller; |
| 157 |
|
| 158 |
ret = port_getn(priv->port_fd, priv->pfd, priv->pfd_size, &nget, |
| 159 |
delay >= 0 ? &(struct timespec) { .tv_sec = delay / 1000, .tv_nsec = delay % 1000 * 1000000 } : NULL); |
| 160 |
|
| 161 |
o_errno = errno; |
| 162 |
mowgli_eventloop_synchronize(eventloop); |
| 163 |
|
| 164 |
if (ret == -1) |
| 165 |
{ |
| 166 |
if (mowgli_eventloop_ignore_errno(o_errno)) |
| 167 |
return; |
| 168 |
|
| 169 |
mowgli_log("mowgli_ports_eventloop_select(): port_getn failed: %d (%s)", o_errno, strerror(o_errno)); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
for (i = 0; i < nget; i++) |
| 174 |
{ |
| 175 |
mowgli_eventloop_pollable_t *pollable = priv->pfd[i].portev_user; |
| 176 |
|
| 177 |
if (priv->pfd[i].portev_source != PORT_SOURCE_FD) |
| 178 |
continue; |
| 179 |
|
| 180 |
if (priv->pfd[i].portev_events & (POLLIN | POLLHUP | POLLERR) && !pollable->removed) |
| 181 |
mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_READ); |
| 182 |
|
| 183 |
if (priv->pfd[i].portev_events & (POLLOUT | POLLHUP | POLLERR) && !pollable->removed) |
| 184 |
mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_WRITE); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
mowgli_eventloop_ops_t _mowgli_ports_pollops = |
| 189 |
{ |
| 190 |
.timeout_once = mowgli_simple_eventloop_timeout_once, |
| 191 |
.run_once = mowgli_simple_eventloop_run_once, |
| 192 |
.pollsetup = mowgli_ports_eventloop_pollsetup, |
| 193 |
.pollshutdown = mowgli_ports_eventloop_pollshutdown, |
| 194 |
.setselect = mowgli_ports_eventloop_setselect, |
| 195 |
.select = mowgli_ports_eventloop_select, |
| 196 |
.destroy = mowgli_ports_eventloop_destroy, |
| 197 |
}; |
| 198 |
|
| 199 |
#endif |