2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief A parent class to represent long-running tasks which are run in their own thread.
24 #include <boost/thread.hpp>
29 using namespace boost;
31 /** @param s FilmState for the film that we are operating on.
33 * @param l A log that we can write to.
35 Job::Job (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
41 , _progress_unknown (false)
48 /** Start the job in a separate thread, returning immediately */
53 _start_time = time (0);
54 boost::thread (boost::bind (&Job::run_wrapper, this));
57 /** A wrapper for the ::run() method to catch exceptions */
65 } catch (std::exception& e) {
68 set_state (FINISHED_ERROR);
69 set_error (e.what ());
74 /** @return true if the job is running */
78 boost::mutex::scoped_lock lm (_state_mutex);
79 return _state == RUNNING;
82 /** @return true if the job has finished (either successfully or unsuccessfully) */
84 Job::finished () const
86 boost::mutex::scoped_lock lm (_state_mutex);
87 return _state == FINISHED_OK || _state == FINISHED_ERROR;
90 /** @return true if the job has finished successfully */
92 Job::finished_ok () const
94 boost::mutex::scoped_lock lm (_state_mutex);
95 return _state == FINISHED_OK;
98 /** @return true if the job has finished unsuccessfully */
100 Job::finished_in_error () const
102 boost::mutex::scoped_lock lm (_state_mutex);
103 return _state == FINISHED_ERROR;
106 /** Set the state of this job.
107 * @param s New state.
110 Job::set_state (State s)
112 boost::mutex::scoped_lock lm (_state_mutex);
116 /** A hack to work around our lack of cross-thread
117 * signalling; this emits Finished, and listeners
118 * assume that it will be emitted in the GUI thread,
119 * so this method must be called from the GUI thread.
122 Job::emit_finished ()
127 /** @return Time (in seconds) that this job has been running */
129 Job::elapsed_time () const
131 if (_start_time == 0) {
135 return time (0) - _start_time;
138 /** Set the progress of the current part of the job.
139 * @param p Progress (from 0 to 1)
142 Job::set_progress (float p)
144 boost::mutex::scoped_lock lm (_progress_mutex);
145 _stack.back().normalised = p;
148 /** @return fractional overall progress, or -1 if not known */
150 Job::overall_progress () const
152 boost::mutex::scoped_lock lm (_progress_mutex);
153 if (_progress_unknown) {
159 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
160 factor *= i->allocation;
161 overall += i->normalised * factor;
171 /** Ascend up one level in terms of progress reporting; see descend() */
175 boost::mutex::scoped_lock lm (_progress_mutex);
177 assert (!_stack.empty ());
178 float const a = _stack.back().allocation;
180 _stack.back().normalised += a;
183 /** Descend down one level in terms of progress reporting; e.g. if
184 * there is a task which is split up into N subtasks, each of which
185 * report their progress from 0 to 100%, call descend() before executing
186 * each subtask, and ascend() afterwards to ensure that overall progress
187 * is reported correctly.
189 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
192 Job::descend (float a)
194 boost::mutex::scoped_lock lm (_progress_mutex);
195 _stack.push_back (Level (a));
198 /** @return Any error string that the job has generated */
202 boost::mutex::scoped_lock lm (_state_mutex);
206 /** Set the current error string.
207 * @param e New error string.
210 Job::set_error (string e)
212 boost::mutex::scoped_lock lm (_state_mutex);
216 /** Set that this job's progress will always be unknown */
218 Job::set_progress_unknown ()
220 boost::mutex::scoped_lock lm (_progress_mutex);
221 _progress_unknown = true;
227 float const p = overall_progress ();
228 int const t = elapsed_time ();
231 if (!finished () && p >= 0 && t > 10) {
232 s << rint (p * 100) << "%; about " << seconds_to_approximate_hms (t / p - t) << " remaining";
233 } else if (!finished () && t <= 10) {
234 s << rint (p * 100) << "%";
235 } else if (finished_ok ()) {
236 s << "OK (ran for " << seconds_to_hms (t) << ")";
237 } else if (finished_in_error ()) {
238 s << "Error (" << error() << ")";