/Src/Dependencies/Boost/boost/asio/detail/posix_event.hpp

http://hadesmem.googlecode.com/ · C++ Header · 100 lines · 66 code · 19 blank · 15 comment · 3 complexity · 1b98be9a545aaf532b31ac2aed169460 MD5 · raw file

  1. //
  2. // detail/posix_event.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_POSIX_EVENT_HPP
  11. #define BOOST_ASIO_DETAIL_POSIX_EVENT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_HAS_PTHREADS) && !defined(BOOST_ASIO_DISABLE_THREADS)
  17. #include <boost/assert.hpp>
  18. #include <pthread.h>
  19. #include <boost/asio/detail/noncopyable.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. class posix_event
  25. : private noncopyable
  26. {
  27. public:
  28. // Constructor.
  29. BOOST_ASIO_DECL posix_event();
  30. // Destructor.
  31. ~posix_event()
  32. {
  33. ::pthread_cond_destroy(&cond_);
  34. }
  35. // Signal the event.
  36. template <typename Lock>
  37. void signal(Lock& lock)
  38. {
  39. BOOST_ASSERT(lock.locked());
  40. (void)lock;
  41. signalled_ = true;
  42. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  43. }
  44. // Signal the event and unlock the mutex.
  45. template <typename Lock>
  46. void signal_and_unlock(Lock& lock)
  47. {
  48. BOOST_ASSERT(lock.locked());
  49. signalled_ = true;
  50. lock.unlock();
  51. ::pthread_cond_signal(&cond_); // Ignore EINVAL.
  52. }
  53. // Reset the event.
  54. template <typename Lock>
  55. void clear(Lock& lock)
  56. {
  57. BOOST_ASSERT(lock.locked());
  58. (void)lock;
  59. signalled_ = false;
  60. }
  61. // Wait for the event to become signalled.
  62. template <typename Lock>
  63. void wait(Lock& lock)
  64. {
  65. BOOST_ASSERT(lock.locked());
  66. while (!signalled_)
  67. ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
  68. }
  69. private:
  70. ::pthread_cond_t cond_;
  71. bool signalled_;
  72. };
  73. } // namespace detail
  74. } // namespace asio
  75. } // namespace boost
  76. #include <boost/asio/detail/pop_options.hpp>
  77. #if defined(BOOST_ASIO_HEADER_ONLY)
  78. # include <boost/asio/detail/impl/posix_event.ipp>
  79. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  80. #endif // defined(BOOST_HAS_PTHREADS) && !defined(BOOST_ASIO_DISABLE_THREADS)
  81. #endif // BOOST_ASIO_DETAIL_POSIX_EVENT_HPP