/xbmc/utils/Job.h

http://github.com/xbmc/xbmc · C Header · 160 lines · 37 code · 14 blank · 109 comment · 0 complexity · 2548fb87e9eca7fde0ddae672257ef85 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. class CJob;
  10. #include <stddef.h>
  11. #define kJobTypeMediaFlags "mediaflags"
  12. #define kJobTypeCacheImage "cacheimage"
  13. #define kJobTypeDDSCompress "ddscompress"
  14. /*!
  15. \ingroup jobs
  16. \brief Callback interface for asynchronous jobs.
  17. Used by clients of the CJobManager to receive progress and completion notification of jobs.
  18. Clients of small jobs wishing to perform actions on job completion should implement the
  19. IJobCallback::OnJobComplete() function. Clients of larger jobs may choose to implement the
  20. IJobCallback::OnJobProgress() function in order to be kept informed of progress.
  21. \sa CJobManager and CJob
  22. */
  23. class IJobCallback
  24. {
  25. public:
  26. /*!
  27. \brief Destructor for job call back objects.
  28. \sa CJobManager and CJob
  29. */
  30. virtual ~IJobCallback() = default;
  31. /*!
  32. \brief The callback used when a job completes.
  33. OnJobComplete is called at the completion of the job's DoWork() function, and is used
  34. to return information to the caller on the result of the job. On returning form this function
  35. the CJobManager will destroy this job.
  36. \param jobID the unique id of the job (as retrieved from CJobManager::AddJob)
  37. \param success the result from the DoWork call
  38. \param job the job that has been processed. The job will be destroyed after this function returns
  39. \sa CJobManager and CJob
  40. */
  41. virtual void OnJobComplete(unsigned int jobID, bool success, CJob *job)=0;
  42. /*!
  43. \brief An optional callback function that a job may call while processing.
  44. OnJobProgress may be called periodically by a job during it's DoWork() function. It is used
  45. by the job to report on progress.
  46. \param jobID the unique id of the job (as retrieved from CJobManager::AddJob)
  47. \param progress the current progress of the job, out of total.
  48. \param total the total amount of work to be processed.
  49. \param job the job that has been processed.
  50. \sa CJobManager and CJob
  51. */
  52. virtual void OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job) {};
  53. };
  54. class CJobManager;
  55. /*!
  56. \ingroup jobs
  57. \brief Base class for jobs that are executed asynchronously.
  58. Clients of the CJobManager should subclass CJob and provide the DoWork() function. Data should be
  59. passed to the job on creation, and any data sharing between the job and the client should be kept to within
  60. the callback functions if possible, and guarded with critical sections as appropriate.
  61. Jobs typically fall into two groups: small jobs that perform a single function, and larger jobs that perform a
  62. sequence of functions. Clients with small jobs should implement the IJobCallback::OnJobComplete() callback to receive results.
  63. Clients with larger jobs may wish to implement both the IJobCallback::OnJobComplete() and IJobCallback::OnJobProgress()
  64. callbacks to receive updates. Jobs may be cancelled at any point by the client via CJobManager::CancelJob(), however
  65. effort should be taken to ensure that any callbacks and cancellation is suitably guarded against simultaneous thread access.
  66. Handling cancellation of jobs within the OnJobProgress callback is a threadsafe operation, as all execution is
  67. then in the Job thread.
  68. \sa CJobManager and IJobCallback
  69. */
  70. class CJob
  71. {
  72. public:
  73. /*!
  74. \brief Priority levels for jobs, specified by clients when adding jobs to the CJobManager.
  75. \sa CJobManager
  76. */
  77. enum PRIORITY {
  78. PRIORITY_LOW_PAUSABLE = 0,
  79. PRIORITY_LOW,
  80. PRIORITY_NORMAL,
  81. PRIORITY_HIGH,
  82. PRIORITY_DEDICATED, // will create a new worker if no worker is available at queue time
  83. };
  84. CJob() { m_callback = NULL; };
  85. /*!
  86. \brief Destructor for job objects.
  87. Jobs are destroyed by the CJobManager after the OnJobComplete() callback is complete.
  88. CJob subclasses should therefore supply a virtual destructor to cleanup any memory allocated by
  89. complete or cancelled jobs.
  90. \sa CJobManager
  91. */
  92. virtual ~CJob() = default;
  93. /*!
  94. \brief Main workhorse function of CJob instances
  95. All CJob subclasses must implement this function, performing all processing. Once this function
  96. is complete, the OnJobComplete() callback is called, and the job is then destroyed.
  97. \sa CJobManager, IJobCallback::OnJobComplete()
  98. */
  99. virtual bool DoWork() = 0; // function to do the work
  100. /*!
  101. \brief Function that returns the type of job.
  102. CJob subclasses may optionally implement this function to specify the type of job.
  103. This is useful for the CJobManager::AddLIFOJob() routine, which preempts similar jobs
  104. with the new job.
  105. \return a unique character string describing the job.
  106. \sa CJobManager
  107. */
  108. virtual const char *GetType() const { return ""; };
  109. virtual bool operator==(const CJob* job) const
  110. {
  111. return false;
  112. }
  113. /*!
  114. \brief Function for longer jobs to report progress and check whether they have been cancelled.
  115. Jobs that contain loops that may take time should check this routine each iteration of the loop,
  116. both to (optionally) report progress, and to check for cancellation.
  117. \param progress the amount of the job performed, out of total.
  118. \param total the total amount of processing to be performed
  119. \return if true, the job has been asked to cancel.
  120. \sa IJobCallback::OnJobProgress()
  121. */
  122. virtual bool ShouldCancel(unsigned int progress, unsigned int total) const;
  123. private:
  124. friend class CJobManager;
  125. CJobManager *m_callback;
  126. };