1// excerpts from http://code.google.com/p/muduo/ 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the License file. 5// 6// Author: Shuo Chen (chenshuo at chenshuo dot com) 7 8#include "EventLoop.h" 9 10#include "logging/Logging.h" 11 12#include <assert.h> 13#include <poll.h> 14 15using namespace muduo; 16 17__thread EventLoop* t_loopInThisThread = 0; 18 19EventLoop::EventLoop() 20 : looping_(false), 21 threadId_(CurrentThread::tid()) 22{ 23 LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_; 24 if (t_loopInThisThread) 25 { 26 LOG_FATAL << "Another EventLoop " << t_loopInThisThread 27 << " exists in this thread " << threadId_; 28 } 29 else 30 { 31 t_loopInThisThread = this; 32 } 33} 34 35EventLoop::~EventLoop() 36{ 37 assert(!looping_); 38 t_loopInThisThread = NULL; 39} 40 41void EventLoop::loop() 42{ 43 assert(!looping_); 44 assertInLoopThread(); 45 looping_ = true; 46 47 ::poll(NULL, 0, 5*1000); 48 49 LOG_TRACE << "EventLoop " << this << " stop looping"; 50 looping_ = false; 51} 52 53void EventLoop::abortNotInLoopThread() 54{ 55 LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this 56 << " was created in threadId_ = " << threadId_ 57 << ", current thread id = " << CurrentThread::tid(); 58} 59 60