2
* ivykis, an event handling library
3
* Copyright (C) 2002, 2003, 2009 Lennert Buytenhek
4
* Dedicated to Marija Kulikova.
6
* This library is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License version
8
* 2.1 as published by the Free Software Foundation.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License version 2.1 for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License version 2.1 along with this library; if not, write to the
17
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18
* Boston, MA 02110-1301, USA.
27
#include "iv_private.h"
38
#define SET_IN (POLLIN | POLLPRI | POLLHUP | POLLRDNORM | \
39
POLLRDBAND | POLLMSG | POLLRDHUP)
40
#define SET_OUT (POLLOUT | POLLWRNORM | POLLWRBAND)
41
#define SET_ERR (POLLERR)
47
int num_registered_fds;
51
#define pfds __tls_deref(pfds)
52
#define fds __tls_deref(fds)
53
#define num_registered_fds __tls_deref(num_registered_fds)
55
static int iv_poll_init(int maxfd)
57
pfds = malloc(maxfd * sizeof(struct pollfd));
61
fds = malloc(maxfd * sizeof(struct iv_fd_ *));
67
num_registered_fds = 0;
72
static void iv_poll_poll(int numfds, struct list_head *active, int msec)
79
* AIX sometimes leaves errno uninitialized even if poll
85
ret = poll(pfds, num_registered_fds, msec);
90
fprintf(stderr, "iv_poll_poll: got error %d[%s]", errno,
95
for (i = 0; i < num_registered_fds; i++) {
96
struct iv_fd_ *fd = fds[i];
98
if (pfds[i].revents & (SET_IN | SET_ERR))
99
iv_fd_make_ready(active, fd, MASKIN);
100
if (pfds[i].revents & (SET_OUT | SET_ERR))
101
iv_fd_make_ready(active, fd, MASKOUT);
102
if (pfds[i].revents & SET_ERR)
103
iv_fd_make_ready(active, fd, MASKERR);
107
static void iv_poll_register_fd(struct iv_fd_ *fd)
112
static int bits_to_poll_mask(int bits)
127
static void iv_poll_notify_fd(struct iv_fd_ *fd, int wanted)
129
if (fd->registered_bands == wanted)
132
if (fd->index == -1 && wanted) {
133
fd->index = num_registered_fds++;
134
pfds[fd->index].fd = fd->fd;
135
pfds[fd->index].events = bits_to_poll_mask(wanted);
137
} else if (fd->index != -1 && !wanted) {
138
if (fd->index != num_registered_fds - 1) {
139
struct iv_fd_ *last = fds[num_registered_fds - 1];
141
last->index = fd->index;
142
pfds[fd->index] = pfds[num_registered_fds - 1];
143
fds[fd->index] = last;
145
num_registered_fds--;
149
pfds[fd->index].events = bits_to_poll_mask(wanted);
152
fd->registered_bands = wanted;
155
static void iv_poll_deinit(void)
162
struct iv_poll_method iv_method_poll = {
164
.init = iv_poll_init,
165
.poll = iv_poll_poll,
166
.register_fd = iv_poll_register_fd,
167
.notify_fd = iv_poll_notify_fd,
168
.deinit = iv_poll_deinit,