spinlock_pool.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/spinlock_pool.hpp
  9. //
  10. // Copyright (c) 2008 Peter Dimov
  11. //
  12. // Distributed under the Boost Software License, Version 1.0.
  13. // See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. // spinlock_pool<0> is reserved for atomic<>, when/if it arrives
  17. // spinlock_pool<1> is reserved for shared_ptr reference counts
  18. // spinlock_pool<2> is reserved for shared_ptr atomic access
  19. //
  20. #include <boost/config.hpp>
  21. #include <boost/smart_ptr/detail/spinlock.hpp>
  22. #include <cstddef>
  23. namespace boost
  24. {
  25. namespace detail
  26. {
  27. template< int M > class spinlock_pool
  28. {
  29. private:
  30. static spinlock pool_[ 41 ];
  31. public:
  32. static spinlock & spinlock_for( void const * pv )
  33. {
  34. #if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
  35. std::size_t i = reinterpret_cast< unsigned long long >( pv ) % 41;
  36. #else
  37. std::size_t i = reinterpret_cast< std::size_t >( pv ) % 41;
  38. #endif
  39. return pool_[ i ];
  40. }
  41. class scoped_lock
  42. {
  43. private:
  44. spinlock & sp_;
  45. scoped_lock( scoped_lock const & );
  46. scoped_lock & operator=( scoped_lock const & );
  47. public:
  48. explicit scoped_lock( void const * pv ): sp_( spinlock_for( pv ) )
  49. {
  50. sp_.lock();
  51. }
  52. ~scoped_lock()
  53. {
  54. sp_.unlock();
  55. }
  56. };
  57. };
  58. template< int M > spinlock spinlock_pool< M >::pool_[ 41 ] =
  59. {
  60. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  61. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  62. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  63. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  64. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  65. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  66. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  67. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  68. BOOST_DETAIL_SPINLOCK_INIT
  69. };
  70. } // namespace detail
  71. } // namespace boost
  72. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED