EventLoop.cc revision 4b7a333e
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
8c903e3e8SShuo Chen#include "EventLoop.h"
9c903e3e8SShuo Chen
104b7a333eSShuo Chen#include "Channel.h"
114b7a333eSShuo Chen#include "Poller.h"
124b7a333eSShuo Chen
13c903e3e8SShuo Chen#include "logging/Logging.h"
14c903e3e8SShuo Chen
15c903e3e8SShuo Chen#include <assert.h>
16c903e3e8SShuo Chen
17c903e3e8SShuo Chenusing namespace muduo;
18c903e3e8SShuo Chen
19c903e3e8SShuo Chen__thread EventLoop* t_loopInThisThread = 0;
204b7a333eSShuo Chenconst int kPollTimeMs = 10000;
21c903e3e8SShuo Chen
22c903e3e8SShuo ChenEventLoop::EventLoop()
23c903e3e8SShuo Chen  : looping_(false),
244b7a333eSShuo Chen    threadId_(CurrentThread::tid()),
254b7a333eSShuo Chen    poller_(new Poller(this))
26c903e3e8SShuo Chen{
27c903e3e8SShuo Chen  LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
28c903e3e8SShuo Chen  if (t_loopInThisThread)
29c903e3e8SShuo Chen  {
30c903e3e8SShuo Chen    LOG_FATAL << "Another EventLoop " << t_loopInThisThread
31c903e3e8SShuo Chen              << " exists in this thread " << threadId_;
32c903e3e8SShuo Chen  }
33c903e3e8SShuo Chen  else
34c903e3e8SShuo Chen  {
35c903e3e8SShuo Chen    t_loopInThisThread = this;
36c903e3e8SShuo Chen  }
37c903e3e8SShuo Chen}
38c903e3e8SShuo Chen
39c903e3e8SShuo ChenEventLoop::~EventLoop()
40c903e3e8SShuo Chen{
41c903e3e8SShuo Chen  assert(!looping_);
42c903e3e8SShuo Chen  t_loopInThisThread = NULL;
43c903e3e8SShuo Chen}
44c903e3e8SShuo Chen
45c903e3e8SShuo Chenvoid EventLoop::loop()
46c903e3e8SShuo Chen{
47c903e3e8SShuo Chen  assert(!looping_);
48c903e3e8SShuo Chen  assertInLoopThread();
49c903e3e8SShuo Chen  looping_ = true;
504b7a333eSShuo Chen  while (!quit_)
514b7a333eSShuo Chen  {
524b7a333eSShuo Chen    activeChannels_.clear();
534b7a333eSShuo Chen    poller_->poll(kPollTimeMs, &activeChannels_);
544b7a333eSShuo Chen    for (ChannelList::iterator it = activeChannels_.begin();
554b7a333eSShuo Chen        it != activeChannels_.end(); ++it)
564b7a333eSShuo Chen    {
574b7a333eSShuo Chen      (*it)->handleEvent();
584b7a333eSShuo Chen    }
594b7a333eSShuo Chen  }
60c903e3e8SShuo Chen
61c903e3e8SShuo Chen  LOG_TRACE << "EventLoop " << this << " stop looping";
62c903e3e8SShuo Chen  looping_ = false;
63c903e3e8SShuo Chen}
64c903e3e8SShuo Chen
654b7a333eSShuo Chenvoid EventLoop::quit()
664b7a333eSShuo Chen{
674b7a333eSShuo Chen  quit_ = true;
684b7a333eSShuo Chen  // wakeup();
694b7a333eSShuo Chen}
704b7a333eSShuo Chen
714b7a333eSShuo Chenvoid EventLoop::updateChannel(Channel* channel)
724b7a333eSShuo Chen{
734b7a333eSShuo Chen  assert(channel->ownerLoop() == this);
744b7a333eSShuo Chen  assertInLoopThread();
754b7a333eSShuo Chen  poller_->updateChannel(channel);
764b7a333eSShuo Chen}
774b7a333eSShuo Chen
784b7a333eSShuo Chenvoid EventLoop::removeChannel(Channel* channel)
794b7a333eSShuo Chen{
804b7a333eSShuo Chen  assert(channel->ownerLoop() == this);
814b7a333eSShuo Chen  assertInLoopThread();
824b7a333eSShuo Chen  poller_->removeChannel(channel);
834b7a333eSShuo Chen}
844b7a333eSShuo Chen
85c903e3e8SShuo Chenvoid EventLoop::abortNotInLoopThread()
86c903e3e8SShuo Chen{
87c903e3e8SShuo Chen  LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
88c903e3e8SShuo Chen            << " was created in threadId_ = " << threadId_
89c903e3e8SShuo Chen            << ", current thread id = " <<  CurrentThread::tid();
90c903e3e8SShuo Chen}
91c903e3e8SShuo Chen
92