spinlock_pt.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_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 <pthread.h>
  15. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  16. #include <boost/config/pragma_message.hpp>
  17. BOOST_PRAGMA_MESSAGE("Using pthread_mutex spinlock emulation")
  18. #endif
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. class spinlock
  24. {
  25. public:
  26. pthread_mutex_t v_;
  27. public:
  28. bool try_lock()
  29. {
  30. return pthread_mutex_trylock( &v_ ) == 0;
  31. }
  32. void lock()
  33. {
  34. pthread_mutex_lock( &v_ );
  35. }
  36. void unlock()
  37. {
  38. pthread_mutex_unlock( &v_ );
  39. }
  40. public:
  41. class scoped_lock
  42. {
  43. private:
  44. spinlock & sp_;
  45. scoped_lock( scoped_lock const & );
  46. scoped_lock & operator=( scoped_lock const & );
  47. public:
  48. explicit scoped_lock( spinlock & sp ): sp_( sp )
  49. {
  50. sp.lock();
  51. }
  52. ~scoped_lock()
  53. {
  54. sp_.unlock();
  55. }
  56. };
  57. };
  58. } // namespace detail
  59. } // namespace boost
  60. #define BOOST_DETAIL_SPINLOCK_INIT { PTHREAD_MUTEX_INITIALIZER }
  61. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SPINLOCK_PT_HPP_INCLUDED