1566406ccSShuo Chen // excerpts from http://code.google.com/p/muduo/ 2566406ccSShuo Chen // 3566406ccSShuo Chen // Use of this source code is governed by a BSD-style license 4566406ccSShuo Chen // that can be found in the License file. 5566406ccSShuo Chen // 6566406ccSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 7566406ccSShuo Chen 8566406ccSShuo Chen #include "EventLoop.h" 9566406ccSShuo Chen 10566406ccSShuo Chen+#include "Channel.h" 11566406ccSShuo Chen+#include "Poller.h" 12566406ccSShuo Chen+ 13566406ccSShuo Chen #include "logging/Logging.h" 14566406ccSShuo Chen 15566406ccSShuo Chen #include <assert.h> 16566406ccSShuo Chen 17566406ccSShuo Chen using namespace muduo; 18566406ccSShuo Chen 19566406ccSShuo Chen __thread EventLoop* t_loopInThisThread = 0; 20566406ccSShuo Chen+const int kPollTimeMs = 10000; 21566406ccSShuo Chen 22566406ccSShuo Chen EventLoop::EventLoop() 23566406ccSShuo Chen : looping_(false), 24566406ccSShuo Chen+ quit_(false), 25566406ccSShuo Chen threadId_(CurrentThread::tid()), 26566406ccSShuo Chen+ poller_(new Poller(this)) 27566406ccSShuo Chen { 28566406ccSShuo Chen LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_; 29566406ccSShuo Chen if (t_loopInThisThread) 30566406ccSShuo Chen { 31566406ccSShuo Chen LOG_FATAL << "Another EventLoop " << t_loopInThisThread 32566406ccSShuo Chen << " exists in this thread " << threadId_; 33566406ccSShuo Chen } 34566406ccSShuo Chen else 35566406ccSShuo Chen { 36566406ccSShuo Chen t_loopInThisThread = this; 37566406ccSShuo Chen } 38566406ccSShuo Chen } 39566406ccSShuo Chen 40566406ccSShuo Chen EventLoop::~EventLoop() 41566406ccSShuo Chen { 42566406ccSShuo Chen assert(!looping_); 43566406ccSShuo Chen t_loopInThisThread = NULL; 44566406ccSShuo Chen } 45566406ccSShuo Chen 46566406ccSShuo Chen void EventLoop::loop() 47566406ccSShuo Chen { 48566406ccSShuo Chen assert(!looping_); 49566406ccSShuo Chen assertInLoopThread(); 50566406ccSShuo Chen looping_ = true; 51566406ccSShuo Chen+ quit_ = false; 52566406ccSShuo Chen 53566406ccSShuo Chen+ while (!quit_) 54566406ccSShuo Chen+ { 55566406ccSShuo Chen+ activeChannels_.clear(); 56566406ccSShuo Chen+ poller_->poll(kPollTimeMs, &activeChannels_); 57566406ccSShuo Chen+ for (ChannelList::iterator it = activeChannels_.begin(); 58566406ccSShuo Chen+ it != activeChannels_.end(); ++it) 59566406ccSShuo Chen+ { 60566406ccSShuo Chen+ (*it)->handleEvent(); 61566406ccSShuo Chen+ } 62566406ccSShuo Chen+ } 63566406ccSShuo Chen 64566406ccSShuo Chen LOG_TRACE << "EventLoop " << this << " stop looping"; 65566406ccSShuo Chen looping_ = false; 66566406ccSShuo Chen } 67566406ccSShuo Chen 68566406ccSShuo Chen+void EventLoop::quit() 69566406ccSShuo Chen+{ 70566406ccSShuo Chen+ quit_ = true; 71566406ccSShuo Chen+ // wakeup(); 72566406ccSShuo Chen+} 73566406ccSShuo Chen+ 74566406ccSShuo Chen+void EventLoop::updateChannel(Channel* channel) 75566406ccSShuo Chen+{ 76566406ccSShuo Chen+ assert(channel->ownerLoop() == this); 77566406ccSShuo Chen+ assertInLoopThread(); 78566406ccSShuo Chen+ poller_->updateChannel(channel); 79566406ccSShuo Chen+} 80566406ccSShuo Chen+ 81566406ccSShuo Chen void EventLoop::abortNotInLoopThread() 82566406ccSShuo Chen { 83566406ccSShuo Chen LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this 84566406ccSShuo Chen << " was created in threadId_ = " << threadId_ 85566406ccSShuo Chen << ", current thread id = " << CurrentThread::tid(); 86566406ccSShuo Chen } 87566406ccSShuo Chen 88