vitastor/ringloop.cpp

61 lines
1.3 KiB
C++
Raw Normal View History

2019-11-05 02:12:04 +03:00
#include "ringloop.h"
ring_loop_t::ring_loop_t(int qd)
{
int ret = io_uring_queue_init(qd, &ring, 0);
2019-11-05 02:12:04 +03:00
if (ret < 0)
{
throw std::runtime_error(std::string("io_uring_queue_init: ") + strerror(-ret));
2019-11-05 02:12:04 +03:00
}
ring_data = (struct ring_data_t*)malloc(sizeof(ring_data_t) * ring.sq.ring_sz);
2019-11-05 02:12:04 +03:00
if (!ring_data)
{
throw std::bad_alloc();
2019-11-05 02:12:04 +03:00
}
}
ring_loop_t::~ring_loop_t()
{
free(ring_data);
2019-11-17 17:45:39 +03:00
io_uring_queue_exit(&ring);
2019-11-05 02:12:04 +03:00
}
int ring_loop_t::register_consumer(ring_consumer_t & consumer)
{
consumer.number = consumers.size();
consumers.push_back(consumer);
return consumer.number;
}
void ring_loop_t::unregister_consumer(int number)
{
if (number < consumers.size())
{
consumers[number].loop = NULL;
}
}
void ring_loop_t::loop(bool sleep)
{
2019-11-18 13:37:32 +03:00
// FIXME: we should loop until all "coroutines" are suspended. currently we loop only once before sleeping
2019-11-05 02:12:04 +03:00
struct io_uring_cqe *cqe;
2019-11-17 17:45:39 +03:00
while (!io_uring_peek_cqe(&ring, &cqe))
2019-11-05 02:12:04 +03:00
{
struct ring_data_t *d = (struct ring_data_t*)cqe->user_data;
if (d->callback)
2019-11-05 02:12:04 +03:00
{
2019-11-17 22:26:55 +03:00
d->res = cqe->res;
d->callback(d);
2019-11-05 02:12:04 +03:00
}
io_uring_cqe_seen(&ring, cqe);
2019-11-05 02:12:04 +03:00
}
for (int i = 0; i < consumers.size(); i++)
{
consumers[i].loop();
}
2019-11-17 17:45:39 +03:00
if (sleep)
{
io_uring_wait_cqe(&ring, &cqe);
}
2019-11-05 02:12:04 +03:00
}