Changeset ce22ac6 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:20Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
53c6e6a
Parents:
a97b838
git-author:
Dzejrou <dzejrou@…> (2018-03-29 12:56:20)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:20)
Message:

cpp: added unique_lock

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/mutex.hpp

    ra97b838 rce22ac6  
    197197    };
    198198
     199    /**
     200     * 30.4.1.4.1, class shared_timed_mutex:
     201     */
     202
     203    // TODO: implement
     204    class shared_timed_mutex;
     205
    199206    struct defer_lock_t
    200207    { /* DUMMY BODY */ };
     
    248255
    249256    template<class Mutex>
    250     class unique_lock;
     257    class unique_lock
     258    {
     259        public:
     260            using mutex_type = Mutex;
     261
     262            /**
     263             * 30.4.2.2.1, construction/copy/destroy:
     264             */
     265
     266            unique_lock() noexcept
     267                : mtx_{nullptr}, owns_{false}
     268            { /* DUMMY BODY */ }
     269
     270            explicit unique_lock(mutex_type& mtx)
     271                : mtx_{&mtx}, owns_{true}
     272            {
     273                mtx_->lock();
     274            }
     275
     276            unique_lock(mutex_type& mtx, defer_lock_t) noexcept
     277                : mtx_{&mtx}, owns_{false}
     278            { /* DUMMY BODY */ }
     279
     280            unique_lock(mutex_type& mtx, try_to_lock_t)
     281                : mtx_{&mtx}, owns_{}
     282            {
     283                owns_ = mtx_->try_lock();
     284            }
     285
     286            unique_lock(mutex_type& mtx, adopt_lock_t)
     287                : mtx_{&mtx}, owns_{true}
     288            { /* DUMMY BODY */ }
     289
     290            template<class Clock, class Duration>
     291            unique_lock(mutex_type& mtx, const chrono::time_point<Clock, Duration>& abs_time)
     292                : mtx_{&mtx}, owns_{}
     293            {
     294                owns_ = mtx_->try_lock_until(abs_time);
     295            }
     296
     297            template<class Rep, class Period>
     298            unique_lock(mutex_type& mtx, const chrono::duration<Rep, Period>& rel_time)
     299                : mtx_{&mtx}, owns_{}
     300            {
     301                owns_ = mtx_->try_lock_for(rel_time);
     302            }
     303
     304            ~unique_lock()
     305            {
     306                if (owns_)
     307                    mtx_->unlock();
     308            }
     309
     310            unique_lock(const unique_lock&) = delete;
     311            unique_lock& operator=(const unique_lock&) = delete;
     312
     313            unique_lock(unique_lock&& other) noexcept
     314                : mtx_{move(other.mtx_)}, owns_{move(other.owns_)}
     315            {
     316                other.mtx_ = nullptr;
     317                other.owns_ = false;
     318            }
     319
     320            unique_lock& operator=(unique_lock&& other)
     321            {
     322                if (owns_)
     323                    mtx_->unlock();
     324
     325                mtx_ = move(other.mtx_);
     326                owns_ = move(other.owns_);
     327
     328                other.mtx_ = nullptr;
     329                other.owns_ = false;
     330            }
     331
     332            /**
     333             * 30.4.2.2.2, locking:
     334             */
     335
     336            void lock()
     337            {
     338                /**
     339                 * TODO:
     340                 * throw system_error operation_not_permitted if mtx_ == nullptr
     341                 * throw system_error resource_deadlock_would_occur if owns_ == true
     342                 */
     343
     344                mtx_->lock();
     345                owns_ = true;
     346            }
     347
     348            bool try_lock()
     349            {
     350                /**
     351                 * TODO:
     352                 * throw system_error operation_not_permitted if mtx_ == nullptr
     353                 * throw system_error resource_deadlock_would_occur if owns_ == true
     354                 */
     355
     356                owns_ = mtx_->try_lock();
     357
     358                return owns_;
     359            }
     360
     361            template<class Rep, class Period>
     362            bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
     363            {
     364                /**
     365                 * TODO:
     366                 * throw system_error operation_not_permitted if mtx_ == nullptr
     367                 * throw system_error resource_deadlock_would_occur if owns_ == true
     368                 */
     369
     370                owns_ = mtx_->try_lock_for(rel_time);
     371
     372                return owns_;
     373            }
     374
     375            template<class Clock, class Duration>
     376            bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
     377            {
     378                /**
     379                 * TODO:
     380                 * throw system_error operation_not_permitted if mtx_ == nullptr
     381                 * throw system_error resource_deadlock_would_occur if owns_ == true
     382                 */
     383
     384                owns_ = mtx_->try_lock_until(abs_time);
     385
     386                return owns_;
     387            }
     388
     389            void unlock();
     390            {
     391                /**
     392                 * TODO:
     393                 * throw system_error operation_not_permitted if owns_ == false
     394                 */
     395
     396                mtx_->unlock();
     397            }
     398
     399            /**
     400             * 30.4.2.2.3, modifiers:
     401             */
     402
     403            void swap(unique_lock& other) noexcept
     404            {
     405                std::swap(mtx_, other.mtx_);
     406                std::swap(owns_, other.owns_);
     407            }
     408
     409            mutex_type* release() noexcept
     410            {
     411                auto ret = mtx_;
     412                mtx_ = nullptr;
     413                owns_ = false;
     414
     415                return ret;
     416            }
     417
     418            /**
     419             * 30.4.2.2.4, observers:
     420             */
     421
     422            bool owns_lock() const noexcept
     423            {
     424                return owns_;
     425            }
     426
     427            explicit operator bool() const noexcept
     428            {
     429                return owns_;
     430            }
     431
     432            mutex_type* mutex() const noexcept
     433            {
     434                return mtx_;
     435            }
     436
     437        private:
     438            mutex_type* mtx_;
     439            bool owns_;
     440    };
    251441
    252442    template<class Mutex>
    253     void swap(unique_lock<Mutex>& lhs, unique_lock<Mutex>& rhs) noexcept;
     443    void swap(unique_lock<Mutex>& lhs, unique_lock<Mutex>& rhs) noexcept
     444    {
     445        lhs.swap(rhs);
     446    }
     447
     448    /**
     449     * 30.4.2.3, class template shared_lock:
     450     */
     451
     452    // TODO:
     453    template<class Mutex>
     454    class shared_lock;
     455
     456    template<class Mutex>
     457    void swap(shared_lock<Mutex>& lhs, shared_lock<Mutex>& rhs) noexcept;
    254458
    255459    template<class L1, class L2, class... L3>
Note: See TracChangeset for help on using the changeset viewer.