1 #ifndef BOOST_BASIC_RECURSIVE_MUTEX_WIN32_HPP
2 #define BOOST_BASIC_RECURSIVE_MUTEX_WIN32_HPP
4 // basic_recursive_mutex.hpp
6 // (C) Copyright 2006-8 Anthony Williams
7 // (C) Copyright 2011-2012,2017-2018 Vicente J. Botet Escriba
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/thread/win32/thread_primitives.hpp>
14 #include <boost/thread/win32/basic_timed_mutex.hpp>
15 #ifdef BOOST_THREAD_USES_CHRONO
16 #include <boost/chrono/system_clocks.hpp>
17 #include <boost/chrono/ceil.hpp>
20 #include <boost/config/abi_prefix.hpp>
26 template<typename underlying_mutex_type>
27 struct basic_recursive_mutex_impl
30 long locking_thread_id;
31 underlying_mutex_type mutex;
45 bool try_lock() BOOST_NOEXCEPT
47 long const current_thread_id=boost::winapi::GetCurrentThreadId();
48 return try_recursive_lock(current_thread_id) || try_basic_lock(current_thread_id);
53 long const current_thread_id=boost::winapi::GetCurrentThreadId();
54 if(!try_recursive_lock(current_thread_id))
57 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
61 #if defined BOOST_THREAD_USES_DATETIME
62 bool timed_lock(::boost::system_time const& target)
64 long const current_thread_id=boost::winapi::GetCurrentThreadId();
65 return try_recursive_lock(current_thread_id) || try_timed_lock(current_thread_id,target);
67 template<typename Duration>
68 bool timed_lock(Duration const& target)
70 long const current_thread_id=boost::winapi::GetCurrentThreadId();
71 return try_recursive_lock(current_thread_id) || try_timed_lock(current_thread_id,target);
75 #ifdef BOOST_THREAD_USES_CHRONO
76 template <class Rep, class Period>
77 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
79 long const current_thread_id=boost::winapi::GetCurrentThreadId();
80 return try_recursive_lock(current_thread_id) || try_timed_lock_for(current_thread_id,rel_time);
82 template <class Clock, class Duration>
83 bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
85 long const current_thread_id=boost::winapi::GetCurrentThreadId();
86 return try_recursive_lock(current_thread_id) || try_timed_lock_until(current_thread_id,t);
91 if(!--recursion_count)
93 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,0);
99 bool try_recursive_lock(long current_thread_id) BOOST_NOEXCEPT
101 if(::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id)
109 bool try_basic_lock(long current_thread_id) BOOST_NOEXCEPT
113 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
120 #if defined BOOST_THREAD_USES_DATETIME
121 bool try_timed_lock(long current_thread_id,::boost::system_time const& target)
123 if(mutex.timed_lock(target))
125 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
131 template<typename Duration>
132 bool try_timed_lock(long current_thread_id,Duration const& target)
134 if(mutex.timed_lock(target))
136 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
143 template <typename TP>
144 bool try_timed_lock_until(long current_thread_id,TP const& target)
146 if(mutex.try_lock_until(target))
148 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
154 template <typename D>
155 bool try_timed_lock_for(long current_thread_id,D const& target)
157 if(mutex.try_lock_for(target))
159 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
167 typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_mutex;
168 typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_timed_mutex;
172 #define BOOST_BASIC_RECURSIVE_MUTEX_INITIALIZER {0}
174 #include <boost/config/abi_suffix.hpp>