• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // spinlock_try_test.cpp
3 //
4 // Copyright 2008 Peter Dimov
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #include <boost/smart_ptr/detail/spinlock.hpp>
12 #include <boost/core/lightweight_test.hpp>
13 
14 // Sanity check only
15 
16 static boost::detail::spinlock sp = BOOST_DETAIL_SPINLOCK_INIT;
17 static boost::detail::spinlock sp2 = BOOST_DETAIL_SPINLOCK_INIT;
18 
main()19 int main()
20 {
21     BOOST_TEST( sp.try_lock() );
22     BOOST_TEST( !sp.try_lock() );
23     BOOST_TEST( sp2.try_lock() );
24     BOOST_TEST( !sp.try_lock() );
25     BOOST_TEST( !sp2.try_lock() );
26     sp.unlock();
27     sp2.unlock();
28 
29     sp.lock();
30     BOOST_TEST( !sp.try_lock() );
31     sp2.lock();
32     BOOST_TEST( !sp.try_lock() );
33     BOOST_TEST( !sp2.try_lock() );
34     sp.unlock();
35     sp2.unlock();
36 
37     {
38         boost::detail::spinlock::scoped_lock lock( sp );
39         BOOST_TEST( !sp.try_lock() );
40         boost::detail::spinlock::scoped_lock lock2( sp2 );
41         BOOST_TEST( !sp.try_lock() );
42         BOOST_TEST( !sp2.try_lock() );
43     }
44 
45     return boost::report_errors();
46 }
47