diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-07-07 04:52:30 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-07-07 04:52:30 +0000 |
commit | 11c8c4dfa4d268bef66f636e126725e46624b7a7 (patch) | |
tree | ebe9c872e416346e4b333e5062da32401c8e0a73 /test/read-before-exit.c | |
parent | d53db6851ea17b2d219d084e1afc683b8b62b105 (diff) | |
parent | 1d27ff1934c5c4292dc00fba7f7f8ae411ed42f5 (diff) | |
download | liburing-11c8c4dfa4d268bef66f636e126725e46624b7a7.tar.gz |
Snap for 10453563 from 1d27ff1934c5c4292dc00fba7f7f8ae411ed42f5 to mainline-media-swcodec-releaseaml_swc_341711000aml_swc_341619000aml_swc_341513600aml_swc_341312300aml_swc_341312020aml_swc_341111000aml_swc_341011020aml_swc_340922010android14-mainline-media-swcodec-release
Change-Id: I7cca6554b0f16e2b5808963242c9da276f36c14d
Diffstat (limited to 'test/read-before-exit.c')
-rw-r--r-- | test/read-before-exit.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/test/read-before-exit.c b/test/read-before-exit.c new file mode 100644 index 0000000..be36bd4 --- /dev/null +++ b/test/read-before-exit.c @@ -0,0 +1,112 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Description: test if issuing IO from thread and immediately exiting will + * proceed correctly. + * + * Original test case from: https://github.com/axboe/liburing/issues/582 + */ +#include <unistd.h> +#include <pthread.h> +#include <sys/timerfd.h> +#include <string.h> +#include <stdio.h> + +#include "liburing.h" +#include "helpers.h" + +struct data { + struct io_uring *ring; + int timer_fd1; + int timer_fd2; + uint64_t buf1; + uint64_t buf2; +}; + +void *submit(void *data) +{ + struct io_uring_sqe *sqe; + struct data *d = data; + int ret; + + sqe = io_uring_get_sqe(d->ring); + io_uring_prep_read(sqe, d->timer_fd1, &d->buf1, sizeof(d->buf1), 0); + + sqe = io_uring_get_sqe(d->ring); + io_uring_prep_read(sqe, d->timer_fd2, &d->buf2, sizeof(d->buf2), 0); + + ret = io_uring_submit(d->ring); + if (ret != 2) + return (void *) (uintptr_t) 1; + + /* Exit suddenly. */ + return NULL; +} + +static int test(int flags) +{ + struct io_uring_params params = { .flags = flags, }; + struct io_uring ring; + struct data d = { .ring = &ring, }; + pthread_t thread; + void *res; + int ret; + + ret = t_create_ring_params(8, &ring, ¶ms); + if (ret == T_SETUP_SKIP) + return 0; + else if (ret != T_SETUP_OK) + return 1; + + d.timer_fd1 = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); + if (d.timer_fd1 < 0) { + perror("timerfd_create"); + return 1; + } + d.timer_fd2 = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC); + if (d.timer_fd2 < 0) { + perror("timerfd_create"); + return 1; + } + + pthread_create(&thread, NULL, submit, &d); + pthread_join(thread, &res); + + /** Wait for completions and do stuff ... **/ + + io_uring_queue_exit(&ring); + + close(d.timer_fd1); + close(d.timer_fd2); + return !!res; +} + +int main(int argc, char *argv[]) +{ + int ret, i; + + for (i = 0; i < 1000; i++) { + ret = test(0); + if (ret) { + fprintf(stderr, "Test failed\n"); + return ret; + } + } + + for (i = 0; i < 1000; i++) { + ret = test(IORING_SETUP_IOPOLL); + if (ret) { + fprintf(stderr, "Test IOPOLL failed\n"); + return ret; + } + } + + for (i = 0; i < 100; i++) { + ret = test(IORING_SETUP_SQPOLL); + if (ret) { + fprintf(stderr, "Test SQPOLL failed\n"); + return ret; + } + } + + return 0; +} |