Changeset 857d4cc 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:
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

Location:
uspace/lib/cpp
Files:
2 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
  • uspace/lib/cpp/src/mutex.cpp

    rbefead8 r857d4cc  
    6969    }
    7070
    71     bool recursive_mutex::try_lock()
     71    bool recursive_mutex::try_lock() noexcept
    7272    {
    7373        if (owner_ != this_thread::get_id())
     
    129129        return &mtx_;
    130130    }
     131
     132    recursive_timed_mutex::~recursive_timed_mutex()
     133    { /* DUMMY BODY */ }
     134
     135    void recursive_timed_mutex::lock()
     136    {
     137        if (owner_ != this_thread::get_id())
     138        {
     139            aux::threading::mutex::lock(mtx_);
     140            owner_ = this_thread::get_id();
     141            lock_level_ = 1;
     142        }
     143        else
     144            ++lock_level_;
     145    }
     146
     147    bool recursive_timed_mutex::try_lock()
     148    {
     149        if (owner_ != this_thread::get_id())
     150        {
     151            bool res = aux::threading::mutex::try_lock(mtx_);
     152            if (res)
     153            {
     154                owner_ = this_thread::get_id();
     155                lock_level_ = 1;
     156            }
     157
     158            return res;
     159        }
     160        else
     161            ++lock_level_;
     162
     163        return true;
     164    }
     165
     166    void recursive_timed_mutex::unlock()
     167    {
     168        if (owner_ != this_thread::get_id())
     169            return;
     170        else if (--lock_level_ == 0)
     171            aux::threading::mutex::unlock(mtx_);
     172    }
     173
     174    recursive_timed_mutex::native_handle_type recursive_timed_mutex::native_handle()
     175    {
     176        return &mtx_;
     177    }
    131178}
Note: See TracChangeset for help on using the changeset viewer.