diff options
-rw-r--r-- | kit/Kit.cpp | 1 | ||||
-rw-r--r-- | net/Socket.cpp | 8 | ||||
-rw-r--r-- | net/Socket.hpp | 22 |
3 files changed, 28 insertions, 3 deletions
diff --git a/kit/Kit.cpp b/kit/Kit.cpp index d609fcb287..69b6e7affa 100644 --- a/kit/Kit.cpp +++ b/kit/Kit.cpp @@ -2418,6 +2418,7 @@ void lokit_main( #endif // MOBILEAPP SocketPoll mainKit("kit"); + mainKit.runOnClientThread(); // We will do the polling on this thread. #ifndef MOBILEAPP mainKit.insertNewWebSocketSync(uri, std::make_shared<KitWebSocketHandler>("child_ws_" + pid, loKit, jailId, mainKit)); diff --git a/net/Socket.cpp b/net/Socket.cpp index 8acd98ba22..208efc3c9c 100644 --- a/net/Socket.cpp +++ b/net/Socket.cpp @@ -66,6 +66,7 @@ SocketPoll::SocketPoll(const std::string& threadName) _stop(false), _threadStarted(false), _threadFinished(false), + _runOnClientThread(false), _owner(std::this_thread::get_id()) { // Create the wakeup fd. @@ -109,8 +110,10 @@ SocketPoll::~SocketPoll() _wakeup[1] = -1; } -void SocketPoll::startThread() +bool SocketPoll::startThread() { + assert(!_runOnClientThread); + if (!_threadStarted) { _threadStarted = true; @@ -119,6 +122,7 @@ void SocketPoll::startThread() try { _thread = std::thread(&SocketPoll::pollingThreadEntry, this); + return true; } catch (const std::exception& exc) { @@ -126,6 +130,8 @@ void SocketPoll::startThread() _threadStarted = false; } } + + return false; } void SocketPoll::joinThread() diff --git a/net/Socket.hpp b/net/Socket.hpp index 72985aa358..8cc363f12f 100644 --- a/net/Socket.hpp +++ b/net/Socket.hpp @@ -412,7 +412,7 @@ public: } } - bool isAlive() const { return _threadStarted && !_threadFinished; } + bool isAlive() const { return (_threadStarted && !_threadFinished) || _runOnClientThread; } /// Check if we should continue polling virtual bool continuePolling() @@ -667,11 +667,28 @@ public: const std::string& name() const { return _name; } /// Start the polling thread (if desired) - void startThread(); + /// Mutually exclusive with runOnClientThread(). + bool startThread(); /// Stop and join the polling thread before returning (if active) void joinThread(); + /// Called to prevent starting own poll thread + /// when polling is done on the client's thread. + /// Mutually exclusive with startThread(). + bool runOnClientThread() + { + assert(!_threadStarted); + + if (!_threadStarted) + { + _runOnClientThread = true; + return true; + } + + return false; + } + private: /// Initialize the poll fds array with the right events void setupPollFds(std::chrono::steady_clock::time_point now, @@ -729,6 +746,7 @@ protected: std::thread _thread; std::atomic<bool> _threadStarted; std::atomic<bool> _threadFinished; + std::atomic<bool> _runOnClientThread; std::thread::id _owner; }; |