7 #include "fwJobs/IJob.hpp" 9 #include "fwJobs/exception/Waiting.hpp" 11 #include <fwCom/Signal.hpp> 12 #include <fwCom/Signal.hxx> 13 #include <fwCom/Signals.hpp> 15 #include <fwThread/Worker.hpp> 16 #include <fwThread/Worker.hxx> 33 m_cancelRequested(false),
44 [&](
const StateHookSeq::value_type& f )
92 State nextState = (
m_state == WAITING) ? CANCELED : CANCELING;
119 if(nextState == CANCELED)
255 [&](
const StateHookSeq::value_type& f )
280 return [ = ] { this->
finish(); };
297 catch( std::future_error& )
315 m_logs.push_back(message);
320 [&](
const LogHookSeq::value_type& f )
408 std::for_each(doneWorkHooks.begin(), doneWorkHooks.end(),
409 [&](
const DoneWorkHookSeq::value_type& f )
411 f(*
this, oldDoneWork);
443 std::for_each(totalWorkUnitsHook.begin(), totalWorkUnitsHook.end(),
444 [&](
const TotalWorkUnitsHookSeq::value_type& f )
446 f(*
this, oldTotalWorkUnits);
CancelHookSeq m_cancelHooks
Container of cancel callbacks. Cancel callbacks will be run when job is canceling.
std::uint64_t m_totalWorkUnits
Number of work units to reach to complete the job.
FWJOBS_API IJob(const std::string &name="")
Default constructor.
FWJOBS_API void addLogHook(LogHook callback)
Add job log callback to sequence for log hook.
FWJOBS_API void log(const std::string &message)
Log a message.
FWJOBS_API void addStateHookNoLock(StateHook callback)
Add cancel callback to sequence without mutex lock for state hook.
::boost::upgrade_lock< ReadWriteMutex > ReadToWriteLock
Defines an upgradable lock type for read/write mutex.
State m_state
Job's state.
FWJOBS_API void addTotalWorkUnitsHook(TotalWorkUnitsHook callback)
Add job work unit callback to sequence for total work unit hook.
bool m_cancelRequested
Determines if cancellation has been requested. The job is/will be canceling if true.
::boost::upgrade_to_unique_lock< ReadWriteMutex > UpgradeToWriteLock
Defines a write lock upgraded from ReadToWriteLock.
std::string m_name
Job's name.
FWJOBS_API void doneWork(std::uint64_t units)
Setter on done work units.
TotalWorkUnitsHookSeq m_totalWorkUnitsHooks
Container of total work unit callbacks. These callbacks take a IJob as parameter. ...
std::shared_ptr< StateSignal > m_sigStarted
Signal emitted when the job has been started.
FWJOBS_API std::function< void() > finishCallback()
Return callback to finish the job.
This class is an interface for class managing job.
DoneWorkHookSeq m_doneWorkHooks
Container of done work callbacks. These callbacks take nothing or a IJob as parameter.
virtual FWJOBS_API SharedFuture cancel()
Cancel the current job and call all available cancel callbacks.
std::shared_future< void > SharedFuture
Future type.
LogHookSeq m_logHooks
Container of log callbacks. These callbacks take a std::string as parameter.
FWJOBS_API State getState() const
Getter on the current State.
virtual FWJOBS_API SharedFuture runImpl()=0
Run an instanciated job.
FWJOBS_API void addLogHookNoLock(LogHook callback)
Add job log callback to sequence without mutex lock for log hook.
SharedFuture m_runFuture
Job's future returned by 'run'.
std::shared_ptr< DoneWorkSignal > m_sigDoneWork
Signal emitted when done work units have been changed. Takes a std::uint64_t as parameter.
bool m_cancelable
Determines if the job can be cancelled. The job is cancelable if true.
std::shared_ptr< CancelRequestedSignal > m_sigCancelRequested
Signal emitted when cancel has been requested.
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
FWJOBS_API void setCancelable(bool cancel)
Setter on cancelable.
FWJOBS_API const bool & cancelRequested() const
Returns the job canceling status.
std::shared_ptr< StateSignal > m_sigCanceled
Signal emitted when te job has been canceled.
virtual FWJOBS_API ~IJob()
Default destructor.
FWJOBS_API void addSimpleCancelHook(CancelHook callback)
Add cancel callback to sequence for cancel hook.
FWJOBS_API std::uint64_t getDoneWorkUnits() const
Getter on the number of done work units.
FWJOBS_API void setTotalWorkUnits(std::uint64_t units)
Setter on total work units.
std::shared_ptr< StateSignal > m_sigFinished
Signal emitted when the job has been finished.
mutable::fwCore::mt::ReadWriteMutex m_mutex
Mutex to protect object access.
std::function< void(IJob &, const std::string &) > LogHook
Log callback type for log hook.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
FWJOBS_API void wait()
Wait job execution ending.
FWJOBS_API void addTotalWorkUnitsHookNoLock(TotalWorkUnitsHook callback)
Add job work unit callback to sequence without mutex lock for total work unit hook.
::boost::shared_lock< ReadWriteMutex > ReadLock
Defines a lock of read type for read/write mutex.
std::function< void(State) > StateHook
State callback type for state hook.
std::function< void(IJob &, std::uint64_t) > TotalWorkUnitsHook
Work units callback type for total work unit hook.
std::function< void(IJob &) > JobCancelHook
Job cancel callback type for cancel hook.
FWJOBS_API void finishNoLock()
Finish the job without mutex lock: set the state to finished or canceled.
std::function< void(IJob &, std::uint64_t) > DoneWorkHook
Done work callback type for done work hook.
std::function< bool() > CancelRequestCallback
Cancel request callback type.
FWJOBS_API void addStateHook(StateHook callback)
Add job state callback to sequence for state hook.
FWJOBS_API void addCancelHook(JobCancelHook callback)
Add cancel callback to sequence for cancel hook.
FWJOBS_API void addCancelHookNoLock(JobCancelHook callback)
Add job cancel callback to sequence without mutex lock for cancel hook.
FWJOBS_API bool isCancelable() const
Getter on cancelable, returns whether the job is cancelable.
FWJOBS_API SharedFuture run()
Run the current job.
FWJOBS_API Logs getLogs() const
Getter on the log container.
FWJOBS_API void addDoneWorkHookNoLock(DoneWorkHook callback)
Add job done work unit callback to sequence without mutex lock for done work hook.
Logs m_logs
Logs container.
FWJOBS_API void setState(State state)
Setter on the state.
FWJOBS_API CancelRequestCallback cancelRequestedCallback() const
Returns a callback on job canceling status. This callback can only be used if the job is still instan...
FWJOBS_API State getStateNoLock() const
Getter on the state without mutex lock.
FWJOBS_API const std::string & getName() const
Getter on the name of the job.
FWJOBS_API void done()
Set done work units to total work units.
StateHookSeq m_stateHooks
Container of state callbacks. These callbacks take a State as parameter.
FWJOBS_API std::uint64_t getTotalWorkUnits() const
Getter on the total number of work units.
FWJOBS_API void setTotalWorkUnitsUpgradeLock(std::uint64_t units,::fwCore::mt::ReadToWriteLock &lock)
Setter on total work units.
std::function< void() > CancelHook
Cancel callback type for cancel hook.
std::vector< std::string > Logs
Log container type.
FWJOBS_API void logNoLock(const std::string &message)
Add a message to thelog sequence.
This namespace fwJobs provides jobs management.
std::shared_ptr< LogSignal > m_sigLogged
Signal emitted when a message has been added to logs. Takes a std::string as parameter.
std::uint64_t m_doneWorkUnits
Number of work units already reached.
FWJOBS_API void setStateNoLock(State state)
Setter on the state without mutex lock.
virtual FWJOBS_API void finish()
Finish the job: set state to finished or canceled.
FWJOBS_API void addDoneWorkHook(DoneWorkHook callback)
Add job done work unit callback to sequence for done work hook.