7 #include "fwJobs/Aggregator.hpp" 9 #include "fwJobs/exception/Waiting.hpp" 10 #include "fwJobs/Job.hpp" 14 #include <fwThread/Worker.hpp> 15 #include <fwThread/Worker.hxx> 26 return std::make_shared<Aggregator>( name );
53 std::vector< SharedFuture > futures;
54 for(const ::fwJobs::IJob::sptr& iJob : jobs)
56 futures.push_back(iJob->run());
59 auto future = std::async(
62 std::for_each(futures.begin(), futures.end(), std::mem_fn(&::std::shared_future<void>::wait));
74 return std::move(future);
83 SLM_ASSERT(
"iJob shall not be added to itself",
this != iJob.get());
94 const auto normValue = std::uint64_t(weight*100);
98 m_jobInfo[iJob.get()] = JobInfo( *iJob );
99 auto& jobInfo = m_jobInfo[iJob.get()];
106 jobInfo.lastValue = std::uint64_t(jobInfo.progress() * normValue);
109 m_jobs.push_back(iJob);
112 this->
doneWork(jobInfo.lastValue, lock);
115 iJob->addDoneWorkHook(
116 [ =, &jobInfo](
IJob& subJob, std::uint64_t oldDoneWork)
120 auto oldInfo = jobInfo;
121 jobInfo = Aggregator::JobInfo( subJob );
123 jobInfo.lastValue = std::uint64_t(normValue * jobInfo.progress());
127 doneWork -= oldInfo.lastValue;
129 this->
doneWork( static_cast<std::uint64_t>(doneWork), sublock );
133 iJob->addTotalWorkUnitsHook(
134 [ = ](
IJob& subJob, std::uint64_t oldTotalWorkUnits)
141 if( oldTotalWorkUnits != newTotalWorkUnits)
143 if( oldTotalWorkUnits && 0 == newTotalWorkUnits)
145 workUnits -= normValue;
147 else if( 0 == oldTotalWorkUnits && newTotalWorkUnits)
149 workUnits += normValue;
162 auto iJobName = iJob->getName();
163 iJobName = iJobName.empty() ?
"" :
"[" + iJobName +
"] ";
164 iJob->addLogHook( [ = ](
IJob& ,
const std::string& message)
166 this->
log( iJobName + message);
169 auto iJobLogs = iJob->getLogs();
170 std::for_each(iJobLogs.begin(), iJobLogs.end(),
171 [&](
const Logs::value_type& message )
FWJOBS_API Aggregator()
Default constructor. The name is initialized with an empty string.
std::uint64_t m_totalWorkUnits
Number of work units to reach to complete the job.
FWJOBS_API void log(const std::string &message)
Log a message.
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
FWJOBS_API SharedFuture runImpl()
Run all the jobs of the Aggregator.
State m_state
Job's state.
::boost::upgrade_to_unique_lock< ReadWriteMutex > UpgradeToWriteLock
Defines a write lock upgraded from ReadToWriteLock.
FWJOBS_API void doneWork(std::uint64_t units)
Setter on done work units.
FWJOBS_API IJobSeq getSubJobs()
Retrieve sub job sequence of the Aggregator.
This class is an interface for class managing job.
std::shared_future< void > SharedFuture
Future type.
::boost::multi_index_container< ::fwJobs::IJob::sptr,::boost::multi_index::indexed_by< ::boost::multi_index::random_access<>,::boost::multi_index::hashed_unique< ::boost::multi_index::identity< ::fwJobs::IJob::sptr > > > > IJobSeq
Aggregator container type.
std::shared_ptr< ::fwJobs::Aggregator > sptr
Aggregator container type.
mutable::fwCore::mt::ReadWriteMutex m_mutex
Mutex to protect object access.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
FWJOBS_API void addCancelHookNoLock(JobCancelHook callback)
Add job cancel callback to sequence without mutex lock for cancel hook.
FWJOBS_API void add(const ::fwJobs::IJob::sptr &iJob, double weight=1.)
Add an IJob to the Aggregator.
FWJOBS_API std::uint64_t getTotalWorkUnits() const
Getter on the total number of work units.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...
FWJOBS_API void setTotalWorkUnitsUpgradeLock(std::uint64_t units,::fwCore::mt::ReadToWriteLock &lock)
Setter on total work units.
static FWJOBS_API sptr New(const std::string &name="")
Create a new Aggregator smart pointer.
FWJOBS_API void logNoLock(const std::string &message)
Add a message to thelog sequence.
This namespace fwJobs provides jobs management.
std::uint64_t m_doneWorkUnits
Number of work units already reached.
virtual FWJOBS_API void finish()
Finish the job: set state to finished or canceled.