You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.8 KiB
C++
115 lines
2.8 KiB
C++
3 years ago
|
// Copyright (c) Vitaliy Filippov, 2019+
|
||
2 years ago
|
// License: VNPL-1.1 or GNU GPL-2.0+ (see README.md for details)
|
||
3 years ago
|
|
||
3 years ago
|
#include <stdlib.h>
|
||
|
|
||
|
#include <stdexcept>
|
||
|
|
||
4 years ago
|
#include "ringloop.h"
|
||
|
|
||
|
ring_loop_t::ring_loop_t(int qd)
|
||
|
{
|
||
4 years ago
|
int ret = io_uring_queue_init(qd, &ring, 0);
|
||
4 years ago
|
if (ret < 0)
|
||
|
{
|
||
4 years ago
|
throw std::runtime_error(std::string("io_uring_queue_init: ") + strerror(-ret));
|
||
4 years ago
|
}
|
||
4 years ago
|
free_ring_data_ptr = *ring.cq.kring_entries;
|
||
3 years ago
|
ring_datas = (struct ring_data_t*)calloc(free_ring_data_ptr, sizeof(ring_data_t));
|
||
4 years ago
|
free_ring_data = (int*)malloc(sizeof(int) * free_ring_data_ptr);
|
||
|
if (!ring_datas || !free_ring_data)
|
||
4 years ago
|
{
|
||
4 years ago
|
throw std::bad_alloc();
|
||
4 years ago
|
}
|
||
4 years ago
|
for (int i = 0; i < free_ring_data_ptr; i++)
|
||
|
{
|
||
|
free_ring_data[i] = i;
|
||
|
}
|
||
3 years ago
|
wait_sqe_id = 1;
|
||
4 years ago
|
}
|
||
|
|
||
|
ring_loop_t::~ring_loop_t()
|
||
|
{
|
||
4 years ago
|
free(free_ring_data);
|
||
|
free(ring_datas);
|
||
4 years ago
|
io_uring_queue_exit(&ring);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
void ring_loop_t::register_consumer(ring_consumer_t *consumer)
|
||
4 years ago
|
{
|
||
3 years ago
|
unregister_consumer(consumer);
|
||
4 years ago
|
consumers.push_back(consumer);
|
||
|
}
|
||
|
|
||
4 years ago
|
void ring_loop_t::wakeup()
|
||
4 years ago
|
{
|
||
4 years ago
|
loop_again = true;
|
||
|
}
|
||
|
|
||
3 years ago
|
void ring_loop_t::unregister_consumer(ring_consumer_t *consumer)
|
||
4 years ago
|
{
|
||
3 years ago
|
for (int i = 0; i < consumers.size(); i++)
|
||
4 years ago
|
{
|
||
3 years ago
|
if (consumers[i] == consumer)
|
||
|
{
|
||
|
consumers.erase(consumers.begin()+i, consumers.begin()+i+1);
|
||
|
break;
|
||
|
}
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
void ring_loop_t::loop()
|
||
4 years ago
|
{
|
||
|
struct io_uring_cqe *cqe;
|
||
4 years ago
|
while (!io_uring_peek_cqe(&ring, &cqe))
|
||
4 years ago
|
{
|
||
|
struct ring_data_t *d = (struct ring_data_t*)cqe->user_data;
|
||
4 years ago
|
if (d->callback)
|
||
4 years ago
|
{
|
||
3 years ago
|
// First free ring_data item, then call the callback
|
||
|
// so it has at least 1 free slot for the next event
|
||
|
// which is required for EPOLLET to function properly
|
||
|
struct ring_data_t dl;
|
||
|
dl.iov = d->iov;
|
||
|
dl.res = cqe->res;
|
||
|
dl.callback.swap(d->callback);
|
||
|
free_ring_data[free_ring_data_ptr++] = d - ring_datas;
|
||
|
dl.callback(&dl);
|
||
4 years ago
|
}
|
||
3 years ago
|
else
|
||
2 years ago
|
{
|
||
|
printf("Warning: empty callback in SQE\n");
|
||
3 years ago
|
free_ring_data[free_ring_data_ptr++] = d - ring_datas;
|
||
2 years ago
|
}
|
||
4 years ago
|
io_uring_cqe_seen(&ring, cqe);
|
||
4 years ago
|
}
|
||
3 years ago
|
while (get_sqe_queue.size() > 0)
|
||
|
{
|
||
|
(get_sqe_queue[0].second)();
|
||
|
get_sqe_queue.erase(get_sqe_queue.begin());
|
||
|
}
|
||
4 years ago
|
do
|
||
4 years ago
|
{
|
||
4 years ago
|
loop_again = false;
|
||
|
for (int i = 0; i < consumers.size(); i++)
|
||
|
{
|
||
3 years ago
|
consumers[i]->loop();
|
||
4 years ago
|
}
|
||
|
} while (loop_again);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
unsigned ring_loop_t::save()
|
||
|
{
|
||
|
return ring.sq.sqe_tail;
|
||
|
}
|
||
|
|
||
|
void ring_loop_t::restore(unsigned sqe_tail)
|
||
|
{
|
||
|
assert(ring.sq.sqe_tail >= sqe_tail);
|
||
|
for (unsigned i = sqe_tail; i < ring.sq.sqe_tail; i++)
|
||
|
{
|
||
|
free_ring_data[free_ring_data_ptr++] = ((ring_data_t*)ring.sq.sqes[i & *ring.sq.kring_mask].user_data) - ring_datas;
|
||
|
}
|
||
|
ring.sq.sqe_tail = sqe_tail;
|
||
|
}
|