spinlock_nt.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // Copyright (c) 2008 Peter Dimov
  9. //
  10. // Distributed under the Boost Software License, Version 1.0.
  11. // See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. #include <boost/assert.hpp>
  15. namespace boost
  16. {
  17. namespace detail
  18. {
  19. class spinlock
  20. {
  21. public:
  22. bool locked_;
  23. public:
  24. inline bool try_lock()
  25. {
  26. if( locked_ )
  27. {
  28. return false;
  29. }
  30. else
  31. {
  32. locked_ = true;
  33. return true;
  34. }
  35. }
  36. inline void lock()
  37. {
  38. BOOST_ASSERT( !locked_ );
  39. locked_ = true;
  40. }
  41. inline void unlock()
  42. {
  43. BOOST_ASSERT( locked_ );
  44. locked_ = false;
  45. }
  46. public:
  47. class scoped_lock
  48. {
  49. private:
  50. spinlock & sp_;
  51. scoped_lock( scoped_lock const & );
  52. scoped_lock & operator=( scoped_lock const & );
  53. public:
  54. explicit scoped_lock( spinlock & sp ): sp_( sp )
  55. {
  56. sp.lock();
  57. }
  58. ~scoped_lock()
  59. {
  60. sp_.unlock();
  61. }
  62. };
  63. };
  64. } // namespace detail
  65. } // namespace boost
  66. #define BOOST_DETAIL_SPINLOCK_INIT { false }
  67. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED