future.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //===------------------------- future.cpp ---------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "__config"
  10. #ifndef _LIBCPP_HAS_NO_THREADS
  11. #include "future"
  12. #include "string"
  13. _LIBCPP_BEGIN_NAMESPACE_STD
  14. class _LIBCPP_HIDDEN __future_error_category
  15. : public __do_message
  16. {
  17. public:
  18. virtual const char* name() const _NOEXCEPT;
  19. virtual string message(int ev) const;
  20. };
  21. const char*
  22. __future_error_category::name() const _NOEXCEPT
  23. {
  24. return "future";
  25. }
  26. #if defined(__clang__)
  27. #pragma clang diagnostic push
  28. #pragma clang diagnostic ignored "-Wswitch"
  29. #elif defined(__GNUC__) || defined(__GNUG__)
  30. #pragma GCC diagnostic push
  31. #pragma GCC diagnostic ignored "-Wswitch"
  32. #endif
  33. string
  34. __future_error_category::message(int ev) const
  35. {
  36. switch (static_cast<future_errc>(ev))
  37. {
  38. case future_errc(0): // For backwards compatibility with C++11 (LWG 2056)
  39. case future_errc::broken_promise:
  40. return string("The associated promise has been destructed prior "
  41. "to the associated state becoming ready.");
  42. case future_errc::future_already_retrieved:
  43. return string("The future has already been retrieved from "
  44. "the promise or packaged_task.");
  45. case future_errc::promise_already_satisfied:
  46. return string("The state of the promise has already been set.");
  47. case future_errc::no_state:
  48. return string("Operation not permitted on an object without "
  49. "an associated state.");
  50. }
  51. return string("unspecified future_errc value\n");
  52. }
  53. #if defined(__clang__)
  54. #pragma clang diagnostic pop
  55. #elif defined(__GNUC__) || defined(__GNUG__)
  56. #pragma GCC diagnostic pop
  57. #endif
  58. const error_category&
  59. future_category() _NOEXCEPT
  60. {
  61. static __future_error_category __f;
  62. return __f;
  63. }
  64. future_error::future_error(error_code __ec)
  65. : logic_error(__ec.message()),
  66. __ec_(__ec)
  67. {
  68. }
  69. future_error::~future_error() _NOEXCEPT
  70. {
  71. }
  72. void
  73. __assoc_sub_state::__on_zero_shared() _NOEXCEPT
  74. {
  75. delete this;
  76. }
  77. void
  78. __assoc_sub_state::set_value()
  79. {
  80. unique_lock<mutex> __lk(__mut_);
  81. #ifndef _LIBCPP_NO_EXCEPTIONS
  82. if (__has_value())
  83. throw future_error(make_error_code(future_errc::promise_already_satisfied));
  84. #endif
  85. __state_ |= __constructed | ready;
  86. __cv_.notify_all();
  87. }
  88. void
  89. __assoc_sub_state::set_value_at_thread_exit()
  90. {
  91. unique_lock<mutex> __lk(__mut_);
  92. #ifndef _LIBCPP_NO_EXCEPTIONS
  93. if (__has_value())
  94. throw future_error(make_error_code(future_errc::promise_already_satisfied));
  95. #endif
  96. __state_ |= __constructed;
  97. __thread_local_data()->__make_ready_at_thread_exit(this);
  98. }
  99. void
  100. __assoc_sub_state::set_exception(exception_ptr __p)
  101. {
  102. unique_lock<mutex> __lk(__mut_);
  103. #ifndef _LIBCPP_NO_EXCEPTIONS
  104. if (__has_value())
  105. throw future_error(make_error_code(future_errc::promise_already_satisfied));
  106. #endif
  107. __exception_ = __p;
  108. __state_ |= ready;
  109. __cv_.notify_all();
  110. }
  111. void
  112. __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
  113. {
  114. unique_lock<mutex> __lk(__mut_);
  115. #ifndef _LIBCPP_NO_EXCEPTIONS
  116. if (__has_value())
  117. throw future_error(make_error_code(future_errc::promise_already_satisfied));
  118. #endif
  119. __exception_ = __p;
  120. __thread_local_data()->__make_ready_at_thread_exit(this);
  121. }
  122. void
  123. __assoc_sub_state::__make_ready()
  124. {
  125. unique_lock<mutex> __lk(__mut_);
  126. __state_ |= ready;
  127. __cv_.notify_all();
  128. }
  129. void
  130. __assoc_sub_state::copy()
  131. {
  132. unique_lock<mutex> __lk(__mut_);
  133. __sub_wait(__lk);
  134. if (__exception_ != nullptr)
  135. rethrow_exception(__exception_);
  136. }
  137. void
  138. __assoc_sub_state::wait()
  139. {
  140. unique_lock<mutex> __lk(__mut_);
  141. __sub_wait(__lk);
  142. }
  143. void
  144. __assoc_sub_state::__sub_wait(unique_lock<mutex>& __lk)
  145. {
  146. if (!__is_ready())
  147. {
  148. if (__state_ & static_cast<unsigned>(deferred))
  149. {
  150. __state_ &= ~static_cast<unsigned>(deferred);
  151. __lk.unlock();
  152. __execute();
  153. }
  154. else
  155. while (!__is_ready())
  156. __cv_.wait(__lk);
  157. }
  158. }
  159. void
  160. __assoc_sub_state::__execute()
  161. {
  162. #ifndef _LIBCPP_NO_EXCEPTIONS
  163. throw future_error(make_error_code(future_errc::no_state));
  164. #endif
  165. }
  166. future<void>::future(__assoc_sub_state* __state)
  167. : __state_(__state)
  168. {
  169. #ifndef _LIBCPP_NO_EXCEPTIONS
  170. if (__state_->__has_future_attached())
  171. throw future_error(make_error_code(future_errc::future_already_retrieved));
  172. #endif
  173. __state_->__add_shared();
  174. __state_->__set_future_attached();
  175. }
  176. future<void>::~future()
  177. {
  178. if (__state_)
  179. __state_->__release_shared();
  180. }
  181. void
  182. future<void>::get()
  183. {
  184. unique_ptr<__shared_count, __release_shared_count> __(__state_);
  185. __assoc_sub_state* __s = __state_;
  186. __state_ = nullptr;
  187. __s->copy();
  188. }
  189. promise<void>::promise()
  190. : __state_(new __assoc_sub_state)
  191. {
  192. }
  193. promise<void>::~promise()
  194. {
  195. if (__state_)
  196. {
  197. #ifndef _LIBCPP_NO_EXCEPTIONS
  198. if (!__state_->__has_value() && __state_->use_count() > 1)
  199. __state_->set_exception(make_exception_ptr(
  200. future_error(make_error_code(future_errc::broken_promise))
  201. ));
  202. #endif // _LIBCPP_NO_EXCEPTIONS
  203. __state_->__release_shared();
  204. }
  205. }
  206. future<void>
  207. promise<void>::get_future()
  208. {
  209. #ifndef _LIBCPP_NO_EXCEPTIONS
  210. if (__state_ == nullptr)
  211. throw future_error(make_error_code(future_errc::no_state));
  212. #endif
  213. return future<void>(__state_);
  214. }
  215. void
  216. promise<void>::set_value()
  217. {
  218. #ifndef _LIBCPP_NO_EXCEPTIONS
  219. if (__state_ == nullptr)
  220. throw future_error(make_error_code(future_errc::no_state));
  221. #endif
  222. __state_->set_value();
  223. }
  224. void
  225. promise<void>::set_exception(exception_ptr __p)
  226. {
  227. #ifndef _LIBCPP_NO_EXCEPTIONS
  228. if (__state_ == nullptr)
  229. throw future_error(make_error_code(future_errc::no_state));
  230. #endif
  231. __state_->set_exception(__p);
  232. }
  233. void
  234. promise<void>::set_value_at_thread_exit()
  235. {
  236. #ifndef _LIBCPP_NO_EXCEPTIONS
  237. if (__state_ == nullptr)
  238. throw future_error(make_error_code(future_errc::no_state));
  239. #endif
  240. __state_->set_value_at_thread_exit();
  241. }
  242. void
  243. promise<void>::set_exception_at_thread_exit(exception_ptr __p)
  244. {
  245. #ifndef _LIBCPP_NO_EXCEPTIONS
  246. if (__state_ == nullptr)
  247. throw future_error(make_error_code(future_errc::no_state));
  248. #endif
  249. __state_->set_exception_at_thread_exit(__p);
  250. }
  251. shared_future<void>::~shared_future()
  252. {
  253. if (__state_)
  254. __state_->__release_shared();
  255. }
  256. shared_future<void>&
  257. shared_future<void>::operator=(const shared_future& __rhs)
  258. {
  259. if (__rhs.__state_)
  260. __rhs.__state_->__add_shared();
  261. if (__state_)
  262. __state_->__release_shared();
  263. __state_ = __rhs.__state_;
  264. return *this;
  265. }
  266. _LIBCPP_END_NAMESPACE_STD
  267. #endif // !_LIBCPP_HAS_NO_THREADS