blob: 70549b7a0a38d6f10b41fc1219ad9b54e69028fe [file] [log] [blame]
Jeff Thompson3d613fd2013-10-15 15:39:04 -07001#ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
2#define NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
Jeff Thompsonf7d49942013-08-01 16:47:40 -07003
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
Jeff Thompson9939dcd2013-10-15 15:12:24 -070011// ndnboost/detail/spinlock_pool.hpp
Jeff Thompsonf7d49942013-08-01 16:47:40 -070012//
13// Copyright (c) 2008 Peter Dimov
14//
15// Distributed under the Boost Software License, Version 1.0.
16// See accompanying file LICENSE_1_0.txt or copy at
17// http://www.boost.org/LICENSE_1_0.txt)
18//
19// spinlock_pool<0> is reserved for atomic<>, when/if it arrives
20// spinlock_pool<1> is reserved for shared_ptr reference counts
21// spinlock_pool<2> is reserved for shared_ptr atomic access
22//
23
Jeff Thompson2277ce52013-08-01 17:34:11 -070024#include <ndnboost/config.hpp>
25#include <ndnboost/smart_ptr/detail/spinlock.hpp>
Jeff Thompsonf7d49942013-08-01 16:47:40 -070026#include <cstddef>
27
28namespace ndnboost
29{
30
31namespace detail
32{
33
34template< int I > class spinlock_pool
35{
36private:
37
38 static spinlock pool_[ 41 ];
39
40public:
41
42 static spinlock & spinlock_for( void const * pv )
43 {
44#if defined(__VMS) && __INITIAL_POINTER_SIZE == 64
45 std::size_t i = reinterpret_cast< unsigned long long >( pv ) % 41;
46#else
47 std::size_t i = reinterpret_cast< std::size_t >( pv ) % 41;
48#endif
49 return pool_[ i ];
50 }
51
52 class scoped_lock
53 {
54 private:
55
56 spinlock & sp_;
57
58 scoped_lock( scoped_lock const & );
59 scoped_lock & operator=( scoped_lock const & );
60
61 public:
62
63 explicit scoped_lock( void const * pv ): sp_( spinlock_for( pv ) )
64 {
65 sp_.lock();
66 }
67
68 ~scoped_lock()
69 {
70 sp_.unlock();
71 }
72 };
73};
74
75template< int I > spinlock spinlock_pool< I >::pool_[ 41 ] =
76{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070077 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
78 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
79 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
80 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
81 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
82 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
83 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
84 NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT, NDNBOOST_DETAIL_SPINLOCK_INIT,
85 NDNBOOST_DETAIL_SPINLOCK_INIT
Jeff Thompsonf7d49942013-08-01 16:47:40 -070086};
87
88} // namespace detail
89} // namespace ndnboost
90
Jeff Thompson3d613fd2013-10-15 15:39:04 -070091#endif // #ifndef NDNBOOST_SMART_PTR_DETAIL_SPINLOCK_POOL_HPP_INCLUDED