// // Created by Kirill Zhukov on 20.04.2025. // #ifndef SHAREDCOMMANDQUEUE_H #define SHAREDCOMMANDQUEUE_H #include "Command.h" #include "utils/datastructures/LFCircullarBuffer.h" #include #include 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 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 &head() noexcept; std::atomic &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 head_; alignas(SHM_ALIGNMENT) std::atomic tail_; alignas(SHM_ALIGNMENT) Command buffer_[]; }; } #endif //SHAREDCOMMANDQUEUE_H