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 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::detail::winapi::GetCurrentThreadId();
48 return try_recursive_lock(current_thread_id) || try_basic_lock(current_thread_id);
53 long const current_thread_id=boost::detail::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::detail::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& timeout)
70 return timed_lock(get_system_time()+timeout);
74 #ifdef BOOST_THREAD_USES_CHRONO
75 template <class Rep, class Period>
76 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
78 long const current_thread_id=boost::detail::winapi::GetCurrentThreadId();
79 return try_recursive_lock(current_thread_id) || try_timed_lock_for(current_thread_id,rel_time);
81 template <class Clock, class Duration>
82 bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
84 long const current_thread_id=boost::detail::winapi::GetCurrentThreadId();
85 return try_recursive_lock(current_thread_id) || try_timed_lock_until(current_thread_id,t);
90 if(!--recursion_count)
92 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,0);
98 bool try_recursive_lock(long current_thread_id) BOOST_NOEXCEPT
100 if(::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id)
108 bool try_basic_lock(long current_thread_id) BOOST_NOEXCEPT
112 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
119 #if defined BOOST_THREAD_USES_DATETIME
120 bool try_timed_lock(long current_thread_id,::boost::system_time const& target)
122 if(mutex.timed_lock(target))
124 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
131 template <typename TP>
132 bool try_timed_lock_until(long current_thread_id,TP const& target)
134 if(mutex.try_lock_until(target))
136 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
142 template <typename D>
143 bool try_timed_lock_for(long current_thread_id,D const& target)
145 if(mutex.try_lock_for(target))
147 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
155 typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_mutex;
156 typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_timed_mutex;
160 #define BOOST_BASIC_RECURSIVE_MUTEX_INITIALIZER {0}
162 #include <boost/config/abi_suffix.hpp>