AudioEngineWorkerThread.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * AudioEngineWorkerThread.h - declaration of class AudioEngineWorkerThread
  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 AUDIO_ENGINE_WORKER_THREAD_H
  25. #define AUDIO_ENGINE_WORKER_THREAD_H
  26. #include <QtCore/QThread>
  27. #include <atomic>
  28. class AudioEngine;
  29. class QWaitCondition;
  30. class ThreadableJob;
  31. class AudioEngineWorkerThread : public QThread
  32. {
  33. Q_OBJECT
  34. public:
  35. // internal representation of the job queue - all functions are thread-safe
  36. class JobQueue
  37. {
  38. public:
  39. enum OperationMode
  40. {
  41. Static, // no jobs added while processing queue
  42. Dynamic // jobs can be added while processing queue
  43. } ;
  44. static constexpr size_t JOB_QUEUE_SIZE = 8192;
  45. JobQueue() :
  46. m_items(),
  47. m_writeIndex( 0 ),
  48. m_itemsDone( 0 ),
  49. m_opMode( Static )
  50. {
  51. std::fill(m_items, m_items + JOB_QUEUE_SIZE, nullptr);
  52. }
  53. void reset( OperationMode _opMode );
  54. void addJob( ThreadableJob * _job );
  55. void run();
  56. void wait();
  57. private:
  58. std::atomic<ThreadableJob*> m_items[JOB_QUEUE_SIZE];
  59. std::atomic_int m_writeIndex;
  60. std::atomic_int m_itemsDone;
  61. OperationMode m_opMode;
  62. } ;
  63. AudioEngineWorkerThread( AudioEngine* audioEngine );
  64. virtual ~AudioEngineWorkerThread();
  65. virtual void quit();
  66. static void resetJobQueue( JobQueue::OperationMode _opMode =
  67. JobQueue::Static )
  68. {
  69. globalJobQueue.reset( _opMode );
  70. }
  71. static void addJob( ThreadableJob * _job )
  72. {
  73. globalJobQueue.addJob( _job );
  74. }
  75. // a convenient helper function allowing to pass a container with pointers
  76. // to ThreadableJob objects
  77. template<typename T>
  78. static void fillJobQueue( const T & _vec,
  79. JobQueue::OperationMode _opMode = JobQueue::Static )
  80. {
  81. resetJobQueue( _opMode );
  82. for( typename T::ConstIterator it = _vec.begin(); it != _vec.end(); ++it )
  83. {
  84. addJob( *it );
  85. }
  86. }
  87. static void startAndWaitForJobs();
  88. private:
  89. void run() override;
  90. static JobQueue globalJobQueue;
  91. static QWaitCondition * queueReadyWaitCond;
  92. static QList<AudioEngineWorkerThread *> workerThreads;
  93. volatile bool m_quit;
  94. } ;
  95. #endif