lwm_pthreads.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <boost/assert.hpp>
  17. #include <pthread.h>
  18. namespace boost
  19. {
  20. namespace detail
  21. {
  22. class lightweight_mutex
  23. {
  24. private:
  25. pthread_mutex_t m_;
  26. lightweight_mutex(lightweight_mutex const &);
  27. lightweight_mutex & operator=(lightweight_mutex const &);
  28. public:
  29. lightweight_mutex()
  30. {
  31. // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
  32. #if defined(__hpux) && defined(_DECTHREADS_)
  33. BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 );
  34. #else
  35. BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
  36. #endif
  37. }
  38. ~lightweight_mutex()
  39. {
  40. BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
  41. }
  42. class scoped_lock;
  43. friend class scoped_lock;
  44. class scoped_lock
  45. {
  46. private:
  47. pthread_mutex_t & m_;
  48. scoped_lock(scoped_lock const &);
  49. scoped_lock & operator=(scoped_lock const &);
  50. public:
  51. scoped_lock(lightweight_mutex & m): m_(m.m_)
  52. {
  53. BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
  54. }
  55. ~scoped_lock()
  56. {
  57. BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
  58. }
  59. };
  60. };
  61. } // namespace detail
  62. } // namespace boost
  63. #endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_PTHREADS_HPP_INCLUDED