mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-24 14:14:45 +02:00
base: Limit work queue size.
This commit is contained in:
parent
3c3101336a
commit
137c726920
@ -23,6 +23,10 @@
|
||||
|
||||
using namespace icinga;
|
||||
|
||||
WorkQueue::WorkQueue(size_t maxItems)
|
||||
: m_MaxItems(maxItems)
|
||||
{ }
|
||||
|
||||
WorkQueue::~WorkQueue(void)
|
||||
{
|
||||
Join();
|
||||
@ -35,6 +39,10 @@ WorkQueue::~WorkQueue(void)
|
||||
void WorkQueue::Enqueue(const WorkCallback& item)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_Mutex);
|
||||
|
||||
while (m_Items.size() >= m_MaxItems)
|
||||
m_CV.wait(lock);
|
||||
|
||||
m_Items.push_back(item);
|
||||
m_CV.notify_all();
|
||||
|
||||
@ -66,6 +74,7 @@ void WorkQueue::ExecuteItem(void)
|
||||
try {
|
||||
WorkCallback wi = m_Items.front();
|
||||
m_Items.pop_front();
|
||||
m_CV.notify_all();
|
||||
|
||||
lock.unlock();
|
||||
wi();
|
||||
|
@ -39,6 +39,7 @@ class I2_BASE_API WorkQueue
|
||||
public:
|
||||
typedef boost::function<void (void)> WorkCallback;
|
||||
|
||||
WorkQueue(size_t maxItems = 25000);
|
||||
~WorkQueue(void);
|
||||
|
||||
void Enqueue(const WorkCallback& item);
|
||||
@ -48,6 +49,7 @@ public:
|
||||
private:
|
||||
boost::mutex m_Mutex;
|
||||
boost::condition_variable m_CV;
|
||||
size_t m_MaxItems;
|
||||
bool m_Executing;
|
||||
std::deque<WorkCallback> m_Items;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user