1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://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 ******************************************************************************/
23 #include "base/lazy-init.hpp"
29 #include <boost/asio/deadline_timer.hpp>
30 #include <boost/asio/io_service.hpp>
31 #include <boost/asio/spawn.hpp>
37 * Scope lock for CPU-bound work done in an I/O thread
44 CpuBoundWork(boost::asio::yield_context yc);
45 CpuBoundWork(const CpuBoundWork&) = delete;
46 CpuBoundWork(CpuBoundWork&&) = delete;
47 CpuBoundWork& operator=(const CpuBoundWork&) = delete;
48 CpuBoundWork& operator=(CpuBoundWork&&) = delete;
58 * Scope break for CPU-bound work done in an I/O thread
65 IoBoundWorkSlot(boost::asio::yield_context yc);
66 IoBoundWorkSlot(const IoBoundWorkSlot&) = delete;
67 IoBoundWorkSlot(IoBoundWorkSlot&&) = delete;
68 IoBoundWorkSlot& operator=(const IoBoundWorkSlot&) = delete;
69 IoBoundWorkSlot& operator=(IoBoundWorkSlot&&) = delete;
73 boost::asio::yield_context yc;
84 friend IoBoundWorkSlot;
87 IoEngine(const IoEngine&) = delete;
88 IoEngine(IoEngine&&) = delete;
89 IoEngine& operator=(const IoEngine&) = delete;
90 IoEngine& operator=(IoEngine&&) = delete;
93 static IoEngine& Get();
95 boost::asio::io_service& GetIoService();
102 static LazyInit<std::unique_ptr<IoEngine>> m_Instance;
104 boost::asio::io_service m_IoService;
105 boost::asio::io_service::work m_KeepAlive;
106 std::vector<std::thread> m_Threads;
107 boost::asio::deadline_timer m_AlreadyExpiredTimer;
108 std::atomic_int_fast32_t m_CpuBoundSemaphore;
111 class TerminateIoThread : public std::exception
116 * Condition variable which doesn't block I/O threads
120 class AsioConditionVariable
123 AsioConditionVariable(boost::asio::io_service& io, bool init = false);
127 void Wait(boost::asio::yield_context yc);
130 boost::asio::deadline_timer m_Timer;
135 #endif /* IO_ENGINE_H */