PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/thread/win32/basic_recursive_mutex.hpp

http://hadesmem.googlecode.com/
C++ Header | 120 lines | 96 code | 17 blank | 7 comment | 7 complexity | c665a5379e6cedf37b0b3e20545415cd MD5 | raw file
  1#ifndef BOOST_BASIC_RECURSIVE_MUTEX_WIN32_HPP
  2#define BOOST_BASIC_RECURSIVE_MUTEX_WIN32_HPP
  3
  4//  basic_recursive_mutex.hpp
  5//
  6//  (C) Copyright 2006-8 Anthony Williams 
  7//
  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)
 11
 12#include "thread_primitives.hpp"
 13#include "basic_timed_mutex.hpp"
 14
 15#include <boost/config/abi_prefix.hpp>
 16
 17namespace boost
 18{
 19    namespace detail
 20    {
 21        template<typename underlying_mutex_type>
 22        struct basic_recursive_mutex_impl
 23        {
 24            long recursion_count;
 25            long locking_thread_id;
 26            underlying_mutex_type mutex;
 27
 28            void initialize()
 29            {
 30                recursion_count=0;
 31                locking_thread_id=0;
 32                mutex.initialize();
 33            }
 34
 35            void destroy()
 36            {
 37                mutex.destroy();
 38            }
 39
 40            bool try_lock()
 41            {
 42                long const current_thread_id=win32::GetCurrentThreadId();
 43                return try_recursive_lock(current_thread_id) || try_basic_lock(current_thread_id);
 44            }
 45            
 46            void lock()
 47            {
 48                long const current_thread_id=win32::GetCurrentThreadId();
 49                if(!try_recursive_lock(current_thread_id))
 50                {
 51                    mutex.lock();
 52                    BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
 53                    recursion_count=1;
 54                }
 55            }
 56            bool timed_lock(::boost::system_time const& target)
 57            {
 58                long const current_thread_id=win32::GetCurrentThreadId();
 59                return try_recursive_lock(current_thread_id) || try_timed_lock(current_thread_id,target);
 60            }
 61            template<typename Duration>
 62            bool timed_lock(Duration const& timeout)
 63            {
 64                return timed_lock(get_system_time()+timeout);
 65            }
 66
 67            void unlock()
 68            {
 69                if(!--recursion_count)
 70                {
 71                    BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,0);
 72                    mutex.unlock();
 73                }
 74            }
 75
 76        private:
 77            bool try_recursive_lock(long current_thread_id)
 78            {
 79                if(::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id)
 80                {
 81                    ++recursion_count;
 82                    return true;
 83                }
 84                return false;
 85            }
 86            
 87            bool try_basic_lock(long current_thread_id)
 88            {
 89                if(mutex.try_lock())
 90                {
 91                    BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
 92                    recursion_count=1;
 93                    return true;
 94                }
 95                return false;
 96            }
 97            
 98            bool try_timed_lock(long current_thread_id,::boost::system_time const& target)
 99            {
100                if(mutex.timed_lock(target))
101                {
102                    BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
103                    recursion_count=1;
104                    return true;
105                }
106                return false;
107            }
108            
109        };
110
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;
113    }
114}
115
116#define BOOST_BASIC_RECURSIVE_MUTEX_INITIALIZER {0}
117
118#include <boost/config/abi_suffix.hpp>
119
120#endif