// squid 的核心循环 class EventLoop { public: EventLoop(); // 注册引擎,这类引擎在每次循环中都被调用 void registerEngine(AsyncEngine *engine); // 主要运行函数 void run(); bool runOnce(); void setPrimaryEngine(AsyncEngine * engine); void setTimeService(TimeEngine *engine); // 定时器? void stop(); // 停止 int errcount; // 出错次数 private: // 设置运行状态字 void prepareToRun(); void checkEngine(AsyncEngine *engine, bool const primary); // 检测引擎 bool dispatchCalls();// 分发处理事件? bool last_loop; typedef Vector<AsyncEngine *> engine_vector; engine_vector engines; // 循环调用事件 TimeEngine * timeService; // 时间触发事件 AsyncEngine *primaryEngine; // 当前的 int loop_delay; bool error; bool runOnceResult; } // 上次循环为假,初始化时 EventLoop::EventLoop():errcount(0),last_loop(false),timeService(NULL), primaryEngine(NULL) {} void EventLoop::checkEngine(AsyncEngine *engine, bool const primary) { int requested_dalay; if (!primary) // primary 为假 requested_dalay = engine->checkEvent(0); // 为假就使用引擎指定? else requested_delay = engine->checkEvent(loop_delay); // loop delay 在运行时可能被指定为1秒== 1000毫秒 if (requested_delay < 0 ) // 小于0 ,就是超时了? { switch(requested_delay){ case AsyncEngine::EVENT_IDLE: // 引擎空闲? debugs(1,9,"Engine" << engine << "is idle"); break; case AsyncEngine::EVENT_ERROR: runOnceResult = false; error = true; // 循环里出现错误 break; default: fatal_dump("unknow AsynEngine result"); // 认为出现严重错误退出 } } else { runOnceResult = false; if (requested_delay < loop_delay) loop_delay = requested_delay; } } // 这么简单,仅仅是两个状态变量赋初值,看处理完后状态有变否 void EventLoop::prepareToRun() { last_loop = false; errcount = 0; } void EventLoop::Run() { prepareToRun(); // runOnce出错时就跳出循环 非 0 返回值就出, 0 就继续循环 // 如果链表中有fire被掉用,返回false,则继续循环 while(!runOnce()); } bool EventLoop::runOnce() { bool sawActivity = false; runOnceResult = true; error = true; loop_delay = EVENT_LOOP_TIMEOUT; // wait 赋值 AsyncEngine *waitingEngine = primaryEngine; if (!waitingEngine && !engines.empty()) // 当前无和同步不为空 waitingEngine = engines.back();// 返回最后一个元素 // 依次调用AsyncCall的链表的fire do { typedef engine_vector::iterator EVI; foe (EVI i = engines.begin(); i != engines.end(); ++i) // 剩下的都要check一次 { if (*i != waitingEngine) checkEngine(*i,false); } sawActivity = dispatchCalls();// 至少链表中有一次fire,sawActivity就为TRUE if (sawActivity) runOnceResult = false; }while(sawActivity); if (waitingEngine != NULL) { checkEngine(waitingEngine, true); // 此处就是weiting的check了 } if (timeService != NULL) timeService->tick(); sawActivity = disptchCalls(); // 实际是fire的返回结果 if (sawActivity) runOnceResult =false; if (error) { ++errcount; debugs(); } else { errcount = 0; } if (errcount == 10) return true; if (last_loop) return true; return runOnceResult; } bool EventLoop::disptchCalls() { bool dispatchedSome = AsyncCallQueue::Instance().fire(); return dispatchedSome; } // 于传入的相等的就是当前的 void EventLoop::setPrimaryEngine(AsyncEngine *engine) { for (engine_vector::iterator i = engines.begin(); i != engines.end(); ++i) { if (*i == engine) { primaryEngine = engine; } } } void EventLoop::setTimeService(TimeEngine *engine) { timeService = engine; } void EventLoop::stop() { last_loop = true; }
squid源码分析1 ----EventLoop
最新推荐文章于 2016-03-30 12:08:08 发布