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; 51e44bbe65SShuo Chen quit_ = false; 52e44bbe65SShuo Chen 534b7a333eSShuo Chen while (!quit_) 544b7a333eSShuo Chen { 554b7a333eSShuo Chen activeChannels_.clear(); 564b7a333eSShuo Chen poller_->poll(kPollTimeMs, &activeChannels_); 574b7a333eSShuo Chen for (ChannelList::iterator it = activeChannels_.begin(); 584b7a333eSShuo Chen it != activeChannels_.end(); ++it) 594b7a333eSShuo Chen { 604b7a333eSShuo Chen (*it)->handleEvent(); 614b7a333eSShuo Chen } 624b7a333eSShuo Chen } 63c903e3e8SShuo Chen 64c903e3e8SShuo Chen LOG_TRACE << "EventLoop " << this << " stop looping"; 65c903e3e8SShuo Chen looping_ = false; 66c903e3e8SShuo Chen} 67c903e3e8SShuo Chen 684b7a333eSShuo Chenvoid EventLoop::quit() 694b7a333eSShuo Chen{ 704b7a333eSShuo Chen quit_ = true; 714b7a333eSShuo Chen // wakeup(); 724b7a333eSShuo Chen} 734b7a333eSShuo Chen 744b7a333eSShuo Chenvoid EventLoop::updateChannel(Channel* channel) 754b7a333eSShuo Chen{ 764b7a333eSShuo Chen assert(channel->ownerLoop() == this); 774b7a333eSShuo Chen assertInLoopThread(); 784b7a333eSShuo Chen poller_->updateChannel(channel); 794b7a333eSShuo Chen} 804b7a333eSShuo Chen 81c903e3e8SShuo Chenvoid EventLoop::abortNotInLoopThread() 82c903e3e8SShuo Chen{ 83c903e3e8SShuo Chen LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this 84c903e3e8SShuo Chen << " was created in threadId_ = " << threadId_ 85c903e3e8SShuo Chen << ", current thread id = " << CurrentThread::tid(); 86c903e3e8SShuo Chen} 87c903e3e8SShuo Chen 88