]> granicus.if.org Git - icinga2/blob - lib/base/io-engine.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / io-engine.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef IO_ENGINE_H
21 #define IO_ENGINE_H
22
23 #include "base/lazy-init.hpp"
24 #include <atomic>
25 #include <exception>
26 #include <memory>
27 #include <thread>
28 #include <vector>
29 #include <boost/asio/deadline_timer.hpp>
30 #include <boost/asio/io_service.hpp>
31 #include <boost/asio/spawn.hpp>
32
33 namespace icinga
34 {
35
36 /**
37  * Scope lock for CPU-bound work done in an I/O thread
38  *
39  * @ingroup base
40  */
41 class CpuBoundWork
42 {
43 public:
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;
49         ~CpuBoundWork();
50
51         void Done();
52
53 private:
54         bool m_Done;
55 };
56
57 /**
58  * Scope break for CPU-bound work done in an I/O thread
59  *
60  * @ingroup base
61  */
62 class IoBoundWorkSlot
63 {
64 public:
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;
70         ~IoBoundWorkSlot();
71
72 private:
73         boost::asio::yield_context yc;
74 };
75
76 /**
77  * Async I/O engine
78  *
79  * @ingroup base
80  */
81 class IoEngine
82 {
83         friend CpuBoundWork;
84         friend IoBoundWorkSlot;
85
86 public:
87         IoEngine(const IoEngine&) = delete;
88         IoEngine(IoEngine&&) = delete;
89         IoEngine& operator=(const IoEngine&) = delete;
90         IoEngine& operator=(IoEngine&&) = delete;
91         ~IoEngine();
92
93         static IoEngine& Get();
94
95         boost::asio::io_service& GetIoService();
96
97 private:
98         IoEngine();
99
100         void RunEventLoop();
101
102         static LazyInit<std::unique_ptr<IoEngine>> m_Instance;
103
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;
109 };
110
111 class TerminateIoThread : public std::exception
112 {
113 };
114
115 /**
116  * Condition variable which doesn't block I/O threads
117  *
118  * @ingroup base
119  */
120 class AsioConditionVariable
121 {
122 public:
123         AsioConditionVariable(boost::asio::io_service& io, bool init = false);
124
125         void Set();
126         void Clear();
127         void Wait(boost::asio::yield_context yc);
128
129 private:
130         boost::asio::deadline_timer m_Timer;
131 };
132
133 }
134
135 #endif /* IO_ENGINE_H */