Make Loop::free/get reusable
This commit is contained in:
+13
-6
@@ -81,24 +81,28 @@ private:
|
|||||||
bool cleanMe = false;
|
bool cleanMe = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static LoopCleaner &getLazyLoop() {
|
||||||
|
static thread_local LoopCleaner lazyLoop;
|
||||||
|
return lazyLoop;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/* Lazily initializes a per-thread loop and returns it.
|
/* Lazily initializes a per-thread loop and returns it.
|
||||||
* Will automatically free all initialized loops at exit. */
|
* Will automatically free all initialized loops at exit. */
|
||||||
static Loop *get(void *existingNativeLoop = nullptr) {
|
static Loop *get(void *existingNativeLoop = nullptr) {
|
||||||
static thread_local LoopCleaner lazyLoop;
|
if (!getLazyLoop().loop) {
|
||||||
if (!lazyLoop.loop) {
|
|
||||||
/* If we are given a native loop pointer we pass that to uSockets and let it deal with it */
|
/* If we are given a native loop pointer we pass that to uSockets and let it deal with it */
|
||||||
if (existingNativeLoop) {
|
if (existingNativeLoop) {
|
||||||
/* Todo: here we want to pass the pointer, not a boolean */
|
/* Todo: here we want to pass the pointer, not a boolean */
|
||||||
lazyLoop.loop = create(existingNativeLoop);
|
getLazyLoop().loop = create(existingNativeLoop);
|
||||||
/* We cannot register automatic free here, must be manually done */
|
/* We cannot register automatic free here, must be manually done */
|
||||||
} else {
|
} else {
|
||||||
lazyLoop.loop = create(nullptr);
|
getLazyLoop().loop = create(nullptr);
|
||||||
lazyLoop.cleanMe = true;
|
getLazyLoop().cleanMe = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lazyLoop.loop;
|
return getLazyLoop().loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Freeing the default loop should be done once */
|
/* Freeing the default loop should be done once */
|
||||||
@@ -107,6 +111,9 @@ public:
|
|||||||
loopData->~LoopData();
|
loopData->~LoopData();
|
||||||
/* uSockets will track whether this loop is owned by us or a borrowed alien loop */
|
/* uSockets will track whether this loop is owned by us or a borrowed alien loop */
|
||||||
us_loop_free((us_loop_t *) this);
|
us_loop_free((us_loop_t *) this);
|
||||||
|
|
||||||
|
/* Reset lazyLoop */
|
||||||
|
getLazyLoop().loop = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addPostHandler(void *key, fu2::unique_function<void(Loop *)> &&handler) {
|
void addPostHandler(void *key, fu2::unique_function<void(Loop *)> &&handler) {
|
||||||
|
|||||||
Reference in New Issue
Block a user