reverse_lock.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_REVERSE_LOCK_HPP
  6. #define BOOST_THREAD_REVERSE_LOCK_HPP
  7. #include <boost/thread/detail/config.hpp>
  8. #include <boost/thread/detail/move.hpp>
  9. #include <boost/thread/lockable_traits.hpp>
  10. #include <boost/thread/lock_options.hpp>
  11. #include <boost/thread/detail/delete.hpp>
  12. namespace boost
  13. {
  14. template<typename Lock>
  15. class reverse_lock
  16. {
  17. public:
  18. typedef typename Lock::mutex_type mutex_type;
  19. BOOST_THREAD_NO_COPYABLE(reverse_lock)
  20. explicit reverse_lock(Lock& m_)
  21. : m(m_), mtx(0)
  22. {
  23. if (m.owns_lock())
  24. {
  25. m.unlock();
  26. }
  27. mtx=m.release();
  28. }
  29. ~reverse_lock()
  30. {
  31. if (mtx) {
  32. mtx->lock();
  33. m = BOOST_THREAD_MAKE_RV_REF(Lock(*mtx, adopt_lock));
  34. }
  35. }
  36. private:
  37. Lock& m;
  38. mutex_type* mtx;
  39. };
  40. #ifdef BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES
  41. template<typename T>
  42. struct is_mutex_type<reverse_lock<T> >
  43. {
  44. BOOST_STATIC_CONSTANT(bool, value = true);
  45. };
  46. #endif
  47. }
  48. #endif // header