sgx_condition_variable 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // -*- C++ -*-
  2. //===---------------------- condition_variable ----------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_SGX_CONDITION_VARIABLE
  11. #define _LIBCPP_SGX_CONDITION_VARIABLE
  12. /*
  13. condition_variable synopsis
  14. namespace std
  15. {
  16. enum class cv_status { no_timeout, timeout };
  17. class condition_variable
  18. {
  19. public:
  20. condition_variable();
  21. ~condition_variable();
  22. condition_variable(const condition_variable&) = delete;
  23. condition_variable& operator=(const condition_variable&) = delete;
  24. void notify_one() noexcept;
  25. void notify_all() noexcept;
  26. void wait(unique_lock<mutex>& lock);
  27. template <class Predicate>
  28. void wait(unique_lock<mutex>& lock, Predicate pred);
  29. Not supported (time API) : template <class Clock, class Duration>
  30. cv_status
  31. wait_until(unique_lock<mutex>& lock,
  32. const chrono::time_point<Clock, Duration>& abs_time);
  33. Not supported (time API) : template <class Clock, class Duration, class Predicate>
  34. bool
  35. wait_until(unique_lock<mutex>& lock,
  36. const chrono::time_point<Clock, Duration>& abs_time,
  37. Predicate pred);
  38. Not supported (time API) : template <class Rep, class Period>
  39. cv_status
  40. wait_for(unique_lock<mutex>& lock,
  41. const chrono::duration<Rep, Period>& rel_time);
  42. Not supported (time API) : template <class Rep, class Period, class Predicate>
  43. bool
  44. wait_for(unique_lock<mutex>& lock,
  45. const chrono::duration<Rep, Period>& rel_time,
  46. Predicate pred);
  47. typedef sgx_thread_cond_t* native_handle_type;
  48. native_handle_type native_handle();
  49. };
  50. void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
  51. class condition_variable_any
  52. {
  53. public:
  54. condition_variable_any();
  55. ~condition_variable_any();
  56. condition_variable_any(const condition_variable_any&) = delete;
  57. condition_variable_any& operator=(const condition_variable_any&) = delete;
  58. void notify_one() noexcept;
  59. void notify_all() noexcept;
  60. template <class Lock>
  61. void wait(Lock& lock);
  62. template <class Lock, class Predicate>
  63. void wait(Lock& lock, Predicate pred);
  64. Not supported (time API) : template <class Lock, class Clock, class Duration>
  65. cv_status
  66. wait_until(Lock& lock,
  67. const chrono::time_point<Clock, Duration>& abs_time);
  68. Not supported (time API) : template <class Lock, class Clock, class Duration, class Predicate>
  69. bool
  70. wait_until(Lock& lock,
  71. const chrono::time_point<Clock, Duration>& abs_time,
  72. Predicate pred);
  73. Not supported (time API) : template <class Lock, class Rep, class Period>
  74. cv_status
  75. wait_for(Lock& lock,
  76. const chrono::duration<Rep, Period>& rel_time);
  77. Not supported (time API) : template <class Lock, class Rep, class Period, class Predicate>
  78. bool
  79. wait_for(Lock& lock,
  80. const chrono::duration<Rep, Period>& rel_time,
  81. Predicate pred);
  82. };
  83. } // std
  84. */
  85. #include <__config>
  86. #include <sgx_thread.h>
  87. #include <support/sgx/__sgx_mutex_base>
  88. #include <memory>
  89. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  90. #pragma GCC system_header
  91. #endif
  92. _LIBCPP_BEGIN_NAMESPACE_STD
  93. class _LIBCPP_TYPE_VIS condition_variable_any
  94. {
  95. condition_variable __cv_;
  96. shared_ptr<mutex> __mut_;
  97. public:
  98. condition_variable_any();
  99. void notify_one() _NOEXCEPT;
  100. void notify_all() _NOEXCEPT;
  101. template <class _Lock>
  102. void wait(_Lock& __lock);
  103. template <class _Lock, class _Predicate>
  104. void wait(_Lock& __lock, _Predicate __pred);
  105. };
  106. inline _LIBCPP_INLINE_VISIBILITY
  107. condition_variable_any::condition_variable_any()
  108. : __mut_(make_shared<mutex>()) {}
  109. inline _LIBCPP_INLINE_VISIBILITY
  110. void
  111. condition_variable_any::notify_one() _NOEXCEPT
  112. {
  113. {lock_guard<mutex> __lx(*__mut_);}
  114. __cv_.notify_one();
  115. }
  116. inline _LIBCPP_INLINE_VISIBILITY
  117. void
  118. condition_variable_any::notify_all() _NOEXCEPT
  119. {
  120. {lock_guard<mutex> __lx(*__mut_);}
  121. __cv_.notify_all();
  122. }
  123. struct __lock_external
  124. {
  125. template <class _Lock>
  126. void operator()(_Lock* __m) {__m->lock();}
  127. };
  128. template <class _Lock>
  129. void
  130. condition_variable_any::wait(_Lock& __lock)
  131. {
  132. shared_ptr<mutex> __mut = __mut_;
  133. unique_lock<mutex> __lk(*__mut);
  134. __lock.unlock();
  135. unique_ptr<_Lock, __lock_external> __lxx(&__lock);
  136. lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
  137. __cv_.wait(__lk);
  138. } // __mut_.unlock(), __lock.lock()
  139. template <class _Lock, class _Predicate>
  140. inline _LIBCPP_INLINE_VISIBILITY
  141. void
  142. condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
  143. {
  144. while (!__pred())
  145. wait(__lock);
  146. }
  147. _LIBCPP_END_NAMESPACE_STD
  148. #endif // _LIBCPP_SGX_CONDITION_VARIABLE