Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:42:09

0001 /* Copyright 2003-2022 Joaquin M Lopez Munoz.
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * See http://www.boost.org/libs/multi_index for library home page.
0007  */
0008 
0009 #ifndef BOOST_MULTI_INDEX_DETAIL_SCOPED_BILOCK_HPP
0010 #define BOOST_MULTI_INDEX_DETAIL_SCOPED_BILOCK_HPP
0011 
0012 #if defined(_MSC_VER)
0013 #pragma once
0014 #endif
0015 
0016 #include <boost/core/noncopyable.hpp>
0017 #include <boost/type_traits/aligned_storage.hpp>
0018 #include <boost/type_traits/alignment_of.hpp>
0019 #include <functional>
0020 #include <new>
0021 
0022 namespace boost{
0023 
0024 namespace multi_index{
0025 
0026 namespace detail{
0027 
0028 /* Locks/unlocks two RAII-lockable mutexes taking care that locking is done in
0029  * a deadlock-avoiding global order and no double locking happens when the two
0030  * mutexes are the same.
0031  */
0032 
0033 template<typename Mutex>
0034 class scoped_bilock:private noncopyable
0035 {
0036 public:
0037   scoped_bilock(Mutex& mutex1,Mutex& mutex2):mutex_eq(&mutex1==&mutex2)
0038   {
0039     bool mutex_lt=std::less<Mutex*>()(&mutex1,&mutex2);
0040 
0041     ::new (static_cast<void*>(&lock1)) scoped_lock(mutex_lt?mutex1:mutex2);
0042     if(!mutex_eq)
0043       ::new (static_cast<void*>(&lock2)) scoped_lock(mutex_lt?mutex2:mutex1);
0044   }
0045 
0046   ~scoped_bilock()
0047   {
0048     reinterpret_cast<scoped_lock*>(&lock1)->~scoped_lock();
0049     if(!mutex_eq)
0050       reinterpret_cast<scoped_lock*>(&lock2)->~scoped_lock();
0051   }
0052 
0053 private:
0054   typedef typename Mutex::scoped_lock scoped_lock;
0055   typedef typename aligned_storage<
0056     sizeof(scoped_lock),
0057     alignment_of<scoped_lock>::value
0058   >::type                             scoped_lock_space;
0059 
0060   bool              mutex_eq;
0061   scoped_lock_space lock1,lock2;
0062 };
0063 
0064 } /* namespace multi_index::detail */
0065 
0066 } /* namespace multi_index */
0067 
0068 } /* namespace boost */
0069 
0070 #endif