3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
28 \brief Callback interface for asynchronous jobs.
30 Used by clients of the CJobManager to receive progress and completion notification of jobs.
31 Clients of small jobs wishing to perform actions on job completion should implement the
32 IJobCallback::OnJobComplete() function. Clients of larger jobs may choose to implement the
33 IJobCallback::OnJobProgress() function in order to be kept informed of progress.
35 \sa CJobManager and CJob
41 \brief Destructor for job call back objects.
43 \sa CJobManager and CJob
45 virtual ~IJobCallback() {};
48 \brief The callback used when a job completes.
50 OnJobComplete is called at the completion of the job's DoWork() function, and is used
51 to return information to the caller on the result of the job. On returning form this function
52 the CJobManager will destroy this job.
54 \param jobID the unique id of the job (as retrieved from CJobManager::AddJob)
55 \param success the result from the DoWork call
56 \param job the job that has been processed. The job will be destroyed after this function returns
57 \sa CJobManager and CJob
59 virtual void OnJobComplete(unsigned int jobID, bool success, CJob *job)=0;
62 \brief An optional callback function that a job may call while processing.
64 OnJobProgress may be called periodically by a job during it's DoWork() function. It is used
65 by the job to report on progress.
67 \param jobID the unique id of the job (as retrieved from CJobManager::AddJob)
68 \param progress the current progress of the job, out of total.
69 \param total the total amount of work to be processed.
70 \param job the job that has been processed.
71 \sa CJobManager and CJob
73 virtual void OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job) {};
80 \brief Base class for jobs that are executed asyncronously.
82 Clients of the CJobManager should subclass CJob and provide the DoWork() function. Data should be
83 passed to the job on creation, and any data sharing between the job and the client should be kept to within
84 the callback functions if possible, and guarded with critical sections as appropriate.
86 Jobs typically fall into two groups: small jobs that perform a single function, and larger jobs that perform a
87 sequence of functions. Clients with small jobs should implement the IJobCallback::OnJobComplete() callback to receive results.
88 Clients with larger jobs may wish to implement both the IJobCallback::OnJobComplete() and IJobCallback::OnJobProgress()
89 callbacks to receive updates. Jobs may be cancelled at any point by the client via CJobManager::CancelJob(), however
90 effort should be taken to ensure that any callbacks and cancellation is suitably guarded against simultaneous thread access.
92 Handling cancellation of jobs within the OnJobProgress callback is a threadsafe operation, as all execution is
93 then in the Job thread.
95 \sa CJobManager and IJobCallback
101 \brief Priority levels for jobs, specified by clients when adding jobs to the CJobManager.
109 CJob() { m_callback = NULL; };
112 \brief Destructor for job objects.
114 Jobs are destroyed by the CJobManager after the OnJobComplete() callback is complete.
115 CJob subclasses should therefore supply a virtual destructor to cleanup any memory allocated by
116 complete or cancelled jobs.
123 \brief Main workhorse function of CJob instances
125 All CJob subclasses must implement this function, performing all processing. Once this function
126 is complete, the OnJobComplete() callback is called, and the job is then destroyed.
128 \sa CJobManager, IJobCallback::OnJobComplete()
130 virtual bool DoWork() = 0; // function to do the work
133 \brief Function that returns the type of job.
135 CJob subclasses may optionally implement this function to specify the type of job.
136 This is useful for the CJobManager::AddLIFOJob() routine, which preempts similar jobs
139 \return a unique character string describing the job.
142 virtual const char *GetType() const { return ""; };
144 virtual bool operator==(const CJob* job) const
150 \brief Function for longer jobs to report progress and check whether they have been cancelled.
152 Jobs that contain loops that may take time should check this routine each iteration of the loop,
153 both to (optionally) report progress, and to check for cancellation.
155 \param progress the amount of the job performed, out of total.
156 \param total the total amount of processing to be performed
157 \return if true, the job has been asked to cancel.
159 \sa IJobCallback::OnJobProgress()
161 bool ShouldCancel(unsigned int progress, unsigned int total) const;
163 friend class CJobManager;
164 CJobManager *m_callback;