注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

asio 的 io_service 的post函数的实现  

2012-02-24 17:59:24|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

没有完全搞明白,应该把调用函数存到一个operation结构里面,然后用widnows 的完成端口触发处理,如果不能马上处理,就先把operation先存到队列里面,下次再处理。


template <typename CompletionHandler>

inline void io_service::post(CompletionHandler handler)

{

  // If you get an error on the following line it means that your handler does

  // not meet the documented type requirements for a CompletionHandler.

  ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;


  impl_.post(handler);

}


template <typename Handler>

void win_iocp_io_service::post(Handler& handler)

{

  // Allocate and construct an operation to wrap the handler.

  typedef completion_handler<Handler> op;

  typename op::ptr p = { boost::addressof(handler),

    asio_handler_alloc_helpers::allocate(

      sizeof(op), handler), 0 };

  p.p = new (p.v) op(handler);


  ASIO_HANDLER_CREATION((p.p, "io_service", this, "post"));


  post_immediate_completion(p.p);

  p.v = p.p = 0;

}


  // Request invocation of the given operation and return immediately. Assumes

  // that work_started() has not yet been called for the operation.

  void post_immediate_completion(win_iocp_operation* op)

  {

    work_started();

    post_deferred_completion(op);

  }



  // Notify that some work has started.

  void work_started()

  {

    ::InterlockedIncrement(&outstanding_work_);

  }


void win_iocp_io_service::post_deferred_completion(win_iocp_operation* op)

{

  // Flag the operation as ready.

  op->ready_ = 1;


  // Enqueue the operation on the I/O completion port.

  if (!::PostQueuedCompletionStatus(iocp_.handle,

        0, overlapped_contains_result, op))             //都用完成端口去做的,不行就先放到队列里面,下次在重试

  {

    // Out of resources. Put on completed queue instead.

    mutex::scoped_lock lock(dispatch_mutex_);

    completed_ops_.push(op);

    ::InterlockedExchange(&dispatch_required_, 1);

  }

}




void win_iocp_io_service::post_deferred_completions(

    op_queue<win_iocp_operation>& ops)

{

  while (win_iocp_operation* op = ops.front())

  {


    ops.pop();


    // Flag the operation as ready.

    op->ready_ = 1;


    // Enqueue the operation on the I/O completion port.

    if (!::PostQueuedCompletionStatus(iocp_.handle,

          0, overlapped_contains_result, op))

    {

      // Out of resources. Put on completed queue instead.

      mutex::scoped_lock lock(dispatch_mutex_);

      completed_ops_.push(op);

      completed_ops_.push(ops);

      ::InterlockedExchange(&dispatch_required_, 1);

    }

  }

}




size_t win_iocp_io_service::do_one(bool block, asio::error_code& ec)

{

  for (;;)

  {

    // Try to acquire responsibility for dispatching timers and completed ops.

    if (::InterlockedCompareExchange(&dispatch_required_, 0, 1) == 1)

    {

      mutex::scoped_lock lock(dispatch_mutex_);


      // Dispatch pending timers and operations.

      op_queue<win_iocp_operation> ops;

      ops.push(completed_ops_);      //把刚才排队延时处理的队列里面的都拿处理继续处理。

      timer_queues_.get_ready_timers(ops);

      post_deferred_completions(ops);

      update_timeout();

    }







  static void do_complete(io_service_impl* owner, operation* base,

      asio::error_code /*ec*/, std::size_t /*bytes_transferred*/)

  {

    // Take ownership of the handler object.

    completion_handler* h(static_cast<completion_handler*>(base));

    ptr p = { boost::addressof(h->handler_), h, h };


    ASIO_HANDLER_COMPLETION((h));


    // Make a copy of the handler so that the memory can be deallocated before

    // the upcall is made. Even if we're not about to make an upcall, a

    // sub-object of the handler may be the true owner of the memory associated

    // with the handler. Consequently, a local copy of the handler is required

    // to ensure that any owning sub-object remains valid until after we have

    // deallocated the memory here.

    Handler handler(ASIO_MOVE_CAST(Handler)(h->handler_));

    p.h = boost::addressof(handler);

    p.reset();


    // Make the upcall if required.

    if (owner)

    {

      asio::detail::fenced_block b;

      ASIO_HANDLER_INVOCATION_BEGIN(());

      asio_handler_invoke_helpers::invoke(handler, handler);

      ASIO_HANDLER_INVOCATION_END;

    }

  }




  评论这张
 
阅读(1509)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017