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 8460998e0SShuo Chen#include "EventLoop.h" 9460998e0SShuo Chen 10460998e0SShuo Chen#include "logging/Logging.h" 11460998e0SShuo Chen 12460998e0SShuo Chen#include <assert.h> 13460998e0SShuo Chen#include <poll.h> 14460998e0SShuo Chen 15460998e0SShuo Chenusing namespace muduo; 16460998e0SShuo Chen 17460998e0SShuo Chen__thread EventLoop* t_loopInThisThread = 0; 18460998e0SShuo Chen 19460998e0SShuo ChenEventLoop::EventLoop() 20460998e0SShuo Chen : looping_(false), 21460998e0SShuo Chen threadId_(CurrentThread::tid()) 22460998e0SShuo Chen{ 23460998e0SShuo Chen LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_; 24460998e0SShuo Chen if (t_loopInThisThread) 25460998e0SShuo Chen { 26460998e0SShuo Chen LOG_FATAL << "Another EventLoop " << t_loopInThisThread 27460998e0SShuo Chen << " exists in this thread " << threadId_; 28460998e0SShuo Chen } 29460998e0SShuo Chen else 30460998e0SShuo Chen { 31460998e0SShuo Chen t_loopInThisThread = this; 32460998e0SShuo Chen } 33460998e0SShuo Chen} 34460998e0SShuo Chen 35460998e0SShuo ChenEventLoop::~EventLoop() 36460998e0SShuo Chen{ 37460998e0SShuo Chen assert(!looping_); 38460998e0SShuo Chen t_loopInThisThread = NULL; 39460998e0SShuo Chen} 40460998e0SShuo Chen 41460998e0SShuo Chenvoid EventLoop::loop() 42460998e0SShuo Chen{ 43460998e0SShuo Chen assert(!looping_); 44460998e0SShuo Chen assertInLoopThread(); 45460998e0SShuo Chen looping_ = true; 46460998e0SShuo Chen 47460998e0SShuo Chen ::poll(NULL, 0, 5*1000); 48460998e0SShuo Chen 49460998e0SShuo Chen LOG_TRACE << "EventLoop " << this << " stop looping"; 50460998e0SShuo Chen looping_ = false; 51460998e0SShuo Chen} 52460998e0SShuo Chen 53460998e0SShuo Chenvoid EventLoop::abortNotInLoopThread() 54460998e0SShuo Chen{ 55460998e0SShuo Chen LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this 56460998e0SShuo Chen << " was created in threadId_ = " << threadId_ 57460998e0SShuo Chen << ", current thread id = " << CurrentThread::tid(); 58460998e0SShuo Chen} 59460998e0SShuo Chen 60