EventLoop.cc revision cbe8e7d0
1cbe8e7d0SShuo Chen// excerpts from http://code.google.com/p/muduo/ 2cbe8e7d0SShuo Chen// 3cbe8e7d0SShuo Chen// Use of this source code is governed by a BSD-style license 4cbe8e7d0SShuo Chen// that can be found in the License file. 5cbe8e7d0SShuo Chen// 6cbe8e7d0SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 7cbe8e7d0SShuo Chen 8cbe8e7d0SShuo Chen#include "EventLoop.h" 9cbe8e7d0SShuo Chen 10cbe8e7d0SShuo Chen#include "Channel.h" 11cbe8e7d0SShuo Chen#include "Poller.h" 12cbe8e7d0SShuo Chen 13cbe8e7d0SShuo Chen#include "logging/Logging.h" 14cbe8e7d0SShuo Chen 15cbe8e7d0SShuo Chen#include <assert.h> 16cbe8e7d0SShuo Chen 17cbe8e7d0SShuo Chenusing namespace muduo; 18cbe8e7d0SShuo Chen 19cbe8e7d0SShuo Chen__thread EventLoop* t_loopInThisThread = 0; 20cbe8e7d0SShuo Chenconst int kPollTimeMs = 10000; 21cbe8e7d0SShuo Chen 22cbe8e7d0SShuo ChenEventLoop::EventLoop() 23cbe8e7d0SShuo Chen : looping_(false), 24cbe8e7d0SShuo Chen quit_(false), 25cbe8e7d0SShuo Chen threadId_(CurrentThread::tid()), 26cbe8e7d0SShuo Chen poller_(new Poller(this)) 27cbe8e7d0SShuo Chen{ 28cbe8e7d0SShuo Chen LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_; 29cbe8e7d0SShuo Chen if (t_loopInThisThread) 30cbe8e7d0SShuo Chen { 31cbe8e7d0SShuo Chen LOG_FATAL << "Another EventLoop " << t_loopInThisThread 32cbe8e7d0SShuo Chen << " exists in this thread " << threadId_; 33cbe8e7d0SShuo Chen } 34cbe8e7d0SShuo Chen else 35cbe8e7d0SShuo Chen { 36cbe8e7d0SShuo Chen t_loopInThisThread = this; 37cbe8e7d0SShuo Chen } 38cbe8e7d0SShuo Chen} 39cbe8e7d0SShuo Chen 40cbe8e7d0SShuo ChenEventLoop::~EventLoop() 41cbe8e7d0SShuo Chen{ 42cbe8e7d0SShuo Chen assert(!looping_); 43cbe8e7d0SShuo Chen t_loopInThisThread = NULL; 44cbe8e7d0SShuo Chen} 45cbe8e7d0SShuo Chen 46cbe8e7d0SShuo Chenvoid EventLoop::loop() 47cbe8e7d0SShuo Chen{ 48cbe8e7d0SShuo Chen assert(!looping_); 49cbe8e7d0SShuo Chen assertInLoopThread(); 50cbe8e7d0SShuo Chen looping_ = true; 51cbe8e7d0SShuo Chen while (!quit_) 52cbe8e7d0SShuo Chen { 53cbe8e7d0SShuo Chen activeChannels_.clear(); 54cbe8e7d0SShuo Chen poller_->poll(kPollTimeMs, &activeChannels_); 55cbe8e7d0SShuo Chen for (ChannelList::iterator it = activeChannels_.begin(); 56cbe8e7d0SShuo Chen it != activeChannels_.end(); ++it) 57cbe8e7d0SShuo Chen { 58cbe8e7d0SShuo Chen (*it)->handleEvent(); 59cbe8e7d0SShuo Chen } 60cbe8e7d0SShuo Chen } 61cbe8e7d0SShuo Chen 62cbe8e7d0SShuo Chen LOG_TRACE << "EventLoop " << this << " stop looping"; 63cbe8e7d0SShuo Chen looping_ = false; 64cbe8e7d0SShuo Chen} 65cbe8e7d0SShuo Chen 66cbe8e7d0SShuo Chenvoid EventLoop::quit() 67cbe8e7d0SShuo Chen{ 68cbe8e7d0SShuo Chen quit_ = true; 69cbe8e7d0SShuo Chen // wakeup(); 70cbe8e7d0SShuo Chen} 71cbe8e7d0SShuo Chen 72cbe8e7d0SShuo Chenvoid EventLoop::updateChannel(Channel* channel) 73cbe8e7d0SShuo Chen{ 74cbe8e7d0SShuo Chen assert(channel->ownerLoop() == this); 75cbe8e7d0SShuo Chen assertInLoopThread(); 76cbe8e7d0SShuo Chen poller_->updateChannel(channel); 77cbe8e7d0SShuo Chen} 78cbe8e7d0SShuo Chen 79cbe8e7d0SShuo Chenvoid EventLoop::abortNotInLoopThread() 80cbe8e7d0SShuo Chen{ 81cbe8e7d0SShuo Chen LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this 82cbe8e7d0SShuo Chen << " was created in threadId_ = " << threadId_ 83cbe8e7d0SShuo Chen << ", current thread id = " << CurrentThread::tid(); 84cbe8e7d0SShuo Chen} 85cbe8e7d0SShuo Chen 86