65 lines
1.4 KiB
C++
65 lines
1.4 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_QUEUE_CAPACITY = 1024;
|
|
|
|
class 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);
|
|
|
|
bool await_response(Command& cmd);
|
|
|
|
private:
|
|
size_t capacity_;
|
|
Command* commands_;
|
|
alignas(64) std::atomic<size_t> head_;
|
|
alignas(64) std::atomic<size_t> tail_;
|
|
alignas(64) Command buffer_[];
|
|
};
|
|
}
|
|
|
|
|
|
#endif //SHAREDCOMMANDQUEUE_H
|