From 3c76e70fe2c011fb46257e46519bee8329786620 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 28 Jan 2016 07:19:32 +0100 Subject: [PATCH] Avoid updating the file descriptor list for poll() unless we really need to refs #11014 --- lib/base/socketevents.cpp | 58 +++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/base/socketevents.cpp b/lib/base/socketevents.cpp index da26b9d55..ff99383de 100644 --- a/lib/base/socketevents.cpp +++ b/lib/base/socketevents.cpp @@ -73,26 +73,31 @@ void SocketEvents::ThreadProc(void) { Utility::SetThreadName("SocketIO"); - for (;;) { - pollfd *pfds; - int pfdcount; - - typedef std::map::value_type SocketDesc; + pollfd *pfds = NULL; + int pfdcount; + for (;;) { { boost::mutex::scoped_lock lock(l_SocketIOMutex); - pfdcount = l_SocketIOSockets.size(); - pfds = new pollfd[pfdcount]; + if (pfds == NULL) { + typedef std::map::value_type SocketDesc; + + pfdcount = l_SocketIOSockets.size(); + pfds = new pollfd[pfdcount]; - int i = 0; + int i = 0; - BOOST_FOREACH(const SocketDesc& desc, l_SocketIOSockets) { - pfds[i].fd = desc.first; - pfds[i].events = desc.second.Events; - pfds[i].revents = 0; + BOOST_FOREACH(const SocketDesc& desc, l_SocketIOSockets) { + pfds[i].fd = desc.first; + pfds[i].events = desc.second.Events; + pfds[i].revents = 0; - i++; + i++; + } + + l_SocketIOFDChanged = false; + l_SocketIOCV.notify_all(); } } @@ -106,9 +111,8 @@ void SocketEvents::ThreadProc(void) boost::mutex::scoped_lock lock(l_SocketIOMutex); if (l_SocketIOFDChanged) { - l_SocketIOFDChanged = false; - l_SocketIOCV.notify_all(); delete [] pfds; + pfds = NULL; continue; } } @@ -153,8 +157,6 @@ void SocketEvents::ThreadProc(void) Log(LogCritical, "SocketEvents", "Exception of unknown type thrown in socket I/O handler."); } } - - delete [] pfds; } } @@ -196,22 +198,24 @@ SocketEvents::~SocketEvents(void) void SocketEvents::Register(Object *lifesupportObject) { - boost::mutex::scoped_lock lock(l_SocketIOMutex); + { + boost::mutex::scoped_lock lock(l_SocketIOMutex); - VERIFY(m_FD != INVALID_SOCKET); + VERIFY(m_FD != INVALID_SOCKET); - SocketEventDescriptor desc; - desc.Events = 0; - desc.EventInterface = this; - desc.LifesupportObject = lifesupportObject; + SocketEventDescriptor desc; + desc.Events = 0; + desc.EventInterface = this; + desc.LifesupportObject = lifesupportObject; - VERIFY(l_SocketIOSockets.find(m_FD) == l_SocketIOSockets.end()); + VERIFY(l_SocketIOSockets.find(m_FD) == l_SocketIOSockets.end()); - l_SocketIOSockets[m_FD] = desc; + l_SocketIOSockets[m_FD] = desc; - m_Events = true; + m_Events = true; + } - /* There's no need to wake up the I/O thread here. */ + WakeUpThread(true); } void SocketEvents::Unregister(void) -- 2.40.0