chromium之message_pump_default

看看头文件,默认的消息泵,该类实现了MessagePump的四个接口
class MessagePumpDefault : public MessagePump {
 public:
  MessagePumpDefault();
  ~MessagePumpDefault() {}

  // MessagePump methods:
  virtual void Run(Delegate* delegate);
  virtual void Quit();
  virtual void ScheduleWork();
  virtual void ScheduleDelayedWork(const Time& delayed_work_time);

 private:
  // This flag is set to false when Run should return.
  bool keep_running_;

  // Used to sleep until there is more work to do.
  WaitableEvent event_;

  // The time at which we should call DoDelayedWork.
  Time delayed_work_time_;

  DISALLOW_COPY_AND_ASSIGN(MessagePumpDefault);
};

 

依次分析下,MessagePumpDefault::Run

 

void MessagePumpDefault::Run(Delegate* delegate) {
  DCHECK(keep_running_) << "Quit must have been called outside of Run!";

  for (;;) {
    ScopedNSAutoreleasePool autorelease_pool; // MAC独有的,其他平台为空实现

    bool did_work = delegate->DoWork();
    if (!keep_running_)
      break;

    did_work |= delegate->DoDelayedWork(&delayed_work_time_);
    if (!keep_running_)
      break;

    if (did_work) // 确认所有的Work和DelayedWork已经做完
      continue;

    did_work = delegate->DoIdleWork(); // 只有Work和DelayWord全部完成,才做空闲的工作
    if (!keep_running_)
      break;

    if (did_work) // 保证所有的工作都做完
      continue;

    if (delayed_work_time_.is_null()) {
      event_.Wait();
    } else {
      TimeDelta delay = delayed_work_time_ - Time::Now();
      if (delay > TimeDelta()) { // 时间还没到,继续等待
        event_.TimedWait(delay);
      } else { // 时间已经超过
        // It looks like delayed_work_time_ indicates a time in the past, so we
        // need to call DoDelayedWork now.
        delayed_work_time_ = Time();
      }
    }
    // Since event_ is auto-reset, we don't need to do anything special here
    // other than service each delegate method.
  }

  keep_running_ = true;
}

 

接下来是MessagePumpDefault::Quit

设置MessagePumpDefault::Run的循环标志为false,退出循环

void MessagePumpDefault::Quit() {
  keep_running_ = false;
}

 

接下来是MessagePumpDefault::ScheduleWork

启动计划工作

void MessagePumpDefault::ScheduleWork() {
  // Since this can be called on any thread, we need to ensure that our Run
  // loop wakes up.
  event_.Signal();
}

 

最后是MessagePumpDefault::ScheduleDelayedWork

这个的意思是设置延时工作的时间,在延时多少时间后工作

void MessagePumpDefault::ScheduleDelayedWork(const Time& delayed_work_time) {
  // We know that we can't be blocked on Wait right now since this method can
  // only be called on the same thread as Run, so we only need to update our
  // record of how long to sleep when we do sleep.
  delayed_work_time_ = delayed_work_time;
}

 

posted on 2018-06-09 10:22  ckelsel  阅读(266)  评论(0编辑  收藏  举报

导航