EventLoop.cc revision c903e3e8
1c903e3e8SShuo Chen#include "EventLoop.h"
2c903e3e8SShuo Chen
3c903e3e8SShuo Chen#include "logging/Logging.h"
4c903e3e8SShuo Chen
5c903e3e8SShuo Chen#include <assert.h>
6c903e3e8SShuo Chen#include <poll.h>
7c903e3e8SShuo Chen
8c903e3e8SShuo Chenusing namespace muduo;
9c903e3e8SShuo Chen
10c903e3e8SShuo Chen__thread EventLoop* t_loopInThisThread = 0;
11c903e3e8SShuo Chen
12c903e3e8SShuo ChenEventLoop::EventLoop()
13c903e3e8SShuo Chen  : looping_(false),
14c903e3e8SShuo Chen    threadId_(CurrentThread::tid())
15c903e3e8SShuo Chen{
16c903e3e8SShuo Chen  LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
17c903e3e8SShuo Chen  if (t_loopInThisThread)
18c903e3e8SShuo Chen  {
19c903e3e8SShuo Chen    LOG_FATAL << "Another EventLoop " << t_loopInThisThread
20c903e3e8SShuo Chen              << " exists in this thread " << threadId_;
21c903e3e8SShuo Chen  }
22c903e3e8SShuo Chen  else
23c903e3e8SShuo Chen  {
24c903e3e8SShuo Chen    t_loopInThisThread = this;
25c903e3e8SShuo Chen  }
26c903e3e8SShuo Chen}
27c903e3e8SShuo Chen
28c903e3e8SShuo ChenEventLoop::~EventLoop()
29c903e3e8SShuo Chen{
30c903e3e8SShuo Chen  assert(!looping_);
31c903e3e8SShuo Chen  t_loopInThisThread = NULL;
32c903e3e8SShuo Chen}
33c903e3e8SShuo Chen
34c903e3e8SShuo Chenvoid EventLoop::loop()
35c903e3e8SShuo Chen{
36c903e3e8SShuo Chen  assert(!looping_);
37c903e3e8SShuo Chen  assertInLoopThread();
38c903e3e8SShuo Chen  looping_ = true;
39c903e3e8SShuo Chen
40c903e3e8SShuo Chen  ::poll(NULL, 0, 5*1000);
41c903e3e8SShuo Chen
42c903e3e8SShuo Chen  LOG_TRACE << "EventLoop " << this << " stop looping";
43c903e3e8SShuo Chen  looping_ = false;
44c903e3e8SShuo Chen}
45c903e3e8SShuo Chen
46c903e3e8SShuo Chenvoid EventLoop::abortNotInLoopThread()
47c903e3e8SShuo Chen{
48c903e3e8SShuo Chen  LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
49c903e3e8SShuo Chen            << " was created in threadId_ = " << threadId_
50c903e3e8SShuo Chen            << ", current thread id = " <<  CurrentThread::tid();
51c903e3e8SShuo Chen}
52c903e3e8SShuo Chen
53