lock_factories.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2012 Vicente J. Botet Escriba
  5. #ifndef BOOST_THREAD_LOCK_FACTORIES_HPP
  6. #define BOOST_THREAD_LOCK_FACTORIES_HPP
  7. #include <boost/thread/lock_types.hpp>
  8. #include <boost/thread/lock_algorithms.hpp>
  9. #if ! defined(BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS)
  10. #include <tuple> // todo change to <boost/tuple.hpp> once Boost.Tuple or Boost.Fusion provides Move semantics.
  11. #endif
  12. #include <boost/config/abi_prefix.hpp>
  13. namespace boost
  14. {
  15. template <typename Lockable>
  16. unique_lock<Lockable> make_unique_lock(Lockable& mtx)
  17. {
  18. return unique_lock<Lockable> (mtx);
  19. }
  20. template <typename Lockable>
  21. unique_lock<Lockable> make_unique_lock(Lockable& mtx, adopt_lock_t)
  22. {
  23. return unique_lock<Lockable> (mtx, adopt_lock);
  24. }
  25. template <typename Lockable>
  26. unique_lock<Lockable> make_unique_lock(Lockable& mtx, defer_lock_t)
  27. {
  28. return unique_lock<Lockable> (mtx, defer_lock);
  29. }
  30. template <typename Lockable>
  31. unique_lock<Lockable> make_unique_lock(Lockable& mtx, try_to_lock_t)
  32. {
  33. return unique_lock<Lockable> (mtx, try_to_lock);
  34. }
  35. #if ! defined(BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS)
  36. #if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
  37. template <typename ...Lockable>
  38. std::tuple<unique_lock<Lockable> ...> make_unique_locks(Lockable& ...mtx)
  39. {
  40. boost::lock(mtx...);
  41. return std::tuple<unique_lock<Lockable> ...>(unique_lock<Lockable>(mtx, adopt_lock)...);
  42. }
  43. #else
  44. template <typename L1, typename L2>
  45. std::tuple<unique_lock<L1>, unique_lock<L2> > make_unique_locks(L1& m1, L2& m2)
  46. {
  47. boost::lock(m1, m2);
  48. return std::tuple<unique_lock<L1>,unique_lock<L2> >(
  49. unique_lock<L1>(m1, adopt_lock),
  50. unique_lock<L2>(m2, adopt_lock)
  51. );
  52. }
  53. template <typename L1, typename L2, typename L3>
  54. std::tuple<unique_lock<L1>, unique_lock<L2>, unique_lock<L3> > make_unique_locks(L1& m1, L2& m2, L3& m3)
  55. {
  56. boost::lock(m1, m2, m3);
  57. return std::tuple<unique_lock<L1>,unique_lock<L2>,unique_lock<L3> >(
  58. unique_lock<L1>(m1, adopt_lock),
  59. unique_lock<L2>(m2, adopt_lock),
  60. unique_lock<L3>(m3, adopt_lock)
  61. );
  62. }
  63. #endif
  64. #endif
  65. }
  66. #include <boost/config/abi_suffix.hpp>
  67. #endif