From 35e6bad33b20c3e630e53b16c16546d2572bccfa Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sat, 2 Mar 2019 04:36:06 +0100 Subject: [PATCH] Tweak Loop::get for "default" loops --- src/Loop.h | 22 ++++++++++++++++------ uSockets | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Loop.h b/src/Loop.h index 7c965bd..1a62a1e 100644 --- a/src/Loop.h +++ b/src/Loop.h @@ -76,19 +76,28 @@ private: return this; } + /* Todo: should take void ptr */ static Loop *create(bool defaultLoop) { return ((Loop *) us_create_loop(defaultLoop, wakeupCb, preCb, postCb, sizeof(LoopData)))->init(); } public: - /* Lazily initializes a per-thread loop and returns it. Will automatically free all initialized loops at exit. */ - static Loop *get() { + /* Lazily initializes a per-thread loop and returns it. + * Will automatically free all initialized loops at exit. */ + static Loop *get(void *existingNativeLoop = nullptr) { static thread_local Loop *lazyLoop; if (!lazyLoop) { - lazyLoop = create(false); - std::atexit([]() { - Loop::get()->free(); - }); + /* If we are given a native loop pointer we pass that to uSockets and let it deal with it */ + if (existingNativeLoop) { + /* Todo: here we want to pass the pointer, not a boolean */ + lazyLoop = create(true); + /* We cannot register automatic free here, must be manually done */ + } else { + lazyLoop = create(false); + std::atexit([]() { + Loop::get()->free(); + }); + } } return lazyLoop; @@ -98,6 +107,7 @@ public: void free() { LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); loopData->~LoopData(); + /* uSockets will track whether this loop is owned by us or a borrowed alien loop */ us_loop_free((us_loop *) this); } diff --git a/uSockets b/uSockets index 923fa82..a83f842 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit 923fa82d8ee63c65adb97af2c5d0e31875d06ec6 +Subproject commit a83f8423af5b4ae088ddb2996a115bab895dd4c3