posix_event.hpp
Go to the documentation of this file.
00001 //
00002 // posix_event.hpp
00003 // ~~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #ifndef ASIO_DETAIL_POSIX_EVENT_HPP
00012 #define ASIO_DETAIL_POSIX_EVENT_HPP
00013 
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017 
00018 #include "asio/detail/push_options.hpp"
00019 
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023 
00024 #if defined(BOOST_HAS_PTHREADS)
00025 
00026 #include "asio/detail/push_options.hpp"
00027 #include <boost/assert.hpp>
00028 #include <boost/throw_exception.hpp>
00029 #include <pthread.h>
00030 #include "asio/detail/pop_options.hpp"
00031 
00032 #include "asio/error.hpp"
00033 #include "asio/system_error.hpp"
00034 #include "asio/detail/noncopyable.hpp"
00035 
00036 namespace asio {
00037 namespace detail {
00038 
00039 class posix_event
00040   : private noncopyable
00041 {
00042 public:
00043   // Constructor.
00044   posix_event()
00045     : signalled_(false)
00046   {
00047     int error = ::pthread_cond_init(&cond_, 0);
00048     if (error != 0)
00049     {
00050       asio::system_error e(
00051           asio::error_code(error,
00052             asio::error::get_system_category()),
00053           "event");
00054       boost::throw_exception(e);
00055     }
00056   }
00057 
00058   // Destructor.
00059   ~posix_event()
00060   {
00061     ::pthread_cond_destroy(&cond_);
00062   }
00063 
00064   // Signal the event.
00065   template <typename Lock>
00066   void signal(Lock& lock)
00067   {
00068     BOOST_ASSERT(lock.locked());
00069     (void)lock;
00070     signalled_ = true;
00071     ::pthread_cond_signal(&cond_); // Ignore EINVAL.
00072   }
00073 
00074   // Reset the event.
00075   template <typename Lock>
00076   void clear(Lock& lock)
00077   {
00078     BOOST_ASSERT(lock.locked());
00079     (void)lock;
00080     signalled_ = false;
00081   }
00082 
00083   // Wait for the event to become signalled.
00084   template <typename Lock>
00085   void wait(Lock& lock)
00086   {
00087     BOOST_ASSERT(lock.locked());
00088     while (!signalled_)
00089       ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
00090   }
00091 
00092 private:
00093   ::pthread_cond_t cond_;
00094   bool signalled_;
00095 };
00096 
00097 } // namespace detail
00098 } // namespace asio
00099 
00100 #endif // defined(BOOST_HAS_PTHREADS)
00101 
00102 #include "asio/detail/pop_options.hpp"
00103 
00104 #endif // ASIO_DETAIL_POSIX_EVENT_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:39