SharedStorage/core/SharedCommandQueue.h
2025-04-27 16:32:10 +00:00

66 lines
1.5 KiB
C++

//
// Created by Kirill Zhukov on 20.04.2025.
//
#ifndef SHAREDCOMMANDQUEUE_H
#define SHAREDCOMMANDQUEUE_H
#include "Command.h"
#include "utils/datastructures/LFCircullarBuffer.h"
#include <atomic>
#include <cstdint>
namespace usub::core {
constexpr size_t SHM_ALIGNMENT = 64;
class alignas(SHM_ALIGNMENT) SharedCommandQueue {
public:
explicit SharedCommandQueue(size_t capacity);
~SharedCommandQueue();
bool try_push(const Command &cmd);
bool try_push_batch(const Command *cmds, size_t count);
void finalize(Command *cmd);
std::optional<Command> try_pop();
size_t try_pop_batch(Command *out, size_t max_count);
Command *peek(size_t index);
void pop();
size_t pending_count() const;
Command *raw_buffer() noexcept;
size_t capacity() const noexcept;
std::atomic<size_t> &head() noexcept;
std::atomic<size_t> &tail() noexcept;
bool enqueue_put(const usub::utils::Hash128 &key, const std::string &value);
bool enqueue_find(const usub::utils::Hash128 &key);
static bool await_response(Command &cmd);
void reset();
static size_t calculate_shm_size(size_t capacity);
private:
size_t capacity_;
alignas(SHM_ALIGNMENT) std::atomic<size_t> head_;
alignas(SHM_ALIGNMENT) std::atomic<size_t> tail_;
alignas(SHM_ALIGNMENT) Command buffer_[];
};
}
#endif //SHAREDCOMMANDQUEUE_H