Files
boost_smart_ptr/include/boost/smart_ptr/detail/spinlock_nt.hpp
T

89 lines
1.4 KiB
C++
Raw Normal View History

#ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
#define BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
2008-04-05 15:06:31 +00:00
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
//
// 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)
//
2008-04-09 17:49:20 +00:00
#include <boost/assert.hpp>
2008-04-05 15:06:31 +00:00
namespace boost
{
namespace detail
{
class spinlock
{
2008-04-09 17:49:20 +00:00
public:
bool locked_;
2008-04-05 15:06:31 +00:00
public:
inline bool try_lock()
{
2008-04-09 17:49:20 +00:00
if( locked_ )
{
return false;
}
else
{
locked_ = true;
return true;
}
2008-04-05 15:06:31 +00:00
}
inline void lock()
{
2008-04-09 17:49:20 +00:00
BOOST_ASSERT( !locked_ );
locked_ = true;
2008-04-05 15:06:31 +00:00
}
inline void unlock()
{
2008-04-09 17:49:20 +00:00
BOOST_ASSERT( locked_ );
locked_ = false;
2008-04-05 15:06:31 +00:00
}
public:
class scoped_lock
{
private:
2008-04-09 17:49:20 +00:00
spinlock & sp_;
2008-04-05 15:06:31 +00:00
scoped_lock( scoped_lock const & );
scoped_lock & operator=( scoped_lock const & );
public:
2008-04-09 17:49:20 +00:00
explicit scoped_lock( spinlock & sp ): sp_( sp )
2008-04-05 15:06:31 +00:00
{
2008-04-09 17:49:20 +00:00
sp.lock();
2008-04-05 15:06:31 +00:00
}
~scoped_lock()
{
2008-04-09 17:49:20 +00:00
sp_.unlock();
2008-04-05 15:06:31 +00:00
}
};
};
} // namespace detail
} // namespace boost
2008-04-09 17:49:20 +00:00
#define BOOST_DETAIL_SPINLOCK_INIT { false }
2008-04-05 15:06:31 +00:00
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED