8 #include <condition_variable>
32 this->running =
false;
33 this->stopped =
false;
34 this->finished =
false;
35 this->workload = 0.0f;
47 RunningTaskIceBase::name = name;
52 impl->customThreadList = threadList;
56 impl->customThreadList->addRunningTask(
this);
63 if (app && app->getForbidThreadCreation())
65 throw LocalException() <<
"Thread creation is now allowed in this application at the point in time! Use Application::getInstance()->getThreadPool() instead.";
68 if (!running && !finished)
70 impl->threadJoined =
false;
71 startTime = IceUtil::Time::now().toMicroSeconds();
72 IceUtil::Thread::start();
77 if (!running && finished)
79 throw LocalException(
"Running Task '" + RunningTaskIceBase::name +
"' is already finished and cannot be started again.");
85 std::unique_lock stoppingMutexLock{impl->stoppingMutex, std::defer_lock};
89 impl->stoppingMutex.lock();
91 else if (!stoppingMutexLock.try_lock())
103 std::unique_lock lock(impl->stopMutex);
105 impl->stopCondition.notify_all();
110 if (waitForJoin && !impl->threadJoined)
112 impl->threadJoined =
true;
113 getThreadControl().join();
116 impl->stoppingMutex.unlock();
119 catch (IceUtil::ThreadSyscallException& e)
121 impl->stoppingMutex.unlock();
123 catch (IceUtil::ThreadNotStartedException& e)
125 impl->stoppingMutex.unlock();
130 impl->stoppingMutex.unlock();
138 std::unique_lock lock(impl->stoppingMutex);
139 if (!impl->threadJoined)
141 impl->threadJoined =
true;
142 getThreadControl().join();
148 return this->running;
153 return this->finished;
158 std::unique_lock lock(impl->finishedMutex);
159 if (!running && !finished)
167 impl->finishedCondition.wait(lock);
169 else if (impl->finishedCondition.wait_for(lock, std::chrono::milliseconds(timeoutMS)) == std::cv_status::timeout)
180 lastFeedbackTime = IceUtil::Time::now().toMicroSeconds();
186 std::unique_lock lock(impl->stopMutex);
190 impl->stopCondition.wait(lock);
197 return RunningTaskIceBase::name;
200 void RunningTaskBase::run()
205 if (impl->customThreadList)
207 impl->customThreadList->addRunningTask(
this);
218 throw std::runtime_error(
"No callback defined in RunningTask");
226 std::unique_lock lock(impl->finishedMutex);
231 if (impl->customThreadList)
233 impl->customThreadList->removeRunningTask(
this);
236 impl->stopCondition.notify_all();
237 impl->finishedCondition.notify_all();