From 36db9ebac493aa07a08c8a92a1380d253e05718e Mon Sep 17 00:00:00 2001 From: Aakarsh Kashyap Date: Mon, 29 Jun 2026 22:27:45 +0530 Subject: input handling using SCSP ring buffer --- include/reader.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'include') diff --git a/include/reader.h b/include/reader.h index cad8bf3..8ff6d72 100644 --- a/include/reader.h +++ b/include/reader.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -19,6 +20,7 @@ typedef struct EditorCfg { int screenrows; int screencols; + int speed; struct termios termios_inst; } EditorCfg; @@ -47,3 +49,61 @@ int is_number(char *strnum); size_t split(const std::string &txt, std::vector &strings, const std::string &delims); + + + +template +class +SCPCQueue +{ +private: + std::vector buff; + const size_t capacity; + + alignas(64) std::atomic head; + alignas(64) std::atomic tail; + +public: + SCPCQueue(size_t size) + : buff(size + 1), capacity(size + 1), head(0), tail(0) {} + + SCPCQueue(const SCPCQueue&) = delete; + SCPCQueue& operator=(const SCPCQueue&) = delete; + + bool + push(const T& data) + { + const size_t cur_tail = tail.load(std::memory_order_relaxed); + const size_t next_tail = (cur_tail + 1) % capacity; + + if (next_tail == head.load(std::memory_order_acquire)) { + /* Queue is full */ + return false; + } + + buff[cur_tail] = data; + + tail.store(next_tail, std::memory_order_release); + + return true; + } + bool pop(T &result) + { + const size_t cur_head = head.load(std::memory_order_relaxed); + + if (cur_head == tail.load(std::memory_order_acquire)) { + return false; + } + result = buff[cur_head]; + + const size_t next_head = (cur_head + 1) % capacity; + + head.store(next_head, std::memory_order_release); + + return true; + } +}; + +void producer(SCPCQueue &queue); + +void consumer(SCPCQueue &queue, EditorCfg *cfg); -- cgit v1.2.3