spinlock_pool.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 I > class spinlock_pool
  28. {
  29. private:
  30. static spinlock pool_[ 41 ];
  31. public:
  32. static spinlock & spinlock_for( void const * pv )
  33. {
  34. std::size_t i = reinterpret_cast< std::size_t >( pv ) % 41;
  35. return pool_[ i ];
  36. }
  37. class scoped_lock
  38. {
  39. private:
  40. spinlock & sp_;
  41. scoped_lock( scoped_lock const & );
  42. scoped_lock & operator=( scoped_lock const & );
  43. public:
  44. explicit scoped_lock( void const * pv ): sp_( spinlock_for( pv ) )
  45. {
  46. sp_.lock();
  47. }
  48. ~scoped_lock()
  49. {
  50. sp_.unlock();
  51. }
  52. };
  53. };
  54. template< int I > spinlock spinlock_pool< I >::pool_[ 41 ] =
  55. {
  56. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  57. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  58. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  59. BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
  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
  65. };
  66. } // namespace detail
  67. } // namespace boost
  68. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED