google base之IncomingTaskQueue

时间:2022-01-20 08:06:13

如同名称描述的那样,这个类就是个taskqueue,也就是任务队列,添加任务到队列,然后由MessageLoop去执行task,比较关心的函数如下:

bool IncomingTaskQueue::AddToIncomingQueue(
const tracked_objects::Location& from_here,
const Closure& task,
TimeDelta delay,
bool nestable) {
AutoLock locked(incoming_queue_lock_);
PendingTask pending_task(
from_here, task, CalculateDelayedRuntime(delay), nestable);
return PostPendingTask(&pending_task);
}

将closeure封装到了 PendingTask 这个里边值得注意的是 from_here 这个其实就是个位置信息,记录了当前代码所在文件,代码所在的行,这个主要是为了追踪,不得不说google在这方面还是考虑得非常周全的。下面看PostPendingTask(&pending_task);这个函数;

bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) {
// Warning: Don't try to short-circuit, and handle this thread's tasks more
// directly, as it could starve handling of foreign threads. Put every task
// into this queue. // This should only be called while the lock is taken.
incoming_queue_lock_.AssertAcquired(); if (!message_loop_) {
pending_task->task.Reset();
return false;
} // Initialize the sequence number. The sequence number is used for delayed
// tasks (to faciliate FIFO sorting when two tasks have the same
// delayed_run_time value) and for identifying the task in about:tracing.
pending_task->sequence_num = next_sequence_num_++; message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask",
*pending_task); bool was_empty = incoming_queue_.empty();
incoming_queue_.push(*pending_task);
pending_task->task.Reset(); // Wake up the pump.
message_loop_->ScheduleWork(was_empty); return true;
}

很显然,将task放到队列,然后调用 message_loop_->ScheduleWork(was_empty);  也就是执行计划任务,代码如下

void MessageLoop::ScheduleWork(bool was_empty) {
if (was_empty || AlwaysNotifyPump(type_))
pump_->ScheduleWork();
}

又转交给了pump

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

这个也简单,就是对事件置信,为什么要这么做呢,这个要看pump的代码才知道,因为在没有任务的时候会一直在event上面等侍,当置信了以后又开始循环值行任务。