ThreadableJob.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ThreadableJob.h - declaration of class ThreadableJob
  3. *
  4. * Copyright (c) 2009-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef THREADABLE_JOB_H
  25. #define THREADABLE_JOB_H
  26. #include "AtomicInt.h"
  27. #include "lmms_basics.h"
  28. class ThreadableJob
  29. {
  30. public:
  31. enum ProcessingState
  32. {
  33. Unstarted,
  34. Queued,
  35. InProgress,
  36. Done
  37. };
  38. ThreadableJob() :
  39. m_state( ThreadableJob::Unstarted )
  40. {
  41. }
  42. inline ProcessingState state() const
  43. {
  44. return static_cast<ProcessingState>( (int) m_state );
  45. }
  46. inline void reset()
  47. {
  48. m_state = Unstarted;
  49. }
  50. inline void queue()
  51. {
  52. m_state = Queued;
  53. }
  54. inline void done()
  55. {
  56. m_state = Done;
  57. }
  58. void process()
  59. {
  60. if( m_state.testAndSetOrdered( Queued, InProgress ) )
  61. {
  62. doProcessing();
  63. m_state = Done;
  64. }
  65. }
  66. virtual bool requiresProcessing() const = 0;
  67. protected:
  68. virtual void doProcessing() = 0;
  69. AtomicInt m_state;
  70. } ;
  71. #endif