fw4spl
Job.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #define BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS
8 
9 #include "fwJobs/Job.hpp"
10 
11 #include "fwJobs/exception/Waiting.hpp"
12 
13 #include <fwThread/Worker.hpp>
14 #include <fwThread/Worker.hxx>
15 
16 #include <boost/scope_exit.hpp>
17 
18 #include <memory>
19 
20 namespace fwJobs
21 {
22 
23 //------------------------------------------------------------------------------
24 
25 Job::sptr Job::New(const std::string& name, Job::Task task, const ::fwThread::Worker::sptr& worker)
26 {
27  return std::make_shared<Job>( name, task, worker );
28 }
29 
30 //------------------------------------------------------------------------------
31 
32 Job::Job(const std::string& name, Job::Task task, const ::fwThread::Worker::sptr& worker) :
33  IJob(name),
34  m_task(task),
35  m_worker(worker)
36 {
37  m_totalWorkUnits = 100;
38 }
39 
40 //------------------------------------------------------------------------------
41 
43 {
44  // No need to lock : m_task & m_worker only writable in constructor
45  if(m_task)
46  {
47  // workaround because of vs2010 issue : http://goo.gl/WHEkQ5
48  // see IJob.hpp
49  auto fCallback = this->finishCallback();
50 
51  auto jobTask = [ = ]()
52  {
53  BOOST_SCOPE_EXIT_ALL( = )
54  {
55  fCallback();
57  m_task = nullptr;
58  };
59 
60  m_task(*this);
61  };
62  if(m_worker)
63  {
64  return m_worker->postTask< void >( jobTask );
65  }
66  else
67  {
68  jobTask();
69  }
70  }
71  else
72  {
73  this->finish();
74  }
75 
76  return ::std::async( []() {} );
77 }
78 
79 //------------------------------------------------------------------------------
80 
82 {
83  auto future = this->IJob::cancel();
85  if (m_task)
86  {
87  m_task = nullptr;
88  }
89  return future;
90 }
91 
92 //------------------------------------------------------------------------------
93 
95 {
96  return [this](std::uint64_t doneWork)
97  {
98  this->doneWork(doneWork);
99  };
100 }
101 
102 //------------------------------------------------------------------------------
103 
104 ::fwThread::Worker::sptr Job::getWorker()
105 {
106  // No need to lock : m_worker only writable in contructor
107  return m_worker;
108 }
109 
110 } //namespace fwJobs
FWJOBS_API SharedFuture runImpl()
Run the task of the job and mark the job as finished.
Definition: Job.cpp:42
std::uint64_t m_totalWorkUnits
Number of work units to reach to complete the job.
Definition: IJob.hpp:376
FWJOBS_API ProgressCallback progressCallback()
Return a job callback with the done work number as parameter This callback can only be used if the jo...
Definition: Job.cpp:94
FWJOBS_API void doneWork(std::uint64_t units)
Setter on done work units.
Definition: IJob.cpp:379
FWJOBS_API std::function< void() > finishCallback()
Return callback to finish the job.
Definition: IJob.cpp:278
This class is an interface for class managing job.
Definition: IJob.hpp:28
virtual FWJOBS_API SharedFuture cancel()
Cancel the current job and call all available cancel callbacks.
Definition: IJob.cpp:87
std::shared_future< void > SharedFuture
Future type.
Definition: IJob.hpp:109
::boost::unique_lock< ReadWriteMutex > WriteLock
Defines a lock of write type for read/write mutex.
virtual FWJOBS_API SharedFuture cancel()
Reimplements IJob&#39;s cancel.
Definition: Job.cpp:81
mutable::fwCore::mt::ReadWriteMutex m_mutex
Mutex to protect object access.
Definition: IJob.hpp:364
FWJOBS_API std::shared_ptr< ::fwThread::Worker > getWorker()
Getter on the current job worker or nullptr if no worker has been set.
Definition: Job.cpp:104
FWJOBS_API Job(const std::string &name, Task task, const std::shared_ptr< ::fwThread::Worker > &worker=nullptr)
Construct a new job.
Definition: Job.cpp:32
static FWJOBS_API sptr New(const std::string &name, Task task, const std::shared_ptr< ::fwThread::Worker > &worker=nullptr)
Construct a new job and return a smart pointer of it.
Definition: Job.cpp:25
std::function< void(Job &) > Task
Task type.
Definition: Job.hpp:40
This namespace fwJobs provides jobs management.
std::shared_ptr< ::fwJobs::Job > sptr
Task type.
Definition: Job.hpp:36
virtual FWJOBS_API void finish()
Finish the job: set state to finished or canceled.
Definition: IJob.cpp:263
std::function< void(std::uint64_t) > ProgressCallback
Progress callback type.
Definition: Job.hpp:43