source: example/io/attach-block.c @ c4f8170

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since c4f8170 was 5869cea, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added new example that demonstrate that attached ring don't have special behavior for polling.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/*
2This is an example that uses io_uring with attach mode.
3It demonstrates the what happens if some thread submits to
4the original ring and then spins, while a second thread waits
5on the attached ring. This deadlocks because even if rings are
6attached, they don't share a completion queue. Therefore all
7completion queues must be polled for progress to be made.
8
9It uses liburing for simplicity.
10*/
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include <errno.h>
17#include <fcntl.h>
18#include <unistd.h>
19
20#include <pthread.h>
21#include <liburing.h>
22
23volatile bool done = false;
24
25void * submitter(void * arg) {
26        struct io_uring * ring = (struct io_uring *)arg;
27
28        const char * path = __FILE__;
29
30        int fd = open(path, 0, O_RDONLY);
31        if( fd < 0) {
32                fprintf(stderr, "Can't open file: (%d) %s\n", errno, strerror(errno));
33                exit(EXIT_FAILURE);
34        }
35        printf("Opened file: %d\n", fd);
36
37        char buffer[1024];
38        struct io_uring_sqe * sqe = io_uring_get_sqe(ring);
39        io_uring_prep_read(sqe, fd, buffer, 1024, 0);
40        io_uring_submit(ring);
41
42        printf("Submitted read\n");
43
44        printf("Spinning\n");
45        while(!__atomic_load_n(&done, __ATOMIC_SEQ_CST));
46
47        return NULL;
48}
49
50void * poller(void * arg) {
51        struct io_uring * ring = (struct io_uring *)arg;
52        struct io_uring_cqe *cqe;
53
54        printf("Waiting for results\n");
55
56        int ret = io_uring_wait_cqe(ring, &cqe);
57        if( ret < 0) {
58                fprintf(stderr, "ioring wait failed: (%d) %s\n", -ret, strerror(-ret));
59                exit(EXIT_FAILURE);
60        }
61        io_uring_cqe_seen(ring, cqe);
62
63        printf("Got result\n");
64
65        __atomic_store_n(&done, true, __ATOMIC_SEQ_CST);
66        return NULL;
67}
68
69int main() {
70        struct io_uring base_ring;
71        io_uring_queue_init(8, &base_ring, 0);
72        printf("Created first ring: %d\n", base_ring.ring_fd);
73
74        struct io_uring attached_ring;
75        struct io_uring_params par;
76        memset(&par, '\0', sizeof(struct io_uring_params));
77        par.flags |= IORING_SETUP_ATTACH_WQ;
78        par.wq_fd = base_ring.ring_fd;
79        io_uring_queue_init_params(8, &attached_ring, &par);
80        printf("Attached second ring: %d\n", attached_ring.ring_fd);
81
82        printf("Forking\n");
83        pthread_t s, p;
84
85        int ret = pthread_create(&s, NULL, submitter, &base_ring);
86        if( ret < 0) {
87                fprintf(stderr, "pthread create 1 error: (%d) %s\n", errno, strerror(errno));
88                exit(EXIT_FAILURE);
89        }
90
91        ret = pthread_create(&p, NULL, poller, &attached_ring);
92        if( ret < 0) {
93                fprintf(stderr, "pthread create 2 error: (%d) %s\n", errno, strerror(errno));
94                exit(EXIT_FAILURE);
95        }
96
97        ret = pthread_join(p, NULL);
98        if( ret < 0) {
99                fprintf(stderr, "pthread join 2 error: (%d) %s\n", errno, strerror(errno));
100                exit(EXIT_FAILURE);
101        }
102
103        ret = pthread_join(s, NULL);
104        if( ret < 0) {
105                fprintf(stderr, "pthread join 1 error: (%d) %s\n", errno, strerror(errno));
106                exit(EXIT_FAILURE);
107        }
108}
Note: See TracBrowser for help on using the repository browser.