aboutsummaryrefslogtreecommitdiff
path: root/src/AsyncEventLoop.hpp
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-03-21 19:00:45 +0530
committerAkshay Nair <phenax5@gmail.com>2025-03-21 19:44:57 +0530
commit7ec8336431787156826185628ad3ee05dc327d2a (patch)
tree0b984450aa1ee09f6592eadb898b17ffddd45219 /src/AsyncEventLoop.hpp
parentfdeb33d34a97d062a120f72da919f81d7b1d45bf (diff)
downloadnull-browser-7ec8336431787156826185628ad3ee05dc327d2a.tar.gz
null-browser-7ec8336431787156826185628ad3ee05dc327d2a.zip
Add event loop with uv
Diffstat (limited to '')
-rw-r--r--src/AsyncEventLoop.hpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/AsyncEventLoop.hpp b/src/AsyncEventLoop.hpp
new file mode 100644
index 0000000..ebe2cad
--- /dev/null
+++ b/src/AsyncEventLoop.hpp
@@ -0,0 +1,40 @@
+#pragma once
+
+#include <atomic>
+#include <functional>
+#include <mutex>
+#include <queue>
+#include <thread>
+#include <uv.h>
+
+#include "utils.hpp"
+
+class AsyncEventLoop {
+public:
+ AsyncEventLoop();
+ ~AsyncEventLoop();
+
+ DEFINE_GETTER(getUVLoop, loop)
+
+ template <typename F> void queueTask(F &&task) {
+ {
+ std::lock_guard<std::mutex> lock(tasksQueueMutex);
+ tasksQueue.push(std::forward<F>(task));
+ }
+ uv_async_send(&asyncHandle);
+ }
+
+protected:
+ void runLoop();
+ void processTasks();
+ static void asyncHandleCallback(uv_async_t *handle);
+ static void closeHandle(uv_handle_t *handle, void *arg = nullptr);
+
+private:
+ uv_loop_t *loop;
+ std::thread loopThread;
+ uv_async_t asyncHandle;
+ std::atomic<bool> isLoopRunning = false;
+ std::queue<std::function<void()>> tasksQueue;
+ std::mutex tasksQueueMutex;
+};