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.

81 lines
1.7KB

  1. //
  2. // detail/posix_global.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_DETAIL_POSIX_GLOBAL_HPP
  11. #define ASIO_DETAIL_POSIX_GLOBAL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_PTHREADS)
  17. #include <exception>
  18. #include <pthread.h>
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace detail {
  22. template <typename T>
  23. struct posix_global_impl
  24. {
  25. // Helper function to perform initialisation.
  26. static void do_init()
  27. {
  28. instance_.static_ptr_ = instance_.ptr_ = new T;
  29. }
  30. // Destructor automatically cleans up the global.
  31. ~posix_global_impl()
  32. {
  33. delete static_ptr_;
  34. }
  35. static ::pthread_once_t init_once_;
  36. static T* static_ptr_;
  37. static posix_global_impl instance_;
  38. T* ptr_;
  39. };
  40. template <typename T>
  41. ::pthread_once_t posix_global_impl<T>::init_once_ = PTHREAD_ONCE_INIT;
  42. template <typename T>
  43. T* posix_global_impl<T>::static_ptr_ = 0;
  44. template <typename T>
  45. posix_global_impl<T> posix_global_impl<T>::instance_;
  46. template <typename T>
  47. T& posix_global()
  48. {
  49. int result = ::pthread_once(
  50. &posix_global_impl<T>::init_once_,
  51. &posix_global_impl<T>::do_init);
  52. if (result != 0)
  53. std::terminate();
  54. return *posix_global_impl<T>::instance_.ptr_;
  55. }
  56. } // namespace detail
  57. } // namespace asio
  58. #include "asio/detail/pop_options.hpp"
  59. #endif // defined(ASIO_HAS_PTHREADS)
  60. #endif // ASIO_DETAIL_POSIX_GLOBAL_HPP