1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Test that the sqthread goes to sleep around the specified time, and that
4 * the NEED_WAKEUP flag is then set.
5 */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/time.h>
11 #include "liburing.h"
12
mtime_since(const struct timeval * s,const struct timeval * e)13 static unsigned long long mtime_since(const struct timeval *s,
14 const struct timeval *e)
15 {
16 long long sec, usec;
17
18 sec = e->tv_sec - s->tv_sec;
19 usec = (e->tv_usec - s->tv_usec);
20 if (sec > 0 && usec < 0) {
21 sec--;
22 usec += 1000000;
23 }
24
25 sec *= 1000;
26 usec /= 1000;
27 return sec + usec;
28 }
29
mtime_since_now(struct timeval * tv)30 static unsigned long long mtime_since_now(struct timeval *tv)
31 {
32 struct timeval end;
33
34 gettimeofday(&end, NULL);
35 return mtime_since(tv, &end);
36 }
37
main(int argc,char * argv[])38 int main(int argc, char *argv[])
39 {
40 struct io_uring_params p = {};
41 struct timeval tv;
42 struct io_uring ring;
43 int ret;
44
45 if (argc > 1)
46 return 0;
47
48 p.flags = IORING_SETUP_SQPOLL;
49 p.sq_thread_idle = 100;
50
51 ret = io_uring_queue_init_params(1, &ring, &p);
52 if (ret) {
53 if (geteuid()) {
54 printf("%s: skipped, not root\n", argv[0]);
55 return 0;
56 }
57 fprintf(stderr, "queue_init=%d\n", ret);
58 return 1;
59 }
60
61 gettimeofday(&tv, NULL);
62 do {
63 usleep(1000);
64 if ((*ring.sq.kflags) & IORING_SQ_NEED_WAKEUP)
65 return 0;
66 } while (mtime_since_now(&tv) < 1000);
67
68 return 1;
69 }
70