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 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/timer.hpp"
25 #include "base/ringbuffer.hpp"
26 #include <boost/thread/thread.hpp>
27 #include <boost/thread/mutex.hpp>
28 #include <boost/thread/condition_variable.hpp>
29 #include <boost/exception_ptr.hpp>
37 enum WorkQueuePriority
44 using TaskFunction = std::function<void ()>;
50 Task(TaskFunction function, WorkQueuePriority priority, int id)
51 : Function(std::move(function)), Priority(priority), ID(id)
54 TaskFunction Function;
55 WorkQueuePriority Priority{PriorityNormal};
59 bool operator<(const Task& a, const Task& b);
69 typedef std::function<void (boost::exception_ptr)> ExceptionCallback;
71 WorkQueue(size_t maxItems = 0, int threadCount = 1);
74 void SetName(const String& name);
75 String GetName() const;
77 boost::mutex::scoped_lock AcquireLock();
78 void EnqueueUnlocked(boost::mutex::scoped_lock& lock, TaskFunction&& function, WorkQueuePriority priority = PriorityNormal);
79 void Enqueue(TaskFunction&& function, WorkQueuePriority priority = PriorityNormal,
80 bool allowInterleaved = false);
81 void Join(bool stop = false);
83 template<typename VectorType, typename FuncType>
84 void ParallelFor(const VectorType& items, const FuncType& func)
86 using SizeType = decltype(items.size());
88 SizeType totalCount = items.size();
90 auto lock = AcquireLock();
94 for (int i = 0; i < m_ThreadCount; i++) {
95 SizeType count = totalCount / static_cast<SizeType>(m_ThreadCount);
96 if (static_cast<SizeType>(i) < totalCount % static_cast<SizeType>(m_ThreadCount))
99 EnqueueUnlocked(lock, [&items, func, offset, count, this]() {
100 for (SizeType j = offset; j < offset + count; j++) {
101 RunTaskFunction([&func, &items, j]() {
110 ASSERT(offset == items.size());
113 bool IsWorkerThread() const;
115 size_t GetLength() const;
116 size_t GetTaskCount(RingBuffer::SizeType span);
118 void SetExceptionCallback(const ExceptionCallback& callback);
120 bool HasExceptions() const;
121 std::vector<boost::exception_ptr> GetExceptions() const;
122 void ReportExceptions(const String& facility) const;
125 void IncreaseTaskCount();
130 static std::atomic<int> m_NextID;
132 bool m_Spawned{false};
134 mutable boost::mutex m_Mutex;
135 boost::condition_variable m_CVEmpty;
136 boost::condition_variable m_CVFull;
137 boost::condition_variable m_CVStarved;
138 boost::thread_group m_Threads;
140 bool m_Stopped{false};
142 std::priority_queue<Task, std::deque<Task> > m_Tasks;
144 ExceptionCallback m_ExceptionCallback;
145 std::vector<boost::exception_ptr> m_Exceptions;
146 Timer::Ptr m_StatusTimer;
147 double m_StatusTimerTimeout;
149 RingBuffer m_TaskStats;
150 size_t m_PendingTasks{0};
151 double m_PendingTasksTimestamp{0};
153 void WorkerThreadProc();
154 void StatusTimerHandler();
156 void RunTaskFunction(const TaskFunction& func);
161 #endif /* WORKQUEUE_H */