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.

127 lines
2.6KB

  1. //
  2. // detail/posix_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_EVENT_HPP
  11. #define ASIO_DETAIL_POSIX_EVENT_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 <pthread.h>
  18. #include "asio/detail/assert.hpp"
  19. #include "asio/detail/noncopyable.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. class posix_event
  24. : private noncopyable
  25. {
  26. public:
  27. // Constructor.
  28. ASIO_DECL posix_event();
  29. // Destructor.
  30. ~posix_event()
  31. {
  32. ::pthread_cond_destroy(&cond_);
  33. }
  34. // Signal the event. (Retained for backward compatibility.)
  35. template <typename Lock>
  36. void signal(Lock& lock)
  37. {
  38. this->signal_all(lock);
  39. }
  40. // Signal all waiters.
  41. template <typename Lock>
  42. void signal_all(Lock& lock)
  43. {
  44. ASIO_ASSERT(lock.locked());
  45. (void)lock;
  46. state_ |= 1;
  47. ::pthread_cond_broadcast(&cond_); // Ignore EINVAL.
  48. }
  49. // Unlock the mutex and signal one waiter.
  50. template <typename Lock>
  51. void unlock_and_signal_one(Lock& lock)
  52. {
  53. ASIO_ASSERT(lock.locked());
  54. state_ |= 1;
  55. bool have_waiters = (state_ > 1);
  56. lock.unlock();
  57. if (have_waiters)
  58. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  59. }
  60. // If there's a waiter, unlock the mutex and signal it.
  61. template <typename Lock>
  62. bool maybe_unlock_and_signal_one(Lock& lock)
  63. {
  64. ASIO_ASSERT(lock.locked());
  65. state_ |= 1;
  66. if (state_ > 1)
  67. {
  68. lock.unlock();
  69. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  70. return true;
  71. }
  72. return false;
  73. }
  74. // Reset the event.
  75. template <typename Lock>
  76. void clear(Lock& lock)
  77. {
  78. ASIO_ASSERT(lock.locked());
  79. (void)lock;
  80. state_ &= ~std::size_t(1);
  81. }
  82. // Wait for the event to become signalled.
  83. template <typename Lock>
  84. void wait(Lock& lock)
  85. {
  86. ASIO_ASSERT(lock.locked());
  87. while ((state_ & 1) == 0)
  88. {
  89. state_ += 2;
  90. ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
  91. state_ -= 2;
  92. }
  93. }
  94. private:
  95. ::pthread_cond_t cond_;
  96. std::size_t state_;
  97. };
  98. } // namespace detail
  99. } // namespace asio
  100. #include "asio/detail/pop_options.hpp"
  101. #if defined(ASIO_HEADER_ONLY)
  102. # include "asio/detail/impl/posix_event.ipp"
  103. #endif // defined(ASIO_HEADER_ONLY)
  104. #endif // defined(ASIO_HAS_PTHREADS)
  105. #endif // ASIO_DETAIL_POSIX_EVENT_HPP