浅析boost asio中的event loop (3)

来源:互联网 发布:io域名注册 编辑:程序博客网 时间:2024/06/01 03:58

我们再来看看task_io_service的实现


task_io_service提供的最主要的接口如下:

1) run:Run the event loop until interrupted or no more work.

size_t run(boost::system::error_code& ec){ec = boost::system::error_code();//如果没有任务了,直接退出if (outstanding_work_ == 0){  stop();  return 0;}typename call_stack<task_io_service>::context ctx(this);idle_thread_info this_idle_thread;this_idle_thread.next = 0;boost::asio::detail::mutex::scoped_lock lock(mutex_);//循环执行直到完成所有任务队列中的任务size_t n = 0;for (; do_one(lock, &this_idle_thread); lock.lock())  if (n != (std::numeric_limits<size_t>::max)())++n;return n;}


2)poll:Poll for operations without blocking.

size_t poll(boost::system::error_code& ec){//如果没有任务了,直接退出if (outstanding_work_ == 0){  stop();  ec = boost::system::error_code();  return 0;}typename call_stack<task_io_service>::context ctx(this);boost::asio::detail::mutex::scoped_lock lock(mutex_);//循环执行直到完成所有任务队列中已经ready的任务,ready等价于epoll返回size_t n = 0;for (; do_one(lock, 0); lock.lock())  if (n != (std::numeric_limits<size_t>::max)())++n;return n;}


 

我们可以看到这两个函数最终都是调用do_one函数,差别只是第二个参数

size_t do_one(boost::asio::detail::mutex::scoped_lock& lock,      idle_thread_info* this_idle_thread){//this_idle_thread == NULL时是polling模式bool polling = !this_idle_thread;bool task_has_run = false;while (!stopped_){  if (!op_queue_.empty())  {    // 从任务队列中获取任务// Prepare to execute first handler from queue.operation* o = op_queue_.front();op_queue_.pop();bool more_handlers = (!op_queue_.empty());//task_operation_是一个标识符,用于标识执行epoll操作,这里就是task_->runif (o == &task_operation_){  task_interrupted_ = more_handlers || polling;  // If the task has already run and we're polling then we're done.  if (task_has_run && polling)  {task_interrupted_ = true;op_queue_.push(&task_operation_);return 0;  }  task_has_run = true;  if (!more_handlers || !wake_one_idle_thread_and_unlock(lock))lock.unlock();    // c析构时,把completed_ops中的op存入op_queue_中  op_queue<operation> completed_ops;  task_cleanup c = { this, &lock, &completed_ops };  (void)c;  // 执行epoll,并将ready的op放入completed_ops  // Run the task. May throw an exception. Only block if the operation  // queue is empty and we're not polling, otherwise we want to return  // as soon as possible.  task_->run(!more_handlers && !polling, completed_ops);}else{  // 执行op_queue_中的非task_operation_任务  if (more_handlers)wake_one_thread_and_unlock(lock);  elselock.unlock();  // Ensure the count of outstanding work is decremented on block exit.  work_finished_on_block_exit on_exit = { this };  (void)on_exit;  // 执行op的complete接口,这里实际上就是执行  // async_send(const ConstBufferSequence & buffers, WriteHandler handler)中的handler  // 后面还会再讲解  // Complete the operation. May throw an exception.  o->complete(*this); // deletes the operation object  return 1;}  }  else if (this_idle_thread)  {    // 如果没有op且不是polling模式的话,线程wait// Nothing to run right now, so just wait for work to do.this_idle_thread->next = first_idle_thread_;first_idle_thread_ = this_idle_thread;this_idle_thread->wakeup_event.clear(lock);this_idle_thread->wakeup_event.wait(lock);  }  else  {return 0;  }}return 0;}


 

原创粉丝点击