diff options
Diffstat (limited to 'include/reader.h')
| -rw-r--r-- | include/reader.h | 60 |
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); |
