spinlock_gcc_arm.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED
  3. //
  4. // Copyright (c) 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. #include <boost/smart_ptr/detail/yield_k.hpp>
  11. namespace boost
  12. {
  13. namespace detail
  14. {
  15. class spinlock
  16. {
  17. public:
  18. int v_;
  19. public:
  20. bool try_lock()
  21. {
  22. int r;
  23. __asm__ __volatile__(
  24. "swp %0, %1, [%2]":
  25. "=&r"( r ): // outputs
  26. "r"( 1 ), "r"( &v_ ): // inputs
  27. "memory", "cc" );
  28. return r == 0;
  29. }
  30. void lock()
  31. {
  32. for( unsigned k = 0; !try_lock(); ++k )
  33. {
  34. boost::detail::yield( k );
  35. }
  36. }
  37. void unlock()
  38. {
  39. __asm__ __volatile__( "" ::: "memory" );
  40. *const_cast< int volatile* >( &v_ ) = 0;
  41. }
  42. public:
  43. class scoped_lock
  44. {
  45. private:
  46. spinlock & sp_;
  47. scoped_lock( scoped_lock const & );
  48. scoped_lock & operator=( scoped_lock const & );
  49. public:
  50. explicit scoped_lock( spinlock & sp ): sp_( sp )
  51. {
  52. sp.lock();
  53. }
  54. ~scoped_lock()
  55. {
  56. sp_.unlock();
  57. }
  58. };
  59. };
  60. } // namespace detail
  61. } // namespace boost
  62. #define BOOST_DETAIL_SPINLOCK_INIT {0}
  63. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_GCC_ARM_HPP_INCLUDED