diff options
Diffstat (limited to '')
| -rw-r--r-- | src/AsyncEventLoop.cpp | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/src/AsyncEventLoop.cpp b/src/AsyncEventLoop.cpp index 6c9b42d..6c6333f 100644 --- a/src/AsyncEventLoop.cpp +++ b/src/AsyncEventLoop.cpp @@ -3,8 +3,6 @@ #include <mutex> #include <queue> #include <thread> -#include <utility> -#include <uv.h> #include "AsyncEventLoop.hpp" @@ -23,6 +21,8 @@ AsyncEventLoop::AsyncEventLoop() { std::this_thread::sleep_for(std::chrono::milliseconds(50)); } +void AsyncEventLoop::wake() { uv_async_send(&asyncHandle); } + void AsyncEventLoop::processTasks() { std::queue<std::function<void()>> tasks; @@ -41,10 +41,9 @@ void AsyncEventLoop::processTasks() { void AsyncEventLoop::runLoop() { isLoopRunning = true; while (isLoopRunning) { - /* int _result = */ uv_run(loop, UV_RUN_NOWAIT); - // qDebug() << "Tasks handled:" << result; - // uv_print_active_handles(loop, stdout); - // if (result == 0) + // qDebug() << "loop iteration" << uv_loop_alive(loop); + uv_run(loop, UV_RUN_NOWAIT); + // qDebug() << "uv_run() returned: " << result; std::this_thread::sleep_for(std::chrono::milliseconds(10)); } } @@ -54,33 +53,28 @@ AsyncEventLoop::~AsyncEventLoop() { return; isLoopRunning = false; - // Clear the tasks queue + // Wake it up. Stab it to death. (clear async queue) + wake(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); { std::lock_guard<std::mutex> lock(tasksQueueMutex); std::queue<std::function<void()>>().swap(tasksQueue); } - - // Wake it up. Stab it to death. - uv_async_send(&asyncHandle); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); uv_stop(loop); // Close all handles AsyncEventLoop::closeHandle(reinterpret_cast<uv_handle_t *>(&asyncHandle)); uv_walk(loop, AsyncEventLoop::closeHandle, nullptr); - while (uv_run(loop, UV_RUN_ONCE) != 0) - ; + while (uv_run(loop, UV_RUN_NOWAIT) != 0) + std::this_thread::sleep_for(std::chrono::milliseconds(100)); // TODO: Fix pending handler case (setTimeout(100) wait(20) close() -> error) - qDebug() << "join start"; if (loopThread.joinable()) loopThread.join(); - qDebug() << "join done"; - while (uv_loop_close(loop) == UV_EBUSY) { - uv_walk(loop, AsyncEventLoop::closeHandle, nullptr); - uv_run(loop, UV_RUN_NOWAIT); - } + while (uv_loop_close(loop)) + uv_run(loop, UV_RUN_DEFAULT); + free(loop); loop = nullptr; } @@ -93,5 +87,6 @@ void AsyncEventLoop::asyncHandleCallback(uv_async_t *handle) { void AsyncEventLoop::closeHandle(uv_handle_t *handle, void *) { if (!uv_is_closing(handle)) { uv_close(handle, [](uv_handle_t *h) { h->data = nullptr; }); + uv_unref(handle); } } |
