future.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Copyright Oliver Kowalke 2013.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_FIBERS_FUTURE_HPP
  6. #define BOOST_FIBERS_FUTURE_HPP
  7. #include <algorithm>
  8. #include <chrono>
  9. #include <exception>
  10. #include <utility>
  11. #include <boost/config.hpp>
  12. #include <boost/fiber/detail/config.hpp>
  13. #include <boost/fiber/exceptions.hpp>
  14. #include <boost/fiber/future/detail/shared_state.hpp>
  15. #include <boost/fiber/future/future_status.hpp>
  16. namespace boost {
  17. namespace fibers {
  18. namespace detail {
  19. template< typename R >
  20. struct future_base {
  21. typedef typename shared_state< R >::ptr_type ptr_type;
  22. ptr_type state_{};
  23. future_base() = default;
  24. explicit future_base( ptr_type p) noexcept :
  25. state_{std::move( p )} {
  26. }
  27. ~future_base() = default;
  28. future_base( future_base const& other) :
  29. state_{ other.state_ } {
  30. }
  31. future_base( future_base && other) noexcept :
  32. state_{ other.state_ } {
  33. other.state_.reset();
  34. }
  35. future_base & operator=( future_base const& other) noexcept {
  36. if ( BOOST_LIKELY( this != & other) ) {
  37. state_ = other.state_;
  38. }
  39. return * this;
  40. }
  41. future_base & operator=( future_base && other) noexcept {
  42. if ( BOOST_LIKELY( this != & other) ) {
  43. state_ = other.state_;
  44. other.state_.reset();
  45. }
  46. return * this;
  47. }
  48. bool valid() const noexcept {
  49. return nullptr != state_.get();
  50. }
  51. std::exception_ptr get_exception_ptr() {
  52. if ( BOOST_UNLIKELY( ! valid() ) ) {
  53. throw future_uninitialized{};
  54. }
  55. return state_->get_exception_ptr();
  56. }
  57. void wait() const {
  58. if ( BOOST_UNLIKELY( ! valid() ) ) {
  59. throw future_uninitialized{};
  60. }
  61. state_->wait();
  62. }
  63. template< typename Rep, typename Period >
  64. future_status wait_for( std::chrono::duration< Rep, Period > const& timeout_duration) const {
  65. if ( BOOST_UNLIKELY( ! valid() ) ) {
  66. throw future_uninitialized{};
  67. }
  68. return state_->wait_for( timeout_duration);
  69. }
  70. template< typename Clock, typename Duration >
  71. future_status wait_until( std::chrono::time_point< Clock, Duration > const& timeout_time) const {
  72. if ( BOOST_UNLIKELY( ! valid() ) ) {
  73. throw future_uninitialized{};
  74. }
  75. return state_->wait_until( timeout_time);
  76. }
  77. };
  78. template< typename R >
  79. struct promise_base;
  80. }
  81. template< typename R >
  82. class shared_future;
  83. template< typename Signature >
  84. class packaged_task;
  85. template< typename R >
  86. class future : private detail::future_base< R > {
  87. private:
  88. typedef detail::future_base< R > base_type;
  89. friend struct detail::promise_base< R >;
  90. friend class shared_future< R >;
  91. template< typename Signature >
  92. friend class packaged_task;
  93. explicit future( typename base_type::ptr_type const& p) noexcept :
  94. base_type{ p } {
  95. }
  96. public:
  97. future() = default;
  98. future( future const&) = delete;
  99. future & operator=( future const&) = delete;
  100. future( future && other) noexcept :
  101. base_type{ std::move( other) } {
  102. }
  103. future & operator=( future && other) noexcept {
  104. if ( BOOST_LIKELY( this != & other) ) {
  105. base_type::operator=( std::move( other) );
  106. }
  107. return * this;
  108. }
  109. shared_future< R > share();
  110. R get() {
  111. if ( BOOST_UNLIKELY( ! base_type::valid() ) ) {
  112. throw future_uninitialized{};
  113. }
  114. typename base_type::ptr_type tmp{};
  115. tmp.swap( base_type::state_);
  116. return std::move( tmp->get() );
  117. }
  118. using base_type::valid;
  119. using base_type::get_exception_ptr;
  120. using base_type::wait;
  121. using base_type::wait_for;
  122. using base_type::wait_until;
  123. };
  124. template< typename R >
  125. class future< R & > : private detail::future_base< R & > {
  126. private:
  127. typedef detail::future_base< R & > base_type;
  128. friend struct detail::promise_base< R & >;
  129. friend class shared_future< R & >;
  130. template< typename Signature >
  131. friend class packaged_task;
  132. explicit future( typename base_type::ptr_type const& p) noexcept :
  133. base_type{ p } {
  134. }
  135. public:
  136. future() = default;
  137. future( future const&) = delete;
  138. future & operator=( future const&) = delete;
  139. future( future && other) noexcept :
  140. base_type{ std::move( other) } {
  141. }
  142. future & operator=( future && other) noexcept {
  143. if ( BOOST_LIKELY( this != & other) ) {
  144. base_type::operator=( std::move( other) );
  145. }
  146. return * this;
  147. }
  148. shared_future< R & > share();
  149. R & get() {
  150. if ( BOOST_UNLIKELY( ! base_type::valid() ) ) {
  151. throw future_uninitialized{};
  152. }
  153. typename base_type::ptr_type tmp{};
  154. tmp.swap( base_type::state_);
  155. return tmp->get();
  156. }
  157. using base_type::valid;
  158. using base_type::get_exception_ptr;
  159. using base_type::wait;
  160. using base_type::wait_for;
  161. using base_type::wait_until;
  162. };
  163. template<>
  164. class future< void > : private detail::future_base< void > {
  165. private:
  166. typedef detail::future_base< void > base_type;
  167. friend struct detail::promise_base< void >;
  168. friend class shared_future< void >;
  169. template< typename Signature >
  170. friend class packaged_task;
  171. explicit future( base_type::ptr_type const& p) noexcept :
  172. base_type{ p } {
  173. }
  174. public:
  175. future() = default;
  176. future( future const&) = delete;
  177. future & operator=( future const&) = delete;
  178. inline
  179. future( future && other) noexcept :
  180. base_type{ std::move( other) } {
  181. }
  182. inline
  183. future & operator=( future && other) noexcept {
  184. if ( BOOST_LIKELY( this != & other) ) {
  185. base_type::operator=( std::move( other) );
  186. }
  187. return * this;
  188. }
  189. shared_future< void > share();
  190. inline
  191. void get() {
  192. if ( BOOST_UNLIKELY( ! base_type::valid() ) ) {
  193. throw future_uninitialized{};
  194. }
  195. base_type::ptr_type tmp{};
  196. tmp.swap( base_type::state_);
  197. tmp->get();
  198. }
  199. using base_type::valid;
  200. using base_type::get_exception_ptr;
  201. using base_type::wait;
  202. using base_type::wait_for;
  203. using base_type::wait_until;
  204. };
  205. template< typename R >
  206. class shared_future : private detail::future_base< R > {
  207. private:
  208. typedef detail::future_base< R > base_type;
  209. explicit shared_future( typename base_type::ptr_type const& p) noexcept :
  210. base_type{ p } {
  211. }
  212. public:
  213. shared_future() = default;
  214. ~shared_future() = default;
  215. shared_future( shared_future const& other) :
  216. base_type{ other } {
  217. }
  218. shared_future( shared_future && other) noexcept :
  219. base_type{ std::move( other) } {
  220. }
  221. shared_future( future< R > && other) noexcept :
  222. base_type{ std::move( other) } {
  223. }
  224. shared_future & operator=( shared_future const& other) noexcept {
  225. if ( BOOST_LIKELY( this != & other) ) {
  226. base_type::operator=( other);
  227. }
  228. return * this;
  229. }
  230. shared_future & operator=( shared_future && other) noexcept {
  231. if ( BOOST_LIKELY( this != & other) ) {
  232. base_type::operator=( std::move( other) );
  233. }
  234. return * this;
  235. }
  236. shared_future & operator=( future< R > && other) noexcept {
  237. base_type::operator=( std::move( other) );
  238. return * this;
  239. }
  240. R const& get() const {
  241. if ( BOOST_UNLIKELY( ! valid() ) ) {
  242. throw future_uninitialized{};
  243. }
  244. return base_type::state_->get();
  245. }
  246. using base_type::valid;
  247. using base_type::get_exception_ptr;
  248. using base_type::wait;
  249. using base_type::wait_for;
  250. using base_type::wait_until;
  251. };
  252. template< typename R >
  253. class shared_future< R & > : private detail::future_base< R & > {
  254. private:
  255. typedef detail::future_base< R & > base_type;
  256. explicit shared_future( typename base_type::ptr_type const& p) noexcept :
  257. base_type{ p } {
  258. }
  259. public:
  260. shared_future() = default;
  261. ~shared_future() = default;
  262. shared_future( shared_future const& other) :
  263. base_type{ other } {
  264. }
  265. shared_future( shared_future && other) noexcept :
  266. base_type{ std::move( other) } {
  267. }
  268. shared_future( future< R & > && other) noexcept :
  269. base_type{ std::move( other) } {
  270. }
  271. shared_future & operator=( shared_future const& other) noexcept {
  272. if ( BOOST_LIKELY( this != & other) ) {
  273. base_type::operator=( other);
  274. }
  275. return * this;
  276. }
  277. shared_future & operator=( shared_future && other) noexcept {
  278. if ( BOOST_LIKELY( this != & other) ) {
  279. base_type::operator=( std::move( other) );
  280. }
  281. return * this;
  282. }
  283. shared_future & operator=( future< R & > && other) noexcept {
  284. base_type::operator=( std::move( other) );
  285. return * this;
  286. }
  287. R & get() const {
  288. if ( BOOST_UNLIKELY( ! valid() ) ) {
  289. throw future_uninitialized{};
  290. }
  291. return base_type::state_->get();
  292. }
  293. using base_type::valid;
  294. using base_type::get_exception_ptr;
  295. using base_type::wait;
  296. using base_type::wait_for;
  297. using base_type::wait_until;
  298. };
  299. template<>
  300. class shared_future< void > : private detail::future_base< void > {
  301. private:
  302. typedef detail::future_base< void > base_type;
  303. explicit shared_future( base_type::ptr_type const& p) noexcept :
  304. base_type{ p } {
  305. }
  306. public:
  307. shared_future() = default;
  308. ~shared_future() = default;
  309. inline
  310. shared_future( shared_future const& other) :
  311. base_type{ other } {
  312. }
  313. inline
  314. shared_future( shared_future && other) noexcept :
  315. base_type{ std::move( other) } {
  316. }
  317. inline
  318. shared_future( future< void > && other) noexcept :
  319. base_type{ std::move( other) } {
  320. }
  321. inline
  322. shared_future & operator=( shared_future const& other) noexcept {
  323. if ( BOOST_LIKELY( this != & other) ) {
  324. base_type::operator=( other);
  325. }
  326. return * this;
  327. }
  328. inline
  329. shared_future & operator=( shared_future && other) noexcept {
  330. if ( BOOST_LIKELY( this != & other) ) {
  331. base_type::operator=( std::move( other) );
  332. }
  333. return * this;
  334. }
  335. inline
  336. shared_future & operator=( future< void > && other) noexcept {
  337. base_type::operator=( std::move( other) );
  338. return * this;
  339. }
  340. inline
  341. void get() const {
  342. if ( BOOST_UNLIKELY( ! valid() ) ) {
  343. throw future_uninitialized{};
  344. }
  345. base_type::state_->get();
  346. }
  347. using base_type::valid;
  348. using base_type::get_exception_ptr;
  349. using base_type::wait;
  350. using base_type::wait_for;
  351. using base_type::wait_until;
  352. };
  353. template< typename R >
  354. shared_future< R >
  355. future< R >::share() {
  356. if ( BOOST_UNLIKELY( ! base_type::valid() ) ) {
  357. throw future_uninitialized{};
  358. }
  359. return shared_future< R >{ std::move( * this) };
  360. }
  361. template< typename R >
  362. shared_future< R & >
  363. future< R & >::share() {
  364. if ( BOOST_UNLIKELY( ! base_type::valid() ) ) {
  365. throw future_uninitialized{};
  366. }
  367. return shared_future< R & >{ std::move( * this) };
  368. }
  369. inline
  370. shared_future< void >
  371. future< void >::share() {
  372. if ( BOOST_UNLIKELY( ! base_type::valid() ) ) {
  373. throw future_uninitialized{};
  374. }
  375. return shared_future< void >{ std::move( * this) };
  376. }
  377. }}
  378. #endif