dispatchqueue.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "dispatchqueue.hpp"
  19. DispatchQueue::DispatchQueue(std::string name, size_t threadCount) :
  20. name{std::move(name)}, threads(threadCount) {
  21. for(size_t i = 0; i < threads.size(); i++)
  22. {
  23. threads[i] = std::thread(&DispatchQueue::dispatchThreadHandler, this);
  24. }
  25. }
  26. DispatchQueue::~DispatchQueue() {
  27. // Signal to dispatch threads that it's time to wrap up
  28. std::unique_lock<std::mutex> lock(lockMutex);
  29. quit = true;
  30. lock.unlock();
  31. condition.notify_all();
  32. // Wait for threads to finish before we exit
  33. for(size_t i = 0; i < threads.size(); i++)
  34. {
  35. if(threads[i].joinable())
  36. {
  37. threads[i].join();
  38. }
  39. }
  40. }
  41. void DispatchQueue::removePending() {
  42. std::unique_lock<std::mutex> lock(lockMutex);
  43. queue = {};
  44. }
  45. void DispatchQueue::dispatch(const fp_t& op) {
  46. std::unique_lock<std::mutex> lock(lockMutex);
  47. queue.push(op);
  48. // Manual unlocking is done before notifying, to avoid waking up
  49. // the waiting thread only to block again (see notify_one for details)
  50. lock.unlock();
  51. condition.notify_one();
  52. }
  53. void DispatchQueue::dispatch(fp_t&& op) {
  54. std::unique_lock<std::mutex> lock(lockMutex);
  55. queue.push(std::move(op));
  56. // Manual unlocking is done before notifying, to avoid waking up
  57. // the waiting thread only to block again (see notify_one for details)
  58. lock.unlock();
  59. condition.notify_one();
  60. }
  61. void DispatchQueue::dispatchThreadHandler(void) {
  62. std::unique_lock<std::mutex> lock(lockMutex);
  63. do {
  64. //Wait until we have data or a quit signal
  65. condition.wait(lock, [this]{
  66. return (queue.size() || quit);
  67. });
  68. //after wait, we own the lock
  69. if(!quit && queue.size())
  70. {
  71. auto op = std::move(queue.front());
  72. queue.pop();
  73. //unlock now that we're done messing with the queue
  74. lock.unlock();
  75. op();
  76. lock.lock();
  77. }
  78. } while (!quit);
  79. }