spinlock_nt.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  16. #include <boost/config/pragma_message.hpp>
  17. BOOST_PRAGMA_MESSAGE("Using single-threaded spinlock emulation")
  18. #endif
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. class spinlock
  24. {
  25. public:
  26. bool locked_;
  27. public:
  28. inline bool try_lock()
  29. {
  30. if( locked_ )
  31. {
  32. return false;
  33. }
  34. else
  35. {
  36. locked_ = true;
  37. return true;
  38. }
  39. }
  40. inline void lock()
  41. {
  42. BOOST_ASSERT( !locked_ );
  43. locked_ = true;
  44. }
  45. inline void unlock()
  46. {
  47. BOOST_ASSERT( locked_ );
  48. locked_ = false;
  49. }
  50. public:
  51. class scoped_lock
  52. {
  53. private:
  54. spinlock & sp_;
  55. scoped_lock( scoped_lock const & );
  56. scoped_lock & operator=( scoped_lock const & );
  57. public:
  58. explicit scoped_lock( spinlock & sp ): sp_( sp )
  59. {
  60. sp.lock();
  61. }
  62. ~scoped_lock()
  63. {
  64. sp_.unlock();
  65. }
  66. };
  67. };
  68. } // namespace detail
  69. } // namespace boost
  70. #define BOOST_DETAIL_SPINLOCK_INIT { false }
  71. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_NT_HPP_INCLUDED