aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/thread-exit.c
blob: 7c83e4f549feca9c9c4e3bf950a2ccce743f3f49 (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test that thread pool issued requests don't cancel on thread
 *		exit, but do get canceled once the parent exits. Do both
 *		writes that finish and a poll request that sticks around.
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>

#include "helpers.h"
#include "liburing.h"

#define NR_IOS	8
#define WSIZE	512

struct d {
	int fd;
	struct io_uring *ring;
	unsigned long off;
	int pipe_fd;
	int err;
	int i;
};

static char *g_buf[NR_IOS] = {NULL};

static void free_g_buf(void)
{
	int i;
	for (i = 0; i < NR_IOS; i++)
		free(g_buf[i]);
}

static void *do_io(void *data)
{
	struct d *d = data;
	struct io_uring_sqe *sqe;
	char *buffer;
	int ret;

	buffer = t_malloc(WSIZE);
	g_buf[d->i] = buffer;
	memset(buffer, 0x5a, WSIZE);
	sqe = io_uring_get_sqe(d->ring);
	if (!sqe) {
		d->err++;
		return NULL;
	}
	io_uring_prep_write(sqe, d->fd, buffer, WSIZE, d->off);
	sqe->user_data = d->off;

	sqe = io_uring_get_sqe(d->ring);
	if (!sqe) {
		d->err++;
		return NULL;
	}
	io_uring_prep_poll_add(sqe, d->pipe_fd, POLLIN);

	ret = io_uring_submit(d->ring);
	if (ret != 2)
		d->err++;
	return NULL;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	const char *fname;
	pthread_t thread;
	int ret, do_unlink, i, fd;
	struct d d;
	int fds[2];

	if (pipe(fds) < 0) {
		perror("pipe");
		return 1;
	}

	ret = io_uring_queue_init(32, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup failed\n");
		return 1;
	}

	if (argc > 1) {
		fname = argv[1];
		do_unlink = 0;
	} else {
		fname = ".thread.exit";
		do_unlink = 1;
		t_create_file(fname, 4096);
	}

	fd = open(fname, O_WRONLY);
	if (do_unlink)
		unlink(fname);
	if (fd < 0) {
		if (errno == EPERM || errno == EACCES)
			goto skip;
		perror("open");
		return 1;
	}

	d.fd = fd;
	d.ring = &ring;
	d.off = 0;
	d.pipe_fd = fds[0];
	d.err = 0;
	for (i = 0; i < NR_IOS; i++) {
		d.i = i;
		memset(&thread, 0, sizeof(thread));
		pthread_create(&thread, NULL, do_io, &d);
		pthread_join(thread, NULL);
		d.off += WSIZE;
	}

	for (i = 0; i < NR_IOS; i++) {
		struct io_uring_cqe *cqe;

		ret = io_uring_wait_cqe(&ring, &cqe);
		if (ret) {
			fprintf(stderr, "io_uring_wait_cqe=%d\n", ret);
			goto err;
		}
		if (cqe->res != WSIZE) {
			fprintf(stderr, "cqe->res=%d, Expected %d\n", cqe->res,
								WSIZE);
			goto err;
		}
		io_uring_cqe_seen(&ring, cqe);
	}

	free_g_buf();
	return d.err;
err:
	free_g_buf();
	return 1;
skip:
	free_g_buf();
	return T_EXIT_SKIP;
}