void ThreadUtility::priv_data::doPeriodicTasks()

in src/main/cpp/threadutility.cpp [345:402]


void ThreadUtility::priv_data::doPeriodicTasks()
{
	while (!this->terminated)
	{
		auto currentTime = std::chrono::system_clock::now();
		TimePoint nextOperationTime = currentTime + this->maxDelay;
		{
			std::lock_guard<std::recursive_mutex> lock(this->job_mutex);
			for (auto& item : this->jobs)
			{
				if (this->terminated)
					return;
				if (item.removed)
					;
				else if (item.nextRun <= currentTime)
				{
					try
					{
						item.f();
						item.nextRun = std::chrono::system_clock::now() + item.delay;
						if (item.nextRun < nextOperationTime)
							nextOperationTime = item.nextRun;
						item.errorCount = 0;
					}
					catch (std::exception& ex)
					{
						LogLog::warn(item.name, ex);
						++item.errorCount;
					}
					catch (...)
					{
						LogLog::warn(item.name + LOG4CXX_STR(" threw an exception"));
						++item.errorCount;
					}
				}
				else if (item.nextRun < nextOperationTime)
					nextOperationTime = item.nextRun;
			}
		}
		// Delete removed and faulty tasks
		while (1)
		{
			std::lock_guard<std::recursive_mutex> lock(this->job_mutex);
			auto pItem = std::find_if(this->jobs.begin(), this->jobs.end()
				, [this](const NamedPeriodicFunction& item)
				{ return item.removed || this->retryCount < item.errorCount; }
				);
			if (this->jobs.end() == pItem)
				break;
			this->jobs.erase(pItem);
			if (this->jobs.empty())
				return;
		}

		std::unique_lock<std::mutex> lock(this->interrupt_mutex);
		this->interrupt.wait_until(lock, nextOperationTime);
	}
}