source: example/io/eventfds.c @ 3606fe4

ADTast-experimentalpthread-emulation
Last change on this file since 3606fe4 was 8da377d3, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Added io_uring example for eventfds.

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[8da377d3]1/*
2This is a simple "eventfd" example that uses io_uring
3It demonstrates that reads work as expected on eventfds.
4It uses liburing for simplicity.
5*/
6
7
8#ifndef _GNU_SOURCE
9#define _GNU_SOURCE
10#endif
11
12#include <fcntl.h>
13#include <liburing.h>
14#include <stdio.h>
15#include <string.h>
16#include <unistd.h>
17#include <sys/eventfd.h>
18
19struct io_uring ring;
20
21
22int main(int argc,  char * argv[]) {
23      int fd = eventfd(0, 0);
24      if(fd < 0) {
25            printf("Could not open event fd.\n");
26            return 2;
27      }
28
29      /* prep the array */
30        char buf[sizeof(uint64_t)] = { 0 };
31      struct iovec iov = { buf, 8 };
32
33        {
34            /* Do one write so we can compare blocking behaviour */
35                eventfd_t val;
36                val = 1;
37                eventfd_write( fd, val );
38        }
39
40      /* init liburing */
41      io_uring_queue_init(256, &ring, 0);
42
43      /* declare required structs */
44      struct io_uring_sqe * sqe;
45      struct io_uring_cqe * cqe;
46
47      /* get an sqe and fill in a READ operation */
48      sqe = io_uring_get_sqe(&ring);
49        io_uring_prep_read(sqe, fd, buf, 8, 0);
50
51      sqe->user_data = fd;
52
53      /* tell the kernel we have an sqe ready for consumption */
54      io_uring_submit(&ring);
55
56        printf("First wait\n");
57
58      /* wait for the sqe to complete */
59      int ret = io_uring_wait_cqe(&ring, &cqe);
60
61      /* read and process cqe event */
62      if(ret == 0) {
63            signed int len = cqe->res;
64            io_uring_cqe_seen(&ring, cqe);
65                printf("%d ", len);
66            if(len > 0) {
67                  printf("%.*s", len, buf);
68            }
69            else if( len < 0 ) {
70                  fprintf(stderr, "readv/read returned error : %s\n", strerror(-len));
71            }
72      }
73      else {
74            printf("%d\n", ret);
75            io_uring_cqe_seen(&ring, cqe);
76      }
77
78      /* ============================================ */
79      /* DO it again so the we can compare behaviour. */
80      /* ============================================ */
81
82      /* get an sqe and fill in a READ operation */
83      sqe = io_uring_get_sqe(&ring);
84        io_uring_prep_read(sqe, fd, buf, 8, 0);
85
86      sqe->user_data = fd;
87
88        printf("Second wait\n");
89
90      /* tell the kernel we have an sqe ready for consumption */
91      io_uring_submit(&ring);
92
93      /* wait for the sqe to complete */
94      ret = io_uring_wait_cqe(&ring, &cqe);
95
96      /* read and process cqe event */
97      if(ret == 0) {
98            signed int len = cqe->res;
99            io_uring_cqe_seen(&ring, cqe);
100                printf("%d ", len);
101            if(len > 0) {
102                  printf("%.*s", len, buf);
103            }
104            else if( len < 0 ) {
105                  fprintf(stderr, "readv/read returned error : %s\n", strerror(-len));
106            }
107      }
108      else {
109            printf("%d\n", ret);
110            io_uring_cqe_seen(&ring, cqe);
111      }
112
113      io_uring_queue_exit(&ring);
114
115      close(fd);
116}
Note: See TracBrowser for help on using the repository browser.