PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 370 lines | 287 code | 62 blank | 21 comment | 41 complexity | 632b77bef9c607a03f80cd66c8813c19 MD5 | raw file
  1//
  2// detail/impl/task_io_service.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_TASK_IO_SERVICE_IPP
 12#define BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_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_ASIO_HAS_IOCP)
 21
 22#include <boost/limits.hpp>
 23#include <boost/asio/detail/call_stack.hpp>
 24#include <boost/asio/detail/event.hpp>
 25#include <boost/asio/detail/reactor.hpp>
 26#include <boost/asio/detail/task_io_service.hpp>
 27
 28#include <boost/asio/detail/push_options.hpp>
 29
 30namespace boost {
 31namespace asio {
 32namespace detail {
 33
 34struct task_io_service::task_cleanup
 35{
 36  ~task_cleanup()
 37  {
 38    // Enqueue the completed operations and reinsert the task at the end of
 39    // the operation queue.
 40    lock_->lock();
 41    task_io_service_->task_interrupted_ = true;
 42    task_io_service_->op_queue_.push(*ops_);
 43    task_io_service_->op_queue_.push(&task_io_service_->task_operation_);
 44  }
 45
 46  task_io_service* task_io_service_;
 47  mutex::scoped_lock* lock_;
 48  op_queue<operation>* ops_;
 49};
 50
 51struct task_io_service::work_finished_on_block_exit
 52{
 53  ~work_finished_on_block_exit()
 54  {
 55    task_io_service_->work_finished();
 56  }
 57
 58  task_io_service* task_io_service_;
 59};
 60
 61struct task_io_service::idle_thread_info
 62{
 63  event wakeup_event;
 64  idle_thread_info* next;
 65};
 66
 67task_io_service::task_io_service(boost::asio::io_service& io_service)
 68  : boost::asio::detail::service_base<task_io_service>(io_service),
 69    mutex_(),
 70    task_(0),
 71    task_interrupted_(true),
 72    outstanding_work_(0),
 73    stopped_(false),
 74    shutdown_(false),
 75    first_idle_thread_(0)
 76{
 77  BOOST_ASIO_HANDLER_TRACKING_INIT;
 78}
 79
 80void task_io_service::init(std::size_t /*concurrency_hint*/)
 81{
 82}
 83
 84void task_io_service::shutdown_service()
 85{
 86  mutex::scoped_lock lock(mutex_);
 87  shutdown_ = true;
 88  lock.unlock();
 89
 90  // Destroy handler objects.
 91  while (!op_queue_.empty())
 92  {
 93    operation* o = op_queue_.front();
 94    op_queue_.pop();
 95    if (o != &task_operation_)
 96      o->destroy();
 97  }
 98
 99  // Reset to initial state.
100  task_ = 0;
101}
102
103void task_io_service::init_task()
104{
105  mutex::scoped_lock lock(mutex_);
106  if (!shutdown_ && !task_)
107  {
108    task_ = &use_service<reactor>(this->get_io_service());
109    op_queue_.push(&task_operation_);
110    wake_one_thread_and_unlock(lock);
111  }
112}
113
114std::size_t task_io_service::run(boost::system::error_code& ec)
115{
116  ec = boost::system::error_code();
117  if (outstanding_work_ == 0)
118  {
119    stop();
120    return 0;
121  }
122
123  call_stack<task_io_service>::context ctx(this);
124
125  idle_thread_info this_idle_thread;
126  this_idle_thread.next = 0;
127
128  mutex::scoped_lock lock(mutex_);
129
130  std::size_t n = 0;
131  for (; do_one(lock, &this_idle_thread); lock.lock())
132    if (n != (std::numeric_limits<std::size_t>::max)())
133      ++n;
134  return n;
135}
136
137std::size_t task_io_service::run_one(boost::system::error_code& ec)
138{
139  ec = boost::system::error_code();
140  if (outstanding_work_ == 0)
141  {
142    stop();
143    return 0;
144  }
145
146  call_stack<task_io_service>::context ctx(this);
147
148  idle_thread_info this_idle_thread;
149  this_idle_thread.next = 0;
150
151  mutex::scoped_lock lock(mutex_);
152
153  return do_one(lock, &this_idle_thread);
154}
155
156std::size_t task_io_service::poll(boost::system::error_code& ec)
157{
158  if (outstanding_work_ == 0)
159  {
160    stop();
161    ec = boost::system::error_code();
162    return 0;
163  }
164
165  call_stack<task_io_service>::context ctx(this);
166
167  mutex::scoped_lock lock(mutex_);
168
169  std::size_t n = 0;
170  for (; do_one(lock, 0); lock.lock())
171    if (n != (std::numeric_limits<std::size_t>::max)())
172      ++n;
173  return n;
174}
175
176std::size_t task_io_service::poll_one(boost::system::error_code& ec)
177{
178  ec = boost::system::error_code();
179  if (outstanding_work_ == 0)
180  {
181    stop();
182    return 0;
183  }
184
185  call_stack<task_io_service>::context ctx(this);
186
187  mutex::scoped_lock lock(mutex_);
188
189  return do_one(lock, 0);
190}
191
192void task_io_service::stop()
193{
194  mutex::scoped_lock lock(mutex_);
195  stop_all_threads(lock);
196}
197
198bool task_io_service::stopped() const
199{
200  mutex::scoped_lock lock(mutex_);
201  return stopped_;
202}
203
204void task_io_service::reset()
205{
206  mutex::scoped_lock lock(mutex_);
207  stopped_ = false;
208}
209
210void task_io_service::post_immediate_completion(task_io_service::operation* op)
211{
212  work_started();
213  post_deferred_completion(op);
214}
215
216void task_io_service::post_deferred_completion(task_io_service::operation* op)
217{
218  mutex::scoped_lock lock(mutex_);
219  op_queue_.push(op);
220  wake_one_thread_and_unlock(lock);
221}
222
223void task_io_service::post_deferred_completions(
224    op_queue<task_io_service::operation>& ops)
225{
226  if (!ops.empty())
227  {
228    mutex::scoped_lock lock(mutex_);
229    op_queue_.push(ops);
230    wake_one_thread_and_unlock(lock);
231  }
232}
233
234void task_io_service::abandon_operations(
235    op_queue<task_io_service::operation>& ops)
236{
237  op_queue<task_io_service::operation> ops2;
238  ops2.push(ops);
239}
240
241std::size_t task_io_service::do_one(mutex::scoped_lock& lock,
242    task_io_service::idle_thread_info* this_idle_thread)
243{
244  bool polling = !this_idle_thread;
245  bool task_has_run = false;
246  while (!stopped_)
247  {
248    if (!op_queue_.empty())
249    {
250      // Prepare to execute first handler from queue.
251      operation* o = op_queue_.front();
252      op_queue_.pop();
253      bool more_handlers = (!op_queue_.empty());
254
255      if (o == &task_operation_)
256      {
257        task_interrupted_ = more_handlers || polling;
258
259        // If the task has already run and we're polling then we're done.
260        if (task_has_run && polling)
261        {
262          task_interrupted_ = true;
263          op_queue_.push(&task_operation_);
264          return 0;
265        }
266        task_has_run = true;
267
268        if (!more_handlers || !wake_one_idle_thread_and_unlock(lock))
269          lock.unlock();
270
271        op_queue<operation> completed_ops;
272        task_cleanup c = { this, &lock, &completed_ops };
273        (void)c;
274
275        // Run the task. May throw an exception. Only block if the operation
276        // queue is empty and we're not polling, otherwise we want to return
277        // as soon as possible.
278        task_->run(!more_handlers && !polling, completed_ops);
279      }
280      else
281      {
282        if (more_handlers)
283          wake_one_thread_and_unlock(lock);
284        else
285          lock.unlock();
286
287        // Ensure the count of outstanding work is decremented on block exit.
288        work_finished_on_block_exit on_exit = { this };
289        (void)on_exit;
290
291        // Complete the operation. May throw an exception.
292        o->complete(*this); // deletes the operation object
293
294        return 1;
295      }
296    }
297    else if (this_idle_thread)
298    {
299      // Nothing to run right now, so just wait for work to do.
300      this_idle_thread->next = first_idle_thread_;
301      first_idle_thread_ = this_idle_thread;
302      this_idle_thread->wakeup_event.clear(lock);
303      this_idle_thread->wakeup_event.wait(lock);
304    }
305    else
306    {
307      return 0;
308    }
309  }
310
311  return 0;
312}
313
314void task_io_service::stop_all_threads(
315    mutex::scoped_lock& lock)
316{
317  stopped_ = true;
318
319  while (first_idle_thread_)
320  {
321    idle_thread_info* idle_thread = first_idle_thread_;
322    first_idle_thread_ = idle_thread->next;
323    idle_thread->next = 0;
324    idle_thread->wakeup_event.signal(lock);
325  }
326
327  if (!task_interrupted_ && task_)
328  {
329    task_interrupted_ = true;
330    task_->interrupt();
331  }
332}
333
334bool task_io_service::wake_one_idle_thread_and_unlock(
335    mutex::scoped_lock& lock)
336{
337  if (first_idle_thread_)
338  {
339    idle_thread_info* idle_thread = first_idle_thread_;
340    first_idle_thread_ = idle_thread->next;
341    idle_thread->next = 0;
342    idle_thread->wakeup_event.signal_and_unlock(lock);
343    return true;
344  }
345  return false;
346}
347
348void task_io_service::wake_one_thread_and_unlock(
349    mutex::scoped_lock& lock)
350{
351  if (!wake_one_idle_thread_and_unlock(lock))
352  {
353    if (!task_interrupted_ && task_)
354    {
355      task_interrupted_ = true;
356      task_->interrupt();
357    }
358    lock.unlock();
359  }
360}
361
362} // namespace detail
363} // namespace asio
364} // namespace boost
365
366#include <boost/asio/detail/pop_options.hpp>
367
368#endif // !defined(BOOST_ASIO_HAS_IOCP)
369
370#endif // BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP