summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2019-10-24 19:17:58 -0600
committerJens Axboe <axboe@kernel.dk>2019-10-24 19:17:58 -0600
commita769aa109c7e1368c8a80c92ce8849ce3e3322d4 (patch)
tree0b9337339c97af2518406405517fc228b3084ade
parent1c8734fd8d851227e6da997c731f9aaa2f0d62f9 (diff)
downloadliburing-a769aa109c7e1368c8a80c92ce8849ce3e3322d4.tar.gz
liburing-a769aa109c7e1368c8a80c92ce8849ce3e3322d4.tar.bz2
test/send_recvmsg: hopefully make it reliable
Split the recv part in prep, sync, then receive. This should make it more reliable, hopefully. Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--test/Makefile1
-rw-r--r--test/send_recvmsg.c90
2 files changed, 50 insertions, 41 deletions
diff --git a/test/Makefile b/test/Makefile
index 93249d4..412fe99 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -30,6 +30,7 @@ test_objs := $(patsubst %.c,%.ol,$(test_srcs))
35fa71a030ca-test: XCFLAGS = -lpthread
232c93d07b74-test: XCFLAGS = -lpthread
+send_recvmsg: XCFLAGS = -lpthread
clean:
rm -f $(all_targets) $(test_objs)
diff --git a/test/send_recvmsg.c b/test/send_recvmsg.c
index 956fcfb..9d220eb 100644
--- a/test/send_recvmsg.c
+++ b/test/send_recvmsg.c
@@ -8,6 +8,7 @@
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <pthread.h>
#include "liburing.h"
@@ -18,33 +19,13 @@ static char str[] = "This is a test of sendmsg and recvmsg over io_uring!";
#define PORT 10200
#define HOST "127.0.0.1"
-static void sig_alrm(int sig)
-{
- exit(0);
-}
-
-static int do_recvmsg(void)
+static int recv_prep(struct io_uring *ring, struct iovec *iov)
{
struct sockaddr_in saddr;
- char buf[MAX_MSG + 1];
struct msghdr msg;
- struct iovec iov = {
- .iov_base = buf,
- .iov_len = sizeof(buf) - 1,
- };
- struct io_uring ring;
- struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
int sockfd, ret;
- signal(SIGALRM, sig_alrm);
-
- ret = io_uring_queue_init(1, &ring, 0);
- if (ret) {
- printf("queue init fail\n");
- return 1;
- }
-
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -64,22 +45,29 @@ static int do_recvmsg(void)
memset(&msg, 0, sizeof(msg));
msg.msg_namelen = sizeof(struct sockaddr_in);
- msg.msg_iov = &iov;
+ msg.msg_iov = iov;
msg.msg_iovlen = 1;
- sqe = io_uring_get_sqe(&ring);
+ sqe = io_uring_get_sqe(ring);
io_uring_prep_recvmsg(sqe, sockfd, &msg, 0);
- ret = io_uring_submit(&ring);
+ ret = io_uring_submit(ring);
if (ret <= 0) {
printf("submit failed\n");
goto err;
}
- /* we may never get the data, ensure we exit */
- alarm(1);
+ return 0;
+err:
+ close(sockfd);
+ return 1;
+}
- ret = io_uring_wait_cqe(&ring, &cqe);
+static int do_recvmsg(struct io_uring *ring, struct iovec *iov)
+{
+ struct io_uring_cqe *cqe;
+
+ io_uring_wait_cqe(ring, &cqe);
if (cqe->res < 0) {
printf("failed cqe: %d\n", cqe->res);
goto err;
@@ -90,18 +78,36 @@ static int do_recvmsg(void)
goto err;
}
- if (strcmp(str, iov.iov_base)) {
+ if (strcmp(str, iov->iov_base)) {
printf("string mismatch\n");
goto err;
}
- close(sockfd);
return 0;
err:
- close(sockfd);
return 1;
}
+static void *recv_fn(void *data)
+{
+ pthread_mutex_t *mutex = data;
+ char buf[MAX_MSG + 1];
+ struct iovec iov = {
+ .iov_base = buf,
+ .iov_len = sizeof(buf) - 1,
+ };
+ struct io_uring ring;
+
+ io_uring_queue_init(1, &ring, 0);
+
+ recv_prep(&ring, &iov);
+ pthread_mutex_unlock(mutex);
+ do_recvmsg(&ring, &iov);
+
+ io_uring_queue_exit(&ring);
+ return NULL;
+}
+
static int do_sendmsg(void)
{
struct sockaddr_in saddr;
@@ -162,21 +168,23 @@ err:
int main(int argc, char *argv[])
{
- pid_t pid;
+ pthread_mutexattr_t attr;
+ pthread_t recv_thread;
+ pthread_mutex_t mutex;
int ret;
- pid = fork();
- switch (pid) {
- case -1:
- printf("fork() failed\n");
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_setpshared(&attr, 1);
+ pthread_mutex_init(&mutex, &attr);
+ pthread_mutex_lock(&mutex);
+
+ ret = pthread_create(&recv_thread, NULL, recv_fn, &mutex);
+ if (ret) {
+ fprintf(stderr, "Thread create failed\n");
return 1;
- case 0:
- ret = do_sendmsg();
- break;
- default:
- ret = do_recvmsg();
- break;
}
+ pthread_mutex_lock(&mutex);
+ do_sendmsg();
return ret;
}