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

cpp: added recursive_timed_mutex

File:
1 edited

Legend:

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

    rbefead8 r857d4cc  
    8484
    8585            void lock();
    86             bool try_lock();
     86            bool try_lock() noexcept;
    8787            void unlock();
    8888
     
    141141     */
    142142
    143     // TODO: implement
    144     class recursive_timed_mutex;
     143    class recursive_timed_mutex
     144    {
     145        public:
     146            recursive_timed_mutex() noexcept
     147                : mtx_{}, lock_level_{}, owner_{}
     148            {
     149                aux::threading::mutex::init(mtx_);
     150            }
     151
     152            ~recursive_timed_mutex();
     153
     154            recursive_timed_mutex(const recursive_timed_mutex&) = delete;
     155            recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
     156
     157            void lock();
     158            bool try_lock();
     159            void unlock();
     160
     161            template<class Rep, class Period>
     162            bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
     163            {
     164                if (owner_ == this_thread::get_id())
     165                    return true;
     166
     167                auto time = aux::threading::time::convert(rel_time);
     168                auto ret = aux::threading::mutex::try_lock_for(time);
     169
     170                if (ret)
     171                    ++lock_level_;
     172                return ret;
     173            }
     174
     175            template<class Clock, class Duration>
     176            bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time)
     177            {
     178                if (owner_ == this_thread::get_id())
     179                    return true;
     180
     181                auto dur = (abs_time - Clock::now());
     182                auto time = aux::threading::time::convert(dur);
     183                auto ret = aux::threading::mutex::try_lock_for(time);
     184
     185                if (ret)
     186                    ++lock_level_;
     187                return ret;
     188            }
     189
     190            using native_handle_type = aux::mutex_t*;
     191            native_handle_type native_handle();
     192
     193        private:
     194            aux::mutex_t mtx_;
     195            size_t lock_level_;
     196            thread::id owner_;
     197    };
    145198
    146199    struct defer_lock_t
Note: See TracChangeset for help on using the changeset viewer.