PageRenderTime 14ms CodeModel.GetById 11ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/detail/impl/pipe_select_interrupter.ipp

http://hadesmem.googlecode.com/
C++ Header | 123 lines | 93 code | 21 blank | 9 comment | 13 complexity | 1620c6ab75624f153a64c0b8affe2d8c MD5 | raw file
  1//
  2// detail/impl/pipe_select_interrupter.ipp
  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
 11#ifndef BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
 12#define BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP
 13
 14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 15# pragma once
 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 17
 18#include <boost/asio/detail/config.hpp>
 19
 20#if !defined(BOOST_WINDOWS)
 21#if !defined(__CYGWIN__)
 22#if !defined(__SYMBIAN32__)
 23#if !defined(BOOST_ASIO_HAS_EVENTFD)
 24
 25#include <fcntl.h>
 26#include <sys/stat.h>
 27#include <sys/types.h>
 28#include <unistd.h>
 29#include <boost/asio/detail/pipe_select_interrupter.hpp>
 30#include <boost/asio/detail/throw_error.hpp>
 31#include <boost/asio/error.hpp>
 32
 33#include <boost/asio/detail/push_options.hpp>
 34
 35namespace boost {
 36namespace asio {
 37namespace detail {
 38
 39pipe_select_interrupter::pipe_select_interrupter()
 40{
 41  open_descriptors();
 42}
 43
 44void pipe_select_interrupter::open_descriptors()
 45{
 46  int pipe_fds[2];
 47  if (pipe(pipe_fds) == 0)
 48  {
 49    read_descriptor_ = pipe_fds[0];
 50    ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
 51    write_descriptor_ = pipe_fds[1];
 52    ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
 53
 54#if defined(FD_CLOEXEC)
 55    ::fcntl(read_descriptor_, F_SETFD, FD_CLOEXEC);
 56    ::fcntl(write_descriptor_, F_SETFD, FD_CLOEXEC);
 57#endif // defined(FD_CLOEXEC)
 58  }
 59  else
 60  {
 61    boost::system::error_code ec(errno,
 62        boost::asio::error::get_system_category());
 63    boost::asio::detail::throw_error(ec, "pipe_select_interrupter");
 64  }
 65}
 66
 67pipe_select_interrupter::~pipe_select_interrupter()
 68{
 69  close_descriptors();
 70}
 71
 72void pipe_select_interrupter::close_descriptors()
 73{
 74  if (read_descriptor_ != -1)
 75    ::close(read_descriptor_);
 76  if (write_descriptor_ != -1)
 77    ::close(write_descriptor_);
 78}
 79
 80void pipe_select_interrupter::recreate()
 81{
 82  close_descriptors();
 83
 84  write_descriptor_ = -1;
 85  read_descriptor_ = -1;
 86
 87  open_descriptors();
 88}
 89
 90void pipe_select_interrupter::interrupt()
 91{
 92  char byte = 0;
 93  int result = ::write(write_descriptor_, &byte, 1);
 94  (void)result;
 95}
 96
 97bool pipe_select_interrupter::reset()
 98{
 99  for (;;)
100  {
101    char data[1024];
102    int bytes_read = ::read(read_descriptor_, data, sizeof(data));
103    if (bytes_read < 0 && errno == EINTR)
104      continue;
105    bool was_interrupted = (bytes_read > 0);
106    while (bytes_read == sizeof(data))
107      bytes_read = ::read(read_descriptor_, data, sizeof(data));
108    return was_interrupted;
109  }
110}
111
112} // namespace detail
113} // namespace asio
114} // namespace boost
115
116#include <boost/asio/detail/pop_options.hpp>
117
118#endif // !defined(BOOST_ASIO_HAS_EVENTFD)
119#endif // !defined(__SYMBIAN32__)
120#endif // !defined(__CYGWIN__)
121#endif // !defined(BOOST_WINDOWS)
122
123#endif // BOOST_ASIO_DETAIL_IMPL_PIPE_SELECT_INTERRUPTER_IPP