Simplified distributed block storage with strong consistency, like in Ceph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB

3 years ago
#include "ringloop.h"
ring_loop_t::ring_loop_t(int qd)
{
int ret = io_uring_queue_init(qd, &ring, 0);
3 years ago
if (ret < 0)
{
throw std::runtime_error(std::string("io_uring_queue_init: ") + strerror(-ret));
3 years ago
}
ring_data = (struct ring_data_t*)malloc(sizeof(ring_data_t) * ring.sq.ring_sz);
3 years ago
if (!ring_data)
{
throw std::bad_alloc();
3 years ago
}
}
ring_loop_t::~ring_loop_t()
{
free(ring_data);
io_uring_queue_exit(&ring);
3 years ago
}
int ring_loop_t::register_consumer(ring_consumer_t & consumer)
{
consumer.number = consumers.size();
consumers.push_back(consumer);
return consumer.number;
}
3 years ago
void ring_loop_t::wakeup()
3 years ago
{
3 years ago
loop_again = true;
}
void ring_loop_t::unregister_consumer(ring_consumer_t & consumer)
{
if (consumer.number >= 0 && consumer.number < consumers.size())
3 years ago
{
3 years ago
consumers[consumer.number].loop = NULL;
consumer.number = -1;
3 years ago
}
}
3 years ago
void ring_loop_t::loop()
3 years ago
{
struct io_uring_cqe *cqe;
while (!io_uring_peek_cqe(&ring, &cqe))
3 years ago
{
struct ring_data_t *d = (struct ring_data_t*)cqe->user_data;
if (d->callback)
3 years ago
{
d->res = cqe->res;
d->callback(d);
3 years ago
}
io_uring_cqe_seen(&ring, cqe);
3 years ago
}
3 years ago
do
{
3 years ago
loop_again = false;
for (int i = 0; i < consumers.size(); i++)
{
consumers[i].loop();
}
} while (loop_again);
3 years ago
}