Changeset b3b8405 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8a8a9273
Parents:
a6139852
git-author:
Dzejrou <dzejrou@…> (2018-06-25 15:20:35)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: split <mutex> into <mutex> and <shared_mutex>

Location:
uspace/lib/cpp
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/Makefile

    ra6139852 rb3b8405  
    4747        src/mutex.cpp \
    4848        src/new.cpp \
     49        src/shared_mutex.cpp \
    4950        src/stdexcept.cpp \
    5051        src/string.cpp \
  • uspace/lib/cpp/include/impl/mutex.hpp

    ra6139852 rb3b8405  
    198198    };
    199199
    200     /**
    201      * 30.4.1.4.1, class shared_timed_mutex:
    202      */
    203 
    204     class shared_timed_mutex
    205     {
    206         public:
    207             shared_timed_mutex() noexcept;
    208             ~shared_timed_mutex();
    209 
    210             shared_timed_mutex(const shared_timed_mutex&) = delete;
    211             shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
    212 
    213             void lock();
    214             bool try_lock();
    215             void unlock();
    216 
    217             template<class Rep, class Period>
    218             bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
    219             {
    220                 auto time = aux::threading::time::convert(rel_time);
    221 
    222                 return aux::threading::shared_mutex::try_lock_for(time);
    223             }
    224 
    225             template<class Clock, class Duration>
    226             bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
    227             {
    228                 auto dur = (abs_time - Clock::now());
    229                 auto time = aux::threading::time::convert(dur);
    230 
    231                 return aux::threading::shared_mutex::try_lock_for(time);
    232             }
    233 
    234             void lock_shared();
    235             bool try_lock_shared();
    236             void unlock_shared();
    237 
    238             template<class Rep, class Period>
    239             bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time)
    240             {
    241                 auto time = aux::threading::time::convert(rel_time);
    242 
    243                 return aux::threading::shared_mutex::try_lock_shared_for(time);
    244             }
    245 
    246             template<class Clock, class Duration>
    247             bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time)
    248             {
    249                 auto dur = (abs_time - Clock::now());
    250                 auto time = aux::threading::time::convert(dur);
    251 
    252                 return aux::threading::shared_mutex::try_lock_shared_for(time);
    253             }
    254 
    255             using native_handle_type = aux::shared_mutex_t*;
    256             native_handle_type native_handle();
    257 
    258         private:
    259             aux::shared_mutex_t mtx_;
    260     };
    261 
    262200    struct defer_lock_t
    263201    { /* DUMMY BODY */ };
     
    498436    template<class Mutex>
    499437    void swap(unique_lock<Mutex>& lhs, unique_lock<Mutex>& rhs) noexcept
    500     {
    501         lhs.swap(rhs);
    502     }
    503 
    504     /**
    505      * 30.4.2.3, class template shared_lock:
    506      */
    507 
    508     template<class Mutex>
    509     class shared_lock
    510     {
    511         public:
    512             using mutex_type = Mutex;
    513 
    514             /**
    515              * 30.4.2.2.1, construction/copy/destroy:
    516              */
    517 
    518             shared_lock() noexcept
    519                 : mtx_{nullptr}, owns_{false}
    520             { /* DUMMY BODY */ }
    521 
    522             explicit shared_lock(mutex_type& mtx)
    523                 : mtx_{&mtx}, owns_{true}
    524             {
    525                 mtx_->lock_shared();
    526             }
    527 
    528             shared_lock(mutex_type& mtx, defer_lock_t) noexcept
    529                 : mtx_{&mtx}, owns_{false}
    530             { /* DUMMY BODY */ }
    531 
    532             shared_lock(mutex_type& mtx, try_to_lock_t)
    533                 : mtx_{&mtx}, owns_{}
    534             {
    535                 owns_ = mtx_->try_lock_shared();
    536             }
    537 
    538             shared_lock(mutex_type& mtx, adopt_lock_t)
    539                 : mtx_{&mtx}, owns_{true}
    540             { /* DUMMY BODY */ }
    541 
    542             template<class Clock, class Duration>
    543             shared_lock(mutex_type& mtx, const chrono::time_point<Clock, Duration>& abs_time)
    544                 : mtx_{&mtx}, owns_{}
    545             {
    546                 owns_ = mtx_->try_lock_shared_until(abs_time);
    547             }
    548 
    549             template<class Rep, class Period>
    550             shared_lock(mutex_type& mtx, const chrono::duration<Rep, Period>& rel_time)
    551                 : mtx_{&mtx}, owns_{}
    552             {
    553                 owns_ = mtx_->try_lock_shared_for(rel_time);
    554             }
    555 
    556             ~shared_lock()
    557             {
    558                 if (owns_)
    559                     mtx_->unlock_shared();
    560             }
    561 
    562             shared_lock(const shared_lock&) = delete;
    563             shared_lock& operator=(const shared_lock&) = delete;
    564 
    565             shared_lock(shared_lock&& other) noexcept
    566                 : mtx_{move(other.mtx_)}, owns_{move(other.owns_)}
    567             {
    568                 other.mtx_ = nullptr;
    569                 other.owns_ = false;
    570             }
    571 
    572             shared_lock& operator=(shared_lock&& other)
    573             {
    574                 if (owns_)
    575                     mtx_->unlock_shared();
    576 
    577                 mtx_ = move(other.mtx_);
    578                 owns_ = move(other.owns_);
    579 
    580                 other.mtx_ = nullptr;
    581                 other.owns_ = false;
    582             }
    583 
    584             /**
    585              * 30.4.2.2.2, locking:
    586              */
    587 
    588             void lock()
    589             {
    590                 /**
    591                  * TODO:
    592                  * throw system_error operation_not_permitted if mtx_ == nullptr
    593                  * throw system_error resource_deadlock_would_occur if owns_ == true
    594                  */
    595 
    596                 mtx_->lock_shared();
    597                 owns_ = true;
    598             }
    599 
    600             bool try_lock()
    601             {
    602                 /**
    603                  * TODO:
    604                  * throw system_error operation_not_permitted if mtx_ == nullptr
    605                  * throw system_error resource_deadlock_would_occur if owns_ == true
    606                  */
    607 
    608                 owns_ = mtx_->try_lock_shared();
    609 
    610                 return owns_;
    611             }
    612 
    613             template<class Rep, class Period>
    614             bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
    615             {
    616                 /**
    617                  * TODO:
    618                  * throw system_error operation_not_permitted if mtx_ == nullptr
    619                  * throw system_error resource_deadlock_would_occur if owns_ == true
    620                  */
    621 
    622                 owns_ = mtx_->try_lock_shared_for(rel_time);
    623 
    624                 return owns_;
    625             }
    626 
    627             template<class Clock, class Duration>
    628             bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
    629             {
    630                 /**
    631                  * TODO:
    632                  * throw system_error operation_not_permitted if mtx_ == nullptr
    633                  * throw system_error resource_deadlock_would_occur if owns_ == true
    634                  */
    635 
    636                 owns_ = mtx_->try_lock_shared_until(abs_time);
    637 
    638                 return owns_;
    639             }
    640 
    641             void unlock()
    642             {
    643                 /**
    644                  * TODO:
    645                  * throw system_error operation_not_permitted if owns_ == false
    646                  */
    647 
    648                 mtx_->unlock_shared();
    649             }
    650 
    651             /**
    652              * 30.4.2.2.3, modifiers:
    653              */
    654 
    655             void swap(shared_lock& other) noexcept
    656             {
    657                 std::swap(mtx_, other.mtx_);
    658                 std::swap(owns_, other.owns_);
    659             }
    660 
    661             mutex_type* release() noexcept
    662             {
    663                 auto ret = mtx_;
    664                 mtx_ = nullptr;
    665                 owns_ = false;
    666 
    667                 return ret;
    668             }
    669 
    670             /**
    671              * 30.4.2.2.4, observers:
    672              */
    673 
    674             bool owns_lock() const noexcept
    675             {
    676                 return owns_;
    677             }
    678 
    679             explicit operator bool() const noexcept
    680             {
    681                 return owns_;
    682             }
    683 
    684             mutex_type* mutex() const noexcept
    685             {
    686                 return mtx_;
    687             }
    688 
    689         private:
    690             mutex_type* mtx_;
    691             bool owns_;
    692     };
    693 
    694     template<class Mutex>
    695     void swap(shared_lock<Mutex>& lhs, shared_lock<Mutex>& rhs) noexcept
    696438    {
    697439        lhs.swap(rhs);
  • uspace/lib/cpp/src/mutex.cpp

    ra6139852 rb3b8405  
    173173        return &mtx_;
    174174    }
    175 
    176     shared_timed_mutex::shared_timed_mutex() noexcept
    177         : mtx_{}
    178     {
    179         aux::threading::shared_mutex::init(mtx_);
    180     }
    181 
    182     shared_timed_mutex::~shared_timed_mutex()
    183     { /* DUMMY BODY */ }
    184 
    185     void shared_timed_mutex::lock()
    186     {
    187         aux::threading::shared_mutex::lock(mtx_);
    188     }
    189 
    190     bool shared_timed_mutex::try_lock()
    191     {
    192         return aux::threading::shared_mutex::try_lock(mtx_);
    193     }
    194 
    195     void shared_timed_mutex::unlock()
    196     {
    197         aux::threading::shared_mutex::unlock(mtx_);
    198     }
    199 
    200     void shared_timed_mutex::lock_shared()
    201     {
    202         aux::threading::shared_mutex::lock_shared(mtx_);
    203     }
    204 
    205     bool shared_timed_mutex::try_lock_shared()
    206     {
    207         return aux::threading::shared_mutex::try_lock_shared(mtx_);
    208     }
    209 
    210     void shared_timed_mutex::unlock_shared()
    211     {
    212         aux::threading::shared_mutex::unlock_shared(mtx_);
    213     }
    214 
    215     shared_timed_mutex::native_handle_type shared_timed_mutex::native_handle()
    216     {
    217         return &mtx_;
    218     }
    219175}
Note: See TracChangeset for help on using the changeset viewer.