aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/options/posix/generic/sys-select-stubs.cpp
blob: e8ff92767420a7fe19c6af8d92cd95db97845692 (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

#include <string.h>
#include <sys/select.h>
#include <unistd.h>
#include <errno.h>

#include <bits/ensure.h>
#include <mlibc-config.h>

#include <mlibc/posix-sysdeps.hpp>

void __FD_CLR(int fd, fd_set *set) {
	__ensure(fd < FD_SETSIZE);
	set->__mlibc_elems[fd / 8] &= ~(1 << (fd % 8));
}
int __FD_ISSET(int fd, fd_set *set) {
	__ensure(fd < FD_SETSIZE);
	return set->__mlibc_elems[fd / 8] & (1 << (fd % 8));
}
void __FD_SET(int fd, fd_set *set) {
	__ensure(fd < FD_SETSIZE);
	set->__mlibc_elems[fd / 8] |= 1 << (fd % 8);
}
void __FD_ZERO(fd_set *set) {
	memset(set->__mlibc_elems, 0, sizeof(fd_set));
}

int select(int num_fds, fd_set *__restrict read_set, fd_set *__restrict write_set,
		fd_set *__restrict except_set, struct timeval *__restrict timeout) {
	int num_events = 0;
	struct timespec timeouts = {};
	struct timespec *timeout_ptr = NULL;
	if (timeout) {
		timeouts.tv_sec = timeout->tv_sec;
		timeouts.tv_nsec = timeout->tv_usec * 1000;
		timeout_ptr = &timeouts;
	}

    MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pselect, -1);
	if(int e = mlibc::sys_pselect(num_fds, read_set, write_set, except_set,
				timeout_ptr, NULL, &num_events); e) {
		errno = e;
		return -1;
	}
	return num_events;
}

int pselect(int num_fds, fd_set *read_set, fd_set *write_set, fd_set *except_set,
		const struct timespec *timeout,	const sigset_t *sigmask) {
	int num_events = 0;
	MLIBC_CHECK_OR_ENOSYS(mlibc::sys_pselect, -1);
	if(int e = mlibc::sys_pselect(num_fds, read_set, write_set, except_set,
				timeout, sigmask, &num_events); e) {
		errno = e;
		return -1;
	}
	return num_events;
}