3#include <condition_variable>
31 this->running =
false;
32 this->stopped =
false;
33 this->finished =
false;
34 this->workload = 0.0f;
47 RunningTaskIceBase::name = name;
53 impl->customThreadList = threadList;
57 impl->customThreadList->addRunningTask(
this);
65 if (app && app->getForbidThreadCreation())
67 throw LocalException()
68 <<
"Thread creation is now allowed in this application at the point in time! Use "
69 "Application::getInstance()->getThreadPool() instead.";
72 if (!running && !finished)
74 impl->threadJoined =
false;
75 startTime = IceUtil::Time::now().toMicroSeconds();
76 IceUtil::Thread::start();
81 if (!running && finished)
83 throw LocalException(
"Running Task '" + RunningTaskIceBase::name +
84 "' is already finished and cannot be started again.");
91 std::unique_lock stoppingMutexLock{impl->stoppingMutex, std::defer_lock};
95 impl->stoppingMutex.lock();
97 else if (!stoppingMutexLock.try_lock())
109 std::unique_lock lock(impl->stopMutex);
111 impl->stopCondition.notify_all();
116 if (waitForJoin && !impl->threadJoined)
118 impl->threadJoined =
true;
119 getThreadControl().join();
122 impl->stoppingMutex.unlock();
124 catch (IceUtil::ThreadSyscallException&
127 impl->stoppingMutex.unlock();
129 catch (IceUtil::ThreadNotStartedException& e)
131 impl->stoppingMutex.unlock();
136 impl->stoppingMutex.unlock();
144 std::unique_lock lock(impl->stoppingMutex);
145 if (!impl->threadJoined)
147 impl->threadJoined =
true;
148 getThreadControl().join();
155 return this->running;
161 return this->finished;
167 std::unique_lock lock(impl->finishedMutex);
168 if (!running && !finished)
176 impl->finishedCondition.wait(lock);
178 else if (impl->finishedCondition.wait_for(lock, std::chrono::milliseconds(timeoutMS)) ==
179 std::cv_status::timeout)
190 lastFeedbackTime = IceUtil::Time::now().toMicroSeconds();
197 std::unique_lock lock(impl->stopMutex);
201 impl->stopCondition.wait(lock);
208 return RunningTaskIceBase::name;
212 RunningTaskBase::run()
217 if (impl->customThreadList)
219 impl->customThreadList->addRunningTask(
this);
230 throw std::runtime_error(
"No callback defined in RunningTask");
238 std::unique_lock lock(impl->finishedMutex);
243 if (impl->customThreadList)
245 impl->customThreadList->removeRunningTask(
this);
248 impl->stopCondition.notify_all();
249 impl->finishedCondition.notify_all();
static ApplicationPtr getInstance()
Retrieve shared pointer to the application object.
static long getThreadId()
~RunningTaskBase() override
Destructor stops the thread and waits for completion.
bool isStopped()
Retrieve whether stop() has been called.
bool waitForFinished(int timeoutMS=-1)
wait blocking for thread to be finished.
bool isFinished() const
Retrieve finished state of the thread.
void start()
Starts the thread.
void join()
Wait for the RunningTask to finish without telling it to finish.
std::string getName() const
void waitForStop()
Wait blocking for thread until stop() has been called.
void setName(const std::string &name)
void stop(bool waitForJoin=true)
Stops the thread.
void setThreadList(ThreadListPtr threadList)
bool isRunning() const
Retrieve running state of the thread.
RunningTaskBase(std::string const &name)
static ThreadListPtr getApplicationThreadList()
getApplicationThreadList retrieves the ThreadList, that contains all TimerTasks and PeriodicTasks in ...
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< ThreadList > ThreadListPtr
std::condition_variable stopCondition
ThreadListPtr customThreadList
std::mutex stoppingMutex
mutex for the stop function
std::mutex stopMutex
mutex for the stopped status
std::condition_variable finishedCondition
std::mutex finishedMutex
mutex for the finished status