EventLoop.cc revision 4792573f
14b7a333eSShuo Chen// excerpts from http://code.google.com/p/muduo/ 24b7a333eSShuo Chen// 34b7a333eSShuo Chen// Use of this source code is governed by a BSD-style license 44b7a333eSShuo Chen// that can be found in the License file. 54b7a333eSShuo Chen// 64b7a333eSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 74b7a333eSShuo Chen 8c903e3e8SShuo Chen#include "EventLoop.h" 9c903e3e8SShuo Chen 104b7a333eSShuo Chen#include "Channel.h" 114b7a333eSShuo Chen#include "Poller.h" 124b7a333eSShuo Chen 13c903e3e8SShuo Chen#include "logging/Logging.h" 14c903e3e8SShuo Chen 15c903e3e8SShuo Chen#include <assert.h> 16c903e3e8SShuo Chen 17c903e3e8SShuo Chenusing namespace muduo; 18c903e3e8SShuo Chen 19c903e3e8SShuo Chen__thread EventLoop* t_loopInThisThread = 0; 204b7a333eSShuo Chenconst int kPollTimeMs = 10000; 21c903e3e8SShuo Chen 22c903e3e8SShuo ChenEventLoop::EventLoop() 23c903e3e8SShuo Chen : looping_(false), 244792573fSShuo Chen quit_(false), 254b7a333eSShuo Chen threadId_(CurrentThread::tid()), 264b7a333eSShuo Chen poller_(new Poller(this)) 27c903e3e8SShuo Chen{ 28c903e3e8SShuo Chen LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_; 29c903e3e8SShuo Chen if (t_loopInThisThread) 30c903e3e8SShuo Chen { 31c903e3e8SShuo Chen LOG_FATAL << "Another EventLoop " << t_loopInThisThread 32c903e3e8SShuo Chen << " exists in this thread " << threadId_; 33c903e3e8SShuo Chen } 34c903e3e8SShuo Chen else 35c903e3e8SShuo Chen { 36c903e3e8SShuo Chen t_loopInThisThread = this; 37c903e3e8SShuo Chen } 38c903e3e8SShuo Chen} 39c903e3e8SShuo Chen 40c903e3e8SShuo ChenEventLoop::~EventLoop() 41c903e3e8SShuo Chen{ 42c903e3e8SShuo Chen assert(!looping_); 43c903e3e8SShuo Chen t_loopInThisThread = NULL; 44c903e3e8SShuo Chen} 45c903e3e8SShuo Chen 46c903e3e8SShuo Chenvoid EventLoop::loop() 47c903e3e8SShuo Chen{ 48c903e3e8SShuo Chen assert(!looping_); 49c903e3e8SShuo Chen assertInLoopThread(); 50c903e3e8SShuo Chen looping_ = true; 514b7a333eSShuo Chen while (!quit_) 524b7a333eSShuo Chen { 534b7a333eSShuo Chen activeChannels_.clear(); 544b7a333eSShuo Chen poller_->poll(kPollTimeMs, &activeChannels_); 554b7a333eSShuo Chen for (ChannelList::iterator it = activeChannels_.begin(); 564b7a333eSShuo Chen it != activeChannels_.end(); ++it) 574b7a333eSShuo Chen { 584b7a333eSShuo Chen (*it)->handleEvent(); 594b7a333eSShuo Chen } 604b7a333eSShuo Chen } 61c903e3e8SShuo Chen 62c903e3e8SShuo Chen LOG_TRACE << "EventLoop " << this << " stop looping"; 63c903e3e8SShuo Chen looping_ = false; 64c903e3e8SShuo Chen} 65c903e3e8SShuo Chen 664b7a333eSShuo Chenvoid EventLoop::quit() 674b7a333eSShuo Chen{ 684b7a333eSShuo Chen quit_ = true; 694b7a333eSShuo Chen // wakeup(); 704b7a333eSShuo Chen} 714b7a333eSShuo Chen 724b7a333eSShuo Chenvoid EventLoop::updateChannel(Channel* channel) 734b7a333eSShuo Chen{ 744b7a333eSShuo Chen assert(channel->ownerLoop() == this); 754b7a333eSShuo Chen assertInLoopThread(); 764b7a333eSShuo Chen poller_->updateChannel(channel); 774b7a333eSShuo Chen} 784b7a333eSShuo Chen 79c903e3e8SShuo Chenvoid EventLoop::abortNotInLoopThread() 80c903e3e8SShuo Chen{ 81c903e3e8SShuo Chen LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this 82c903e3e8SShuo Chen << " was created in threadId_ = " << threadId_ 83c903e3e8SShuo Chen << ", current thread id = " << CurrentThread::tid(); 84c903e3e8SShuo Chen} 85c903e3e8SShuo Chen 86