Tweak Loop::get for "default" loops
This commit is contained in:
+16
-6
@@ -76,19 +76,28 @@ private:
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Todo: should take void ptr */
|
||||||
static Loop *create(bool defaultLoop) {
|
static Loop *create(bool defaultLoop) {
|
||||||
return ((Loop *) us_create_loop(defaultLoop, wakeupCb, preCb, postCb, sizeof(LoopData)))->init();
|
return ((Loop *) us_create_loop(defaultLoop, wakeupCb, preCb, postCb, sizeof(LoopData)))->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/* Lazily initializes a per-thread loop and returns it. Will automatically free all initialized loops at exit. */
|
/* Lazily initializes a per-thread loop and returns it.
|
||||||
static Loop *get() {
|
* Will automatically free all initialized loops at exit. */
|
||||||
|
static Loop *get(void *existingNativeLoop = nullptr) {
|
||||||
static thread_local Loop *lazyLoop;
|
static thread_local Loop *lazyLoop;
|
||||||
if (!lazyLoop) {
|
if (!lazyLoop) {
|
||||||
lazyLoop = create(false);
|
/* If we are given a native loop pointer we pass that to uSockets and let it deal with it */
|
||||||
std::atexit([]() {
|
if (existingNativeLoop) {
|
||||||
Loop::get()->free();
|
/* 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;
|
return lazyLoop;
|
||||||
@@ -98,6 +107,7 @@ public:
|
|||||||
void free() {
|
void free() {
|
||||||
LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this);
|
LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this);
|
||||||
loopData->~LoopData();
|
loopData->~LoopData();
|
||||||
|
/* uSockets will track whether this loop is owned by us or a borrowed alien loop */
|
||||||
us_loop_free((us_loop *) this);
|
us_loop_free((us_loop *) this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
Submodule uSockets updated: 923fa82d8e...a83f8423af
Reference in New Issue
Block a user