lwm_pthreads.hpp 1.4 KB

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