Skip to content

Commit cd4e51e

Browse files
committed
(socket server) instead of busy looping with a sleep, only wake up the GC thread when a new thread will have to be joined, (we know that thanks to the ConnectionState OnSetTerminated callback
1 parent 785842d commit cd4e51e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All changes to this project will be documented in this file.
44

5+
## [10.2.3] - 2020-08-15
6+
7+
(socket server) instead of busy looping with a sleep, only wake up the GC thread when a new thread will have to be joined, (we know that thanks to the ConnectionState OnSetTerminated callback
8+
59
## [10.2.2] - 2020-08-15
610

711
(socket server) add a callback to the ConnectionState to be invoked when the connection is terminated. This will be used by the SocketServer in the future to know on time that the associated connection thread can be terminated.

ixwebsocket/IXSocketServer.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ namespace ix
204204
if (_gcThread.joinable())
205205
{
206206
_stopGc = true;
207+
_conditionVariableGC.notify_one();
207208
_gcThread.join();
208209
_stopGc = false;
209210
}
@@ -259,7 +260,7 @@ namespace ix
259260
if (_stop) return;
260261

261262
// Use poll to check whether a new connection is in progress
262-
int timeoutMs = 10000;
263+
int timeoutMs = 10;
263264
bool readyToRead = true;
264265
PollResultType pollResult =
265266
Socket::poll(readyToRead, timeoutMs, _serverFd, _acceptSelectInterrupt);
@@ -415,8 +416,14 @@ namespace ix
415416
break;
416417
}
417418

418-
// Sleep a little bit then keep cleaning up
419-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
419+
// Unless we are stopping the server, wait for a connection
420+
// to be terminated to run the threads GC, instead of busy waiting
421+
// with a sleep
422+
if (!_stopGc)
423+
{
424+
std::unique_lock<std::mutex> lock(_conditionVariableMutexGC);
425+
_conditionVariableGC.wait(lock);
426+
}
420427
}
421428
}
422429

@@ -427,6 +434,8 @@ namespace ix
427434

428435
void SocketServer::onSetTerminatedCallback()
429436
{
430-
;
437+
// a connection got terminated, we can run the connection thread GC,
438+
// so wake up the thread responsible for that
439+
_conditionVariableGC.notify_one();
431440
}
432441
} // namespace ix

ixwebsocket/IXSocketServer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,10 @@ namespace ix
117117

118118
// to wake up from select
119119
SelectInterruptPtr _acceptSelectInterrupt;
120+
121+
// used by the gc thread, to know that a thread needs to be garbage collected
122+
// as a connection
123+
std::condition_variable _conditionVariableGC;
124+
std::mutex _conditionVariableMutexGC;
120125
};
121126
} // namespace ix

0 commit comments

Comments
 (0)