Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AsioService.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/platforms/asio/AsioTimer.hpp>
  21. #include <ableton/platforms/asio/AsioWrapper.hpp>
  22. #include <thread>
  23. namespace ableton
  24. {
  25. namespace platforms
  26. {
  27. namespace asio
  28. {
  29. class AsioService
  30. {
  31. public:
  32. using Timer = AsioTimer;
  33. AsioService()
  34. : AsioService(DefaultHandler{})
  35. {
  36. }
  37. template <typename ExceptionHandler>
  38. explicit AsioService(ExceptionHandler exceptHandler)
  39. : mpWork(new ::asio::io_service::work(mService))
  40. {
  41. mThread =
  42. std::thread{[](::asio::io_service& service, ExceptionHandler handler) {
  43. for (;;)
  44. {
  45. try
  46. {
  47. service.run();
  48. break;
  49. }
  50. catch (const typename ExceptionHandler::Exception& exception)
  51. {
  52. handler(exception);
  53. }
  54. }
  55. },
  56. std::ref(mService), std::move(exceptHandler)};
  57. }
  58. ~AsioService()
  59. {
  60. mpWork.reset();
  61. mThread.join();
  62. }
  63. AsioTimer makeTimer()
  64. {
  65. return {mService};
  66. }
  67. template <typename Handler>
  68. void post(Handler handler)
  69. {
  70. mService.post(std::move(handler));
  71. }
  72. ::asio::io_service mService;
  73. private:
  74. // Default handler is hidden and defines a hidden exception type
  75. // that will never be thrown by other code, so it effectively does
  76. // not catch.
  77. struct DefaultHandler
  78. {
  79. struct Exception
  80. {
  81. };
  82. void operator()(const Exception&)
  83. {
  84. }
  85. };
  86. std::unique_ptr<::asio::io_service::work> mpWork;
  87. std::thread mThread;
  88. };
  89. } // namespace asio
  90. } // namespace platforms
  91. } // namespace ableton