summaryrefslogtreecommitdiffstats
path: root/src/evpoll.c
blob: 087955f554220588fa2e41092ff09035af5fd46a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * (C) 2016 by Holger Hans Peter Freyther
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "evpoll.h"

#include <osmocom/core/linuxlist.h>
#include <osmocom/core/select.h>
#include <osmocom/core/timer.h>

#include <sys/select.h>

/* based on osmo_select_main GPLv2+ so combined compatible with AGPLv3+ */
int evpoll(struct pollfd *fds, nfds_t nfds, int timeout)
{
	struct timeval *tv, null_tv = { 0, 0} , poll_tv;
	fd_set readset, writeset, exceptset;
	int maxfd, rc, i;

	FD_ZERO(&readset);
	FD_ZERO(&writeset);
	FD_ZERO(&exceptset);

	/* prepare read and write fdsets */
	maxfd = osmo_fd_fill_fds(&readset, &writeset, &exceptset);

	for (i = 0; i < nfds; ++i) {
		if (fds[i].fd < 0)
			continue;
		if ((fds[i].events & (POLLIN | POLLOUT | POLLPRI)) == 0)
			continue;

		/* copy events. Not sure why glib maps PRI to exceptionset */
		if (fds[i].events & POLLIN)
			FD_SET(fds[i].fd, &readset);
		if (fds[i].events & POLLOUT)
			FD_SET(fds[i].fd, &writeset);
		if (fds[i].events & POLLPRI)
			FD_SET(fds[i].fd, &exceptset);

		if (fds[i].fd > maxfd)
			maxfd = fds[i].fd;
	}


	osmo_timers_check();
	osmo_timers_prepare();

	/*
	 * 0 == wake-up immediately
	 * -1 == block forever.. which will depend on our timers
	 */
	if (timeout == 0) {
		tv = &null_tv;
	} else if (timeout == -1) {
		tv = osmo_timers_nearest();
	} else {
		poll_tv.tv_sec = timeout / 1000;
		poll_tv.tv_usec = (timeout % 1000) * 1000;

		tv = osmo_timers_nearest();
		if (!tv)
			tv = &poll_tv;
		else if (timercmp(&poll_tv, tv, <))
			tv = &poll_tv;
	}

	rc = select(maxfd+1, &readset, &writeset, &exceptset, tv);
	if (rc < 0)
		return 0;

	/* fire timers */
	osmo_timers_update();

	/* call registered callback functions */
	osmo_fd_disp_fds(&readset, &writeset, &exceptset);

	for (i = 0; i < nfds; ++i) {
		fds[i].revents = 0;

		if (fds[i].fd < 0)
			continue;

		if (FD_ISSET(fds[i].fd, &readset))
			fds[i].revents = POLLIN | POLLERR;
		if (FD_ISSET(fds[i].fd, &writeset))
			fds[i].revents |= POLLOUT;
		if (FD_ISSET(fds[i].fd, &exceptset))
			fds[i].revents |= POLLPRI;
	}

	return rc;
}