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