lockpool.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef BOOST_ATOMIC_DETAIL_LOCKPOOL_HPP
  2. #define BOOST_ATOMIC_DETAIL_LOCKPOOL_HPP
  3. // Copyright (c) 2011 Helge Bahmann
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <boost/atomic/detail/config.hpp>
  9. #include <boost/atomic/detail/link.hpp>
  10. #ifndef BOOST_ATOMIC_FLAG_LOCK_FREE
  11. #include <boost/smart_ptr/detail/lightweight_mutex.hpp>
  12. #endif
  13. #ifdef BOOST_HAS_PRAGMA_ONCE
  14. #pragma once
  15. #endif
  16. namespace boost {
  17. namespace atomics {
  18. namespace detail {
  19. #ifndef BOOST_ATOMIC_FLAG_LOCK_FREE
  20. class lockpool
  21. {
  22. public:
  23. typedef boost::detail::lightweight_mutex lock_type;
  24. class scoped_lock :
  25. public lock_type::scoped_lock
  26. {
  27. typedef lock_type::scoped_lock base_type;
  28. public:
  29. explicit scoped_lock(const volatile void * addr) : base_type(get_lock_for(addr))
  30. {
  31. }
  32. BOOST_DELETED_FUNCTION(scoped_lock(scoped_lock const&))
  33. BOOST_DELETED_FUNCTION(scoped_lock& operator=(scoped_lock const&))
  34. };
  35. private:
  36. static BOOST_ATOMIC_DECL lock_type& get_lock_for(const volatile void * addr);
  37. };
  38. #else
  39. class lockpool
  40. {
  41. public:
  42. typedef atomic_flag lock_type;
  43. class scoped_lock
  44. {
  45. private:
  46. atomic_flag& flag_;
  47. public:
  48. explicit
  49. scoped_lock(const volatile void * addr) : flag_(get_lock_for(addr))
  50. {
  51. for (; flag_.test_and_set(memory_order_acquire);)
  52. {
  53. #if defined(BOOST_ATOMIC_X86_PAUSE)
  54. BOOST_ATOMIC_X86_PAUSE();
  55. #endif
  56. }
  57. }
  58. ~scoped_lock(void)
  59. {
  60. flag_.clear(memory_order_release);
  61. }
  62. BOOST_DELETED_FUNCTION(scoped_lock(const scoped_lock &))
  63. BOOST_DELETED_FUNCTION(scoped_lock& operator=(const scoped_lock &))
  64. };
  65. private:
  66. static BOOST_ATOMIC_DECL lock_type& get_lock_for(const volatile void * addr);
  67. };
  68. #endif
  69. }
  70. }
  71. }
  72. #endif