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.

72 lines
1.7KB

  1. //
  2. // detail/win_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_WIN_GLOBAL_HPP
  11. #define ASIO_DETAIL_WIN_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. #include "asio/detail/static_mutex.hpp"
  17. #include "asio/detail/tss_ptr.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace detail {
  21. template <typename T>
  22. struct win_global_impl
  23. {
  24. // Destructor automatically cleans up the global.
  25. ~win_global_impl()
  26. {
  27. delete ptr_;
  28. }
  29. static win_global_impl instance_;
  30. static static_mutex mutex_;
  31. T* ptr_;
  32. static tss_ptr<T> tss_ptr_;
  33. };
  34. template <typename T>
  35. win_global_impl<T> win_global_impl<T>::instance_ = { 0 };
  36. template <typename T>
  37. static_mutex win_global_impl<T>::mutex_ = ASIO_STATIC_MUTEX_INIT;
  38. template <typename T>
  39. tss_ptr<T> win_global_impl<T>::tss_ptr_;
  40. template <typename T>
  41. T& win_global()
  42. {
  43. if (static_cast<T*>(win_global_impl<T>::tss_ptr_) == 0)
  44. {
  45. win_global_impl<T>::mutex_.init();
  46. static_mutex::scoped_lock lock(win_global_impl<T>::mutex_);
  47. if (win_global_impl<T>::instance_.ptr_ == 0)
  48. win_global_impl<T>::instance_.ptr_ = new T;
  49. win_global_impl<T>::tss_ptr_ = win_global_impl<T>::instance_.ptr_;
  50. }
  51. return *win_global_impl<T>::tss_ptr_;
  52. }
  53. } // namespace detail
  54. } // namespace asio
  55. #include "asio/detail/pop_options.hpp"
  56. #endif // ASIO_DETAIL_WIN_GLOBAL_HPP