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