aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAakarsh Kashyap <[email protected]>2026-06-29 22:27:45 +0530
committerAakarsh Kashyap <[email protected]>2026-06-29 22:27:45 +0530
commit36db9ebac493aa07a08c8a92a1380d253e05718e (patch)
tree70b528aa4ca444f2251d01787307225c551f8d91 /include
parent53a8f41171179663cebdb9afbee5de88f8b6bbd4 (diff)
input handling using SCSP ring buffer
Diffstat (limited to 'include')
-rw-r--r--include/reader.h60
1 files changed, 60 insertions, 0 deletions
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 <termios.h>
#include <unistd.h>
+#include <atomic>
#include <string>
#include <vector>
@@ -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<std::string> &strings,
const std::string &delims);
+
+
+
+template <typename T>
+class
+SCPCQueue
+{
+private:
+ std::vector<T> buff;
+ const size_t capacity;
+
+ alignas(64) std::atomic<size_t> head;
+ alignas(64) std::atomic<size_t> 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<char> &queue);
+
+void consumer(SCPCQueue<char> &queue, EditorCfg *cfg);