scoped_bilock.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright 2003-2022 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_SCOPED_BILOCK_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_SCOPED_BILOCK_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/core/noncopyable.hpp>
  14. #include <boost/type_traits/aligned_storage.hpp>
  15. #include <boost/type_traits/alignment_of.hpp>
  16. #include <functional>
  17. #include <new>
  18. namespace boost{
  19. namespace multi_index{
  20. namespace detail{
  21. /* Locks/unlocks two RAII-lockable mutexes taking care that locking is done in
  22. * a deadlock-avoiding global order and no double locking happens when the two
  23. * mutexes are the same.
  24. */
  25. template<typename Mutex>
  26. class scoped_bilock:private noncopyable
  27. {
  28. public:
  29. scoped_bilock(Mutex& mutex1,Mutex& mutex2):mutex_eq(&mutex1==&mutex2)
  30. {
  31. bool mutex_lt=std::less<Mutex*>()(&mutex1,&mutex2);
  32. ::new (static_cast<void*>(&lock1)) scoped_lock(mutex_lt?mutex1:mutex2);
  33. if(!mutex_eq)
  34. ::new (static_cast<void*>(&lock2)) scoped_lock(mutex_lt?mutex2:mutex1);
  35. }
  36. ~scoped_bilock()
  37. {
  38. reinterpret_cast<scoped_lock*>(&lock1)->~scoped_lock();
  39. if(!mutex_eq)
  40. reinterpret_cast<scoped_lock*>(&lock2)->~scoped_lock();
  41. }
  42. private:
  43. typedef typename Mutex::scoped_lock scoped_lock;
  44. typedef typename aligned_storage<
  45. sizeof(scoped_lock),
  46. alignment_of<scoped_lock>::value
  47. >::type scoped_lock_space;
  48. bool mutex_eq;
  49. scoped_lock_space lock1,lock2;
  50. };
  51. } /* namespace multi_index::detail */
  52. } /* namespace multi_index */
  53. } /* namespace boost */
  54. #endif