Skip to content

Instantly share code, notes, and snippets.

@CarterLi CarterLi/test_signalfd.c
Last active Sep 18, 2019

Embed
What would you like to do?
A simple test case for liburing, and a working example for epoll
#include <liburing.h>
#include <libaio.h>
#include <unistd.h>
#include <sys/signalfd.h>
#include <sys/epoll.h>
#include <sys/poll.h>
#include <stdio.h>
int setup_signal() {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigprocmask(SIG_BLOCK, &mask, NULL);
int sfd = signalfd(-1, &mask, SFD_NONBLOCK);
return sfd;
}
void test_uring(int sfd) {
struct io_uring ring;
io_uring_queue_init(32, &ring, 0);
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
io_uring_prep_poll_add(sqe, sfd, POLLIN);
io_uring_submit(&ring);
struct io_uring_cqe *cqe;
io_uring_wait_cqe(&ring, &cqe);
io_uring_cqe_seen(&ring, cqe);
io_uring_queue_exit(&ring);
}
void test_aio(int sfd) {
io_context_t context;
io_queue_init(32, &context);
struct iocb iocb, *piocb = &iocb;
io_prep_poll(piocb, sfd, POLLIN);
io_submit(context, 1, &piocb);
struct io_event event;
io_getevents(context, 1, 1, &event, NULL);
io_queue_release(context);
}
void test_epoll(int sfd) {
int epfd = epoll_create(1);
struct epoll_event ev, ret;
ev.data.fd = sfd;
ev.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &ev);
epoll_wait(epfd, &ret, 1, -1);
close(epfd);
}
void test_workaround(int sfd) {
int epfd = epoll_create(1);
struct epoll_event ev;
ev.data.fd = sfd;
ev.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &ev);
struct io_uring ring;
io_uring_queue_init(32, &ring, 0);
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
io_uring_prep_poll_add(sqe, epfd, POLLIN);
io_uring_submit(&ring);
struct io_uring_cqe *cqe;
io_uring_wait_cqe(&ring, &cqe);
io_uring_cqe_seen(&ring, cqe);
io_uring_queue_exit(&ring);
close(epfd);
}
int main() {
int sfd = setup_signal();
test_uring(sfd);
// test_epoll(sfd);
// test_workaround(sfd);
// test_aio(sfd);
puts("OK");
close(sfd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.