1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/socketevents.hpp"
21 #include "base/exception.hpp"
22 #include "base/logger.hpp"
23 #include "base/application.hpp"
24 #include "base/scriptglobal.hpp"
25 #include <boost/thread/once.hpp>
28 # include <sys/epoll.h>
29 #endif /* __linux__ */
31 using namespace icinga;
33 static boost::once_flag l_SocketIOOnceFlag = BOOST_ONCE_INIT;
34 static SocketEventEngine *l_SocketIOEngine;
36 int SocketEvents::m_NextID = 0;
38 void SocketEventEngine::Start()
40 for (int tid = 0; tid < SOCKET_IOTHREADS; tid++) {
41 Socket::SocketPair(m_EventFDs[tid]);
43 Utility::SetNonBlockingSocket(m_EventFDs[tid][0]);
44 Utility::SetNonBlockingSocket(m_EventFDs[tid][1]);
47 Utility::SetCloExec(m_EventFDs[tid][0]);
48 Utility::SetCloExec(m_EventFDs[tid][1]);
51 InitializeThread(tid);
53 m_Threads[tid] = std::thread(std::bind(&SocketEventEngine::ThreadProc, this, tid));
57 void SocketEventEngine::WakeUpThread(int sid, bool wait)
59 int tid = sid % SOCKET_IOTHREADS;
61 if (std::this_thread::get_id() == m_Threads[tid].get_id())
65 boost::mutex::scoped_lock lock(m_EventMutex[tid]);
67 m_FDChanged[tid] = true;
69 while (m_FDChanged[tid]) {
70 (void) send(m_EventFDs[tid][1], "T", 1, 0);
72 boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(50);
73 m_CV[tid].timed_wait(lock, timeout);
76 (void) send(m_EventFDs[tid][1], "T", 1, 0);
80 void SocketEvents::InitializeEngine()
82 String eventEngine = ScriptGlobal::Get("EventEngine", &Empty);
84 if (eventEngine.IsEmpty())
86 eventEngine = "epoll";
89 #endif /* __linux__ */
91 if (eventEngine == "poll")
92 l_SocketIOEngine = new SocketEventEnginePoll();
94 else if (eventEngine == "epoll")
95 l_SocketIOEngine = new SocketEventEngineEpoll();
96 #endif /* __linux__ */
98 Log(LogWarning, "SocketEvents")
99 << "Invalid event engine selected: " << eventEngine << " - Falling back to 'poll'";
101 eventEngine = "poll";
103 l_SocketIOEngine = new SocketEventEnginePoll();
106 l_SocketIOEngine->Start();
108 ScriptGlobal::Set("EventEngine", eventEngine);
112 * Constructor for the SocketEvents class.
114 SocketEvents::SocketEvents(const Socket::Ptr& socket, Object *lifesupportObject)
115 : m_ID(m_NextID++), m_FD(socket->GetFD()), m_EnginePrivate(nullptr)
117 boost::call_once(l_SocketIOOnceFlag, &SocketEvents::InitializeEngine);
119 Register(lifesupportObject);
122 SocketEvents::~SocketEvents()
124 VERIFY(m_FD == INVALID_SOCKET);
127 void SocketEvents::Register(Object *lifesupportObject)
129 l_SocketIOEngine->Register(this, lifesupportObject);
132 void SocketEvents::Unregister()
134 l_SocketIOEngine->Unregister(this);
137 void SocketEvents::ChangeEvents(int events)
139 l_SocketIOEngine->ChangeEvents(this, events);
142 boost::mutex& SocketEventEngine::GetMutex(int tid)
144 return m_EventMutex[tid];
147 bool SocketEvents::IsHandlingEvents() const
149 int tid = m_ID % SOCKET_IOTHREADS;
150 boost::mutex::scoped_lock lock(l_SocketIOEngine->GetMutex(tid));
154 void SocketEvents::OnEvent(int revents)