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
8 // Distributed under the Boost Software License, Version 1.0. (See
9 // accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 #include <boost/thread/win32/thread_primitives.hpp>
13 #include <boost/thread/win32/basic_timed_mutex.hpp>
15 #include <boost/config/abi_prefix.hpp>
21 template<typename underlying_mutex_type>
22 struct basic_recursive_mutex_impl
25 long locking_thread_id;
26 underlying_mutex_type mutex;
42 long const current_thread_id=win32::GetCurrentThreadId();
43 return try_recursive_lock(current_thread_id) || try_basic_lock(current_thread_id);
48 long const current_thread_id=win32::GetCurrentThreadId();
49 if(!try_recursive_lock(current_thread_id))
52 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
56 bool timed_lock(::boost::system_time const& target)
58 long const current_thread_id=win32::GetCurrentThreadId();
59 return try_recursive_lock(current_thread_id) || try_timed_lock(current_thread_id,target);
61 template<typename Duration>
62 bool timed_lock(Duration const& timeout)
64 return timed_lock(get_system_time()+timeout);
69 if(!--recursion_count)
71 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,0);
77 bool try_recursive_lock(long current_thread_id)
79 if(::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id)
87 bool try_basic_lock(long current_thread_id)
91 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
98 bool try_timed_lock(long current_thread_id,::boost::system_time const& target)
100 if(mutex.timed_lock(target))
102 BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
111 typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_mutex;
112 typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_timed_mutex;
116 #define BOOST_BASIC_RECURSIVE_MUTEX_INITIALIZER {0}
118 #include <boost/config/abi_suffix.hpp>