summaryrefslogtreecommitdiff
path: root/src/sched.c
blob: 3618f115eb19bee00f857df5ed508fa8aed4cb73 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Copyright (c) 2008, 2009
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 *      Micah Cowan (micah@cowan.name)
 *      Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 * Copyright (c) 1987 Oliver Laumann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, 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 General Public License
 * along with this program (see the file COPYING); if not, see
 * https://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 ****************************************************************
 */

#include "config.h"

#include "sched.h"

#include <poll.h>
#include <stdint.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>

#include "screen.h"

static Event *evs;
static Event *tevs;
static Event *nextev;
static int calctimeout;
static struct pollfd *pfd;
static int pfd_cnt;


static Event *calctimo(void);

void evenq(Event *ev)
{
	int i = 0;

	Event *evp, **evpp;
	if (ev->queued)
		return;
	evpp = &evs;
	if (ev->type == EV_TIMEOUT) {
		calctimeout = 1;
		evpp = &tevs;
	}

	for (; (evp = *evpp); evpp = &evp->next)
		if (ev->priority > evp->priority)
			break;
	ev->next = evp;
	*evpp = ev;
	ev->queued = true;

	/* check if we need more pollfd */
	for (evp = evs; evp; evp = evp->next)
		if (evp->type == EV_READ || evp->type == EV_WRITE)
			i++;

	if (i > pfd_cnt) {
		pfd_cnt = i;
		pfd = realloc(pfd, pfd_cnt * sizeof(struct pollfd));
	}
}

void evdeq(Event *ev)
{
	Event *evp, **evpp;
	if (!ev || !ev->queued)
		return;
	evpp = &evs;
	if (ev->type == EV_TIMEOUT) {
		calctimeout = 1;
		evpp = &tevs;
	}
	for (; (evp = *evpp); evpp = &evp->next)
		if (evp == ev)
			break;
	*evpp = ev->next;
	ev->queued = false;
	if (ev == nextev)
		nextev = nextev->next;

	/* mark fd to be skipped (see checks in sched()) */
	for (int i = 0; i < pfd_cnt; i++)
		if (pfd[i].fd == ev->fd)
			pfd[i].fd = -pfd[i].fd;
}

static Event *calctimo(void)
{
	Event *ev, *min;
	long mins;

	if ((min = tevs) == NULL)
		return NULL;
	mins = min->timeout;
	for (ev = tevs->next; ev; ev = ev->next) {
		if (mins > ev->timeout) {
			min = ev;
			mins = ev->timeout;
		}
	}
	return min;
}

void sched(void)
{
	Event *ev;
	Event *timeoutev = NULL;
	int timeout;
	int i, n;

	for (;;) {
		if (calctimeout)
			timeoutev = calctimo();
		if (timeoutev) {
			struct timeval now;
			gettimeofday(&now, NULL);
			/* tp - timeout */
			timeout = timeoutev->timeout - (now.tv_sec * 1000 + now.tv_usec / 1000);
			if (timeout < 0)
				timeout = 0;
		}

		memset(pfd, 0, sizeof(struct pollfd) * pfd_cnt);
		i = 0;
		for (ev = evs; ev; ev = ev->next) {
			if (ev->condpos && *ev->condpos <= (ev->condneg ? *ev->condneg : 0))
				goto skip;
			if (ev->type == EV_READ) {
				pfd[i].fd = ev->fd;
				pfd[i].events = POLLIN;
			} else if (ev->type == EV_WRITE) {
				pfd[i].fd = ev->fd;
				pfd[i].events = POLLOUT;
			}
skip:
			if (ev->type == EV_READ || ev->type == EV_WRITE)
				i++;
		}

		n = poll(pfd, i, timeoutev ? timeout : 1000);
		if (n < 0) {
			if (errno != EINTR) {
				Panic(errno, "poll");
			}
			n = 0;
		} else if (n == 0) {	/* timeout */
			if (timeoutev) {
				evdeq(timeoutev);
				timeoutev->handler(timeoutev, timeoutev->data);
			}
		}

		i = 0;

		for (ev = evs; ev; ev = nextev) {
			nextev = ev->next;
			switch (ev->type) {
			case EV_READ:
			case EV_WRITE:
				/* check if we parsed all events from poll()
				 * if we did just continue, as we may still
				 * need to run EV_ALWAYS event */
				if (n == 0)
					continue;
				/* check if we have anything to do for EV_READ
				 * or EV_WRITE, or if event is still queued,
				 * if not skip to the next event */
				if (!pfd[i].revents || pfd[i].fd < 0) {
					i++;
					continue;
				}
				/* this is one of events from poll(), decrease
				 * counter */
				n--;
				/* advance pollfd pointer */
				i++;
				__attribute__ ((fallthrough));
			default:
				if (ev->condpos && *ev->condpos <= (ev->condneg ? *ev->condneg : 0))
					continue;
				ev->handler(ev, ev->data);
			}
		}
	}
}

void SetTimeout(Event *ev, int timo)
{
	struct timeval now;
	gettimeofday(&now, NULL);

	ev->timeout = (now.tv_sec * 1000 + now.tv_usec / 1000) + timo;

	if (ev->queued)
		calctimeout = 1;
}