lwm_pthreads.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef BOOST_SMART_PTR_DETAIL_LWM_PTHREADS_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_LWM_PTHREADS_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/lwm_pthreads.hpp
  9. //
  10. // Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
  11. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. #include <pthread.h>
  17. namespace boost
  18. {
  19. namespace detail
  20. {
  21. class lightweight_mutex
  22. {
  23. private:
  24. pthread_mutex_t m_;
  25. lightweight_mutex(lightweight_mutex const &);
  26. lightweight_mutex & operator=(lightweight_mutex const &);
  27. public:
  28. lightweight_mutex()
  29. {
  30. // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
  31. #if defined(__hpux) && defined(_DECTHREADS_)
  32. pthread_mutex_init(&m_, pthread_mutexattr_default);
  33. #else
  34. pthread_mutex_init(&m_, 0);
  35. #endif
  36. }
  37. ~lightweight_mutex()
  38. {
  39. pthread_mutex_destroy(&m_);
  40. }
  41. class scoped_lock;
  42. friend class scoped_lock;
  43. class scoped_lock
  44. {
  45. private:
  46. pthread_mutex_t & m_;
  47. scoped_lock(scoped_lock const &);
  48. scoped_lock & operator=(scoped_lock const &);
  49. public:
  50. scoped_lock(lightweight_mutex & m): m_(m.m_)
  51. {
  52. pthread_mutex_lock(&m_);
  53. }
  54. ~scoped_lock()
  55. {
  56. pthread_mutex_unlock(&m_);
  57. }
  58. };
  59. };
  60. } // namespace detail
  61. } // namespace boost
  62. #endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_PTHREADS_HPP_INCLUDED