Fix: high cpu loading while waiting for result caused by wrong event selection

Fix: next bug where it wouldn't retrieve all result when using the proper
events causing hangs because it didn't loop isbusy.
This commit is contained in:
Eelke Klein 2017-01-08 15:15:40 +01:00
parent 2502aea9e5
commit 30638b11e5

View file

@ -234,11 +234,12 @@ void ASyncDBConnection::Thread::waitForResult()
{
int sock = m_connection.socket();
Win32Event socket_event(Win32Event::Reset::Auto, Win32Event::Initial::Clear);
Win32Event socket_event(Win32Event::Reset::Manual, Win32Event::Initial::Clear);
long fd = FD_WRITE;
long fd = FD_READ | FD_CLOSE;
while (true) {
bool finished = false;
while ( ! finished) {
WSAEventSelect(sock, socket_event.handle(), fd);
WaitHandleList whl;
@ -253,34 +254,38 @@ void ASyncDBConnection::Thread::waitForResult()
0 // _In_ DWORD dwFlags
);
if (res == wait_result_socket) {
if (m_connection.consumeInput()) {
if ( ! m_connection.isBusy()) {
auto res(m_connection.getResult());
{
std::lock_guard<std::mutex> lg(m_commandQueue.m_mutex);
m_commandQueue.m_queue.front().on_result(res);
if (res == nullptr) {
m_commandQueue.m_queue.pop();
doStateCallback(State::Connected);
break; // leave the while loop
WSANETWORKEVENTS net_events;
WSAEnumNetworkEvents(sock, socket_event.handle(), &net_events);
if (net_events.lNetworkEvents & FD_READ) {
if (m_connection.consumeInput()) {
while ( ! finished && ! m_connection.isBusy()) {
auto res(m_connection.getResult());
{
std::lock_guard<std::mutex> lg(m_commandQueue.m_mutex);
m_commandQueue.m_queue.front().on_result(res);
if (res == nullptr) {
m_commandQueue.m_queue.pop();
doStateCallback(State::Connected);
finished = true;
}
}
}
// else is still waiting for more data
}
// else is still waiting for more data
}
else {
// error during consume
else {
// error during consume
}
}
}
if (res == wait_result_stop) {
// Send cancel, close connection and terminate thread
cancel();
doStateCallback(State::Terminating);
break;
finished = true;
}
} // end while
// When last result received, remove command from queue