diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-23 19:48:53 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-23 21:47:03 +0530 |
| commit | 346c16b4e2ea26f47e0e370a490b7794492a9ebb (patch) | |
| tree | 277a30ac8b0c82a9c9736985385d0d150a55fcb5 /src/AsyncEventLoop.cpp | |
| parent | 9cc72e8ea9f59f9a9627d05528d54a559ebd412c (diff) | |
| download | null-browser-346c16b4e2ea26f47e0e370a490b7794492a9ebb.tar.gz null-browser-346c16b4e2ea26f47e0e370a490b7794492a9ebb.zip | |
Apply clang-tidy suggestions
Diffstat (limited to '')
| -rw-r--r-- | src/AsyncEventLoop.cpp | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/src/AsyncEventLoop.cpp b/src/AsyncEventLoop.cpp index 230d891..3acbc41 100644 --- a/src/AsyncEventLoop.cpp +++ b/src/AsyncEventLoop.cpp @@ -1,8 +1,10 @@ #include <QtCore> +#include <chrono> #include <functional> #include <mutex> #include <queue> #include <thread> +#include <utility> #include <uv.h> #include "AsyncEventLoop.hpp" @@ -12,24 +14,24 @@ AsyncEventLoop::AsyncEventLoop() { loop = (uv_loop_t *)malloc(sizeof(uv_loop_t)); uv_loop_init(loop); - uv_async_init(loop, &asyncHandle, AsyncEventLoop::asyncHandleCallback); - asyncHandle.data = this; + uv_async_init(loop, &async_handle, AsyncEventLoop::async_handle_callback); + async_handle.data = this; - loopThread = std::thread(&AsyncEventLoop::runLoop, this); + loop_thread = std::thread(&AsyncEventLoop::run_loop, this); // Wait for thread to start - while (!isLoopRunning) + while (!is_loop_running) std::this_thread::sleep_for(std::chrono::milliseconds(50)); } -void AsyncEventLoop::wake() { uv_async_send(&asyncHandle); } +void AsyncEventLoop::wake() { uv_async_send(&async_handle); } -void AsyncEventLoop::processTasks() { +void AsyncEventLoop::process_tasks() { std::queue<std::function<void()>> tasks; { - std::lock_guard<std::mutex> lock(tasksQueueMutex); - tasksQueue.swap(tasks); + const std::lock_guard<std::mutex> lock(tasks_queue_mutex); + tasks_queue.swap(tasks); } while (!tasks.empty()) { @@ -39,9 +41,9 @@ void AsyncEventLoop::processTasks() { } } -void AsyncEventLoop::runLoop() { - isLoopRunning = true; - while (isLoopRunning) { +void AsyncEventLoop::run_loop() { + is_loop_running = true; + while (is_loop_running) { // qDebug() << "loop iteration" << uv_loop_alive(loop); uv_run(loop, UV_RUN_NOWAIT); // qDebug() << "uv_run() returned: " << result; @@ -50,48 +52,49 @@ void AsyncEventLoop::runLoop() { } AsyncEventLoop::~AsyncEventLoop() { - if (!isLoopRunning) + if (!is_loop_running) return; - isLoopRunning = false; + is_loop_running = false; - flushTasks(); + flush_tasks(); std::this_thread::sleep_for(std::chrono::milliseconds(50)); uv_stop(loop); // Close all handles - AsyncEventLoop::closeHandle(reinterpret_cast<uv_handle_t *>(&asyncHandle)); - uv_walk(loop, AsyncEventLoop::closeHandle, nullptr); + AsyncEventLoop::close_handle(reinterpret_cast<uv_handle_t *>(&async_handle)); + uv_walk(loop, AsyncEventLoop::close_handle, nullptr); - if (loopThread.joinable()) - loopThread.join(); + if (loop_thread.joinable()) + loop_thread.join(); // Close loop - while (uv_loop_close(loop) == EBUSY) - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + uv_loop_close(loop); uv_run(loop, UV_RUN_DEFAULT); - if (uv_loop_alive(loop)) + if (uv_loop_alive(loop) > 0) qDebug() << "WARNING: Loop still has active handles!"; free(loop); loop = nullptr; } -void AsyncEventLoop::flushTasks() { - std::lock_guard<std::mutex> lock(tasksQueueMutex); - std::queue<std::function<void()>>().swap(tasksQueue); +void AsyncEventLoop::flush_tasks() { + { + const std::lock_guard<std::mutex> lock(tasks_queue_mutex); + std::queue<std::function<void()>>().swap(tasks_queue); + } wake(); } -void AsyncEventLoop::asyncHandleCallback(uv_async_t *handle) { +void AsyncEventLoop::async_handle_callback(uv_async_t *handle) { auto *runtime = static_cast<AsyncEventLoop *>(handle->data); - runtime->processTasks(); + runtime->process_tasks(); } -void AsyncEventLoop::closeHandle(uv_handle_t *handle, void *) { +void AsyncEventLoop::close_handle(uv_handle_t *handle, void * /*unused*/) { if (!uv_is_closing(handle)) { - uv_close(handle, [](uv_handle_t *h) { h->data = nullptr; }); + uv_close(handle, [](uv_handle_t *handle) { handle->data = nullptr; }); uv_unref(handle); } } |
