Refactor Loop::defaultLoop() to Loop::get(), never use "default loops".
This commit is contained in:
@@ -79,7 +79,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
TemplatedApp(us_new_socket_context_options_t options = {}) {
|
TemplatedApp(us_new_socket_context_options_t options = {}) {
|
||||||
httpContext = uWS::HttpContext<SSL>::create(uWS::Loop::defaultLoop(), options);
|
httpContext = uWS::HttpContext<SSL>::create(uWS::Loop::get(), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool constructorFailed() {
|
bool constructorFailed() {
|
||||||
@@ -105,7 +105,7 @@ public:
|
|||||||
"µWebSockets cannot satisfy UserData alignment requirements. You need to recompile µSockets with LIBUS_EXT_ALIGNMENT adjusted accordingly.");
|
"µWebSockets cannot satisfy UserData alignment requirements. You need to recompile µSockets with LIBUS_EXT_ALIGNMENT adjusted accordingly.");
|
||||||
|
|
||||||
/* Every route has its own websocket context with its own behavior and user data type */
|
/* Every route has its own websocket context with its own behavior and user data type */
|
||||||
auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::defaultLoop(), (us_new_socket_context_t *) httpContext);
|
auto *webSocketContext = WebSocketContext<SSL, true>::create(Loop::get(), (us_new_socket_context_t *) httpContext);
|
||||||
|
|
||||||
/* We need to clear this later on */
|
/* We need to clear this later on */
|
||||||
webSocketContexts.push_back(webSocketContext);
|
webSocketContexts.push_back(webSocketContext);
|
||||||
|
|||||||
+8
-27
@@ -23,9 +23,6 @@
|
|||||||
#include "LoopData.h"
|
#include "LoopData.h"
|
||||||
#include <libusockets_new.h>
|
#include <libusockets_new.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
namespace uWS {
|
namespace uWS {
|
||||||
struct Loop {
|
struct Loop {
|
||||||
private:
|
private:
|
||||||
@@ -84,33 +81,17 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/* Returns the default loop if called from one thread, or a dedicated per-thread loop if called from multiple threads */
|
/* Lazily initializes a per-thread loop and returns it. Will automatically free all initialized loops at exit. */
|
||||||
static Loop *defaultLoop() {
|
static Loop *get() {
|
||||||
/* Lock this whole function */
|
static thread_local Loop *lazyLoop;
|
||||||
static std::mutex m;
|
if (!lazyLoop) {
|
||||||
std::lock_guard<std::mutex> lock(m);
|
lazyLoop = create(false);
|
||||||
|
|
||||||
/* Deliver and attach the default loop to the first thread who calls us */
|
|
||||||
static thread_local bool ownsDefaultLoop;
|
|
||||||
static Loop *defaultLoop;
|
|
||||||
if (!defaultLoop) {
|
|
||||||
ownsDefaultLoop = true;
|
|
||||||
defaultLoop = create(true);
|
|
||||||
std::atexit([]() {
|
std::atexit([]() {
|
||||||
Loop::defaultLoop()->free();
|
Loop::get()->free();
|
||||||
});
|
});
|
||||||
return defaultLoop;
|
|
||||||
} else if (ownsDefaultLoop) {
|
|
||||||
return defaultLoop;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Other threads get their non-default loops lazily created */
|
return lazyLoop;
|
||||||
static thread_local Loop *threadLocalLoop;
|
|
||||||
if (!threadLocalLoop) {
|
|
||||||
threadLocalLoop = create(false);
|
|
||||||
return threadLocalLoop;
|
|
||||||
}
|
|
||||||
return threadLocalLoop;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Freeing the default loop should be done once */
|
/* Freeing the default loop should be done once */
|
||||||
@@ -166,7 +147,7 @@ public:
|
|||||||
|
|
||||||
/* Can be called from any thread to run the thread local loop */
|
/* Can be called from any thread to run the thread local loop */
|
||||||
inline void run() {
|
inline void run() {
|
||||||
Loop::defaultLoop()->run();
|
Loop::get()->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -82,7 +82,7 @@ public:
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* Dynamically hook us up with the Loop post handler */
|
/* Dynamically hook us up with the Loop post handler */
|
||||||
Loop::defaultLoop()->addPostHandler([this](Loop *loop) {
|
Loop::get()->addPostHandler([this](Loop *loop) {
|
||||||
|
|
||||||
if (!pubNodes.size()) {
|
if (!pubNodes.size()) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user