summaryrefslogtreecommitdiff
path: root/lib/libc/aio/aio_suspend.c
blob: 89afe9fda8591ec41f66f0175ed61aaddec3da74 (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
#include "linux/io_uring.h" // for IORING_ENTER_GETEVENTS
#include "stddef.h"	    // for NULL

#include <__aio.h>    // for __aio_lookup, __aio_poll, AIO_REQUEST_ST...
#include <aio.h>      // for timespec, aio_suspend
#include <errno.h>    // for errno, EAGAIN, EINVAL
#include <io_uring.h> // for __io_uring, io_uring, io_uring_enter
#include <poll.h>     // for pollfd, poll, POLLIN
#include <stdint.h>   // for uint64_t
#include <time.h>
#include <unistd.h> // for read

int aio_suspend(const struct aiocb *const list[], int nent, const struct timespec *timeout)
{
	struct pollfd pfd;
	struct aio_request *req;

	if ((void *)list == NULL || nent <= 0) {
		errno = EINVAL;
		return -1;
	}

	while (1) {
		int timeout_ms, ret;
		__aio_poll();

		for (int i = 0; i < nent; i++) {
			req = __aio_lookup((struct aiocb *)list[i]);
			if (req && req->status == AIO_REQUEST_STATUS_COMPLETED)
				return 0;
		}

		pfd.fd = __io_uring.eventfd;
		pfd.events = POLLIN;

		if (timeout) {
			timeout_ms = timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000;
		} else {
			timeout_ms = -1;
		}

		ret = poll(&pfd, 1, timeout_ms);

		if (ret == 0) {
			errno = EAGAIN;
			return -1;
		}

		if (ret < 0) {
			return -1;
		}

		uint64_t val;
		read(__io_uring.eventfd, &val, sizeof(val));
		io_uring_enter(__io_uring.fd, 0, 0, IORING_ENTER_GETEVENTS, NULL, 0);
	}
}