[a95e75e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_BITS_THREAD_SHARED_FUTURE
|
---|
| 30 | #define LIBCPP_BITS_THREAD_SHARED_FUTURE
|
---|
| 31 |
|
---|
[a552044] | 32 | #include <__bits/thread/future.hpp>
|
---|
[a6c3bf3] | 33 | #include <__bits/thread/future_common.hpp>
|
---|
| 34 | #include <type_traits>
|
---|
[a552044] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Note: We do synchronization directly on the shared
|
---|
| 38 | * state, this means that with the exception of
|
---|
| 39 | * some member functions (which are only defined
|
---|
| 40 | * for specializations anyway) our future and
|
---|
| 41 | * shared_future are basically identical. Because
|
---|
| 42 | * of that, we can use aux::future_base for
|
---|
| 43 | * shared_future as well.
|
---|
| 44 | */
|
---|
| 45 |
|
---|
[a95e75e] | 46 | namespace std
|
---|
| 47 | {
|
---|
| 48 | /**
|
---|
| 49 | * 30.6.7, class template shared_future:
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | template<class R>
|
---|
[a6c3bf3] | 53 | class shared_future: public aux::future_base<aux::future_inner_t<R>>
|
---|
[a95e75e] | 54 | {
|
---|
[a552044] | 55 | public:
|
---|
| 56 | shared_future() noexcept = default;
|
---|
| 57 |
|
---|
| 58 | shared_future(const shared_future&) = default;
|
---|
| 59 |
|
---|
| 60 | shared_future(shared_future&&) noexcept = default;
|
---|
| 61 |
|
---|
| 62 | shared_future(future<R>&& rhs)
|
---|
[a6c3bf3] | 63 | : aux::future_base<aux::future_inner_t<R>>{move(rhs.state_)}
|
---|
[a552044] | 64 | {
|
---|
| 65 | rhs.state_ = nullptr;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | shared_future& operator=(const shared_future&) = default;
|
---|
| 69 |
|
---|
| 70 | shared_future& operator=(shared_future&&) noexcept = default;
|
---|
| 71 |
|
---|
[1621f91] | 72 | aux::future_return_shared_t<R> get() const
|
---|
[a552044] | 73 | {
|
---|
| 74 | assert(this->state_);
|
---|
| 75 |
|
---|
| 76 | this->wait();
|
---|
| 77 |
|
---|
| 78 | if (this->state_->has_exception())
|
---|
| 79 | this->state_->throw_stored_exception();
|
---|
| 80 |
|
---|
[a6c3bf3] | 81 | /**
|
---|
| 82 | * Using constexpr if and the future_inner and future_result
|
---|
| 83 | * metafunctions we can actually avoid having to create specializations
|
---|
| 84 | * for R& and void in this case.
|
---|
| 85 | */
|
---|
| 86 | if constexpr (!is_same_v<R, void>)
|
---|
| 87 | {
|
---|
| 88 | if constexpr (is_reference_v<R>)
|
---|
| 89 | {
|
---|
| 90 | assert(this->state_->get());
|
---|
| 91 |
|
---|
| 92 | return *this->state_->get();
|
---|
| 93 | }
|
---|
| 94 | else
|
---|
| 95 | return this->state_->get();
|
---|
| 96 | }
|
---|
[a552044] | 97 | }
|
---|
[8e24583] | 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Useful for testing as we can check some information
|
---|
| 101 | * otherwise unavailable to us without waiting, e.g.
|
---|
| 102 | * to check whether the state is ready, its reference
|
---|
| 103 | * count etc.
|
---|
| 104 | */
|
---|
| 105 | aux::shared_state<aux::future_inner_t<R>>* __state() noexcept
|
---|
| 106 | {
|
---|
| 107 | return this->state_;
|
---|
| 108 | }
|
---|
[a95e75e] | 109 | };
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | #endif
|
---|