spinlock_nt.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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