Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset a552044 in mainline


Ignore:
Timestamp:
2019-07-01T15:10:08Z (16 months ago)
Author:
Jaroslav Jindrak <dzejrou@…>
Branches:
master
Children:
a6c3bf3
Parents:
0fc6b6c
Message:

cpp: added shared_future, but it might be possible to remove the specializations as the main template does not have two different getters this time

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/__bits/thread/shared_future.hpp

    r0fc6b6c ra552044  
    3030#define LIBCPP_BITS_THREAD_SHARED_FUTURE
    3131
     32#include <__bits/thread/future.hpp>
     33
     34/**
     35 * Note: We do synchronization directly on the shared
     36 *       state, this means that with the exception of
     37 *       some member functions (which are only defined
     38 *       for specializations anyway) our future and
     39 *       shared_future are basically identical. Because
     40 *       of that, we can use aux::future_base for
     41 *       shared_future as well.
     42 */
     43
    3244namespace std
    3345{
     
    3648     */
    3749
    38     // TODO: Make sure the move-future constructor of shared_future
    39     //       invalidates the state (i.e. sets to nullptr).
    4050    template<class R>
    41     class shared_future
     51    class shared_future: public aux::future_base<R>
    4252    {
    43         // TODO: Copy & modify once future is done.
     53        public:
     54            shared_future() noexcept = default;
     55
     56            shared_future(const shared_future&) = default;
     57
     58            shared_future(shared_future&&) noexcept = default;
     59
     60            shared_future(future<R>&& rhs)
     61                : aux::future_base<R>{move(rhs.state_)}
     62            {
     63                rhs.state_ = nullptr;
     64            }
     65
     66            shared_future& operator=(const shared_future&) = default;
     67
     68            shared_future& operator=(shared_future&&) noexcept = default;
     69
     70            const R& get() const
     71            {
     72                assert(this->state_);
     73
     74                this->wait();
     75
     76                if (this->state_->has_exception())
     77                    this->state_->throw_stored_exception();
     78
     79                return move(this->state_->get());
     80            }
    4481    };
    4582
    4683    template<class R>
    47     class shared_future<R&>
     84    class shared_future<R&>: public aux::future_base<R*>
    4885    {
    49         // TODO: Copy & modify once future is done.
     86        public:
     87            shared_future() noexcept = default;
     88
     89            shared_future(const shared_future&) = default;
     90
     91            shared_future(shared_future&&) noexcept = default;
     92
     93            shared_future(future<R&>&& rhs)
     94                : aux::future_base<R*>{move(rhs.state_)}
     95            {
     96                rhs.state_ = nullptr;
     97            }
     98
     99            shared_future& operator=(const shared_future&) = default;
     100
     101            shared_future& operator=(shared_future&&) noexcept = default;
     102
     103            R& get() const
     104            {
     105                assert(this->state_);
     106
     107                this->wait();
     108
     109                if (this->state_->has_exception())
     110                    this->state_->throw_stored_exception();
     111
     112                assert(this->state_->get());
     113                return *this->state_->get();
     114            }
    50115    };
    51116
    52117    template<>
    53     class shared_future<void>
     118    class shared_future<void>: public aux::future_base<void>
    54119    {
    55         // TODO: Copy & modify once future is done.
     120        public:
     121            shared_future() noexcept = default;
     122
     123            shared_future(const shared_future&) = default;
     124
     125            shared_future(shared_future&&) noexcept = default;
     126
     127            shared_future(future<void>&& rhs)
     128                : aux::future_base<void>{move(rhs.state_)}
     129            {
     130                rhs.state_ = nullptr;
     131            }
     132
     133            shared_future& operator=(const shared_future&) = default;
     134
     135            shared_future& operator=(shared_future&&) noexcept = default;
     136
     137            void get() const
     138            {
     139                assert(this->state_);
     140
     141                this->wait();
     142
     143                if (this->state_->has_exception())
     144                    this->state_->throw_stored_exception();
     145            }
    56146    };
     147
     148    shared_future<void> future<void>::share()
     149    {
     150        return shared_future<void>{move(*this)};
     151    }
    57152}
    58153
Note: See TracChangeset for help on using the changeset viewer.