Boost logo

Boost-Commit :

From: pdimov_at_[hidden]
Date: 2008-04-06 12:53:11


Author: pdimov
Date: 2008-04-06 12:53:11 EDT (Sun, 06 Apr 2008)
New Revision: 44074
URL: http://svn.boost.org/trac/boost/changeset/44074

Log:
detail/spinlock_pool.hpp added.
Added:
   trunk/boost/detail/spinlock_pool.hpp (contents, props changed)
   trunk/libs/smart_ptr/test/spinlock_pool_test.cpp (contents, props changed)
Text files modified:
   trunk/libs/smart_ptr/test/Jamfile.v2 | 1 +
   1 files changed, 1 insertions(+), 0 deletions(-)

Added: trunk/boost/detail/spinlock_pool.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/detail/spinlock_pool.hpp 2008-04-06 12:53:11 EDT (Sun, 06 Apr 2008)
@@ -0,0 +1,85 @@
+#ifndef BOOST_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
+#define BOOST_DETAIL_SPINLOCK_POOL_HPP_INCLUDED
+
+// MS compatible compilers support #pragma once
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)
+# pragma once
+#endif
+
+//
+// boost/detail/spinlock_pool.hpp
+//
+// Copyright (c) 2008 Peter Dimov
+//
+// Distributed under the Boost Software License, Version 1.0.
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+//
+// spinlock_pool<0> is reserved for atomic<>, when/if it arrives
+// spinlock_pool<1> is reserved for shared_ptr reference counts
+// spinlock_pool<2> is reserved for shared_ptr atomic access
+//
+
+#include <boost/detail/spinlock.hpp>
+
+namespace boost
+{
+
+namespace detail
+{
+
+template< int I > class spinlock_pool
+{
+private:
+
+ static spinlock pool_[ 41 ];
+
+public:
+
+ static spinlock & spinlock_for( void * pv )
+ {
+ size_t i = reinterpret_cast< size_t >( pv ) % 41;
+ return pool_[ i ];
+ }
+
+ class scoped_lock
+ {
+ private:
+
+ spinlock & sp_;
+
+ scoped_lock( scoped_lock const & );
+ scoped_lock & operator=( scoped_lock const & );
+
+ public:
+
+ explicit scoped_lock( void * pv ): sp_( spinlock_for( pv ) )
+ {
+ sp_.lock();
+ }
+
+ ~scoped_lock()
+ {
+ sp_.unlock();
+ }
+ };
+};
+
+template< int I > spinlock spinlock_pool< I >::pool_[ 41 ] =
+{
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT, BOOST_DETAIL_SPINLOCK_INIT,
+ BOOST_DETAIL_SPINLOCK_INIT
+};
+
+} // namespace detail
+} // namespace boost
+
+#endif // #ifndef BOOST_DETAIL_SPINLOCK_POOL_HPP_INCLUDED

Modified: trunk/libs/smart_ptr/test/Jamfile.v2
==============================================================================
--- trunk/libs/smart_ptr/test/Jamfile.v2 (original)
+++ trunk/libs/smart_ptr/test/Jamfile.v2 2008-04-06 12:53:11 EDT (Sun, 06 Apr 2008)
@@ -42,5 +42,6 @@
           [ run spinlock_test.cpp ]
           [ run spinlock_try_test.cpp ]
           [ run spinlock_try_test.cpp : : : <threading>multi : spinlock_try_test.mt ]
+ [ run spinlock_pool_test.cpp ]
         ;
 }

Added: trunk/libs/smart_ptr/test/spinlock_pool_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/smart_ptr/test/spinlock_pool_test.cpp 2008-04-06 12:53:11 EDT (Sun, 06 Apr 2008)
@@ -0,0 +1,30 @@
+//
+// spinlock_pool_test.cpp
+//
+// Copyright 2008 Peter Dimov
+//
+// Distributed under the Boost Software License, Version 1.0.
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+//
+
+#include <boost/detail/spinlock_pool.hpp>
+
+// Sanity check only
+
+int main()
+{
+ int x = 0;
+
+ {
+ boost::detail::spinlock_pool<0>::scoped_lock lock( &x );
+ ++x;
+ }
+
+ {
+ boost::detail::spinlock_pool<1>::scoped_lock lock( &x );
+ boost::detail::spinlock_pool<2>::scoped_lock lock2( &x );
+ }
+
+ return 0;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk