EventLoop.cc revision cc7f415c
1cc7f415cSShuo Chen// excerpts from http://code.google.com/p/muduo/
2cc7f415cSShuo Chen//
3cc7f415cSShuo Chen// Use of this source code is governed by a BSD-style license
4cc7f415cSShuo Chen// that can be found in the License file.
5cc7f415cSShuo Chen//
6cc7f415cSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
7cc7f415cSShuo Chen
8cc7f415cSShuo Chen#include "EventLoop.h"
9cc7f415cSShuo Chen
10cc7f415cSShuo Chen#include "Channel.h"
11cc7f415cSShuo Chen#include "Poller.h"
12cc7f415cSShuo Chen#include "TimerQueue.h"
13cc7f415cSShuo Chen
14cc7f415cSShuo Chen#include "logging/Logging.h"
15cc7f415cSShuo Chen
16cc7f415cSShuo Chen#include <assert.h>
17cc7f415cSShuo Chen
18cc7f415cSShuo Chenusing namespace muduo;
19cc7f415cSShuo Chen
20cc7f415cSShuo Chen__thread EventLoop* t_loopInThisThread = 0;
21cc7f415cSShuo Chenconst int kPollTimeMs = 10000;
22cc7f415cSShuo Chen
23cc7f415cSShuo ChenEventLoop::EventLoop()
24cc7f415cSShuo Chen  : looping_(false),
25cc7f415cSShuo Chen    quit_(false),
26cc7f415cSShuo Chen    threadId_(CurrentThread::tid()),
27cc7f415cSShuo Chen    poller_(new Poller(this)),
28cc7f415cSShuo Chen    timerQueue_(new TimerQueue(this))
29cc7f415cSShuo Chen{
30cc7f415cSShuo Chen  LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
31cc7f415cSShuo Chen  if (t_loopInThisThread)
32cc7f415cSShuo Chen  {
33cc7f415cSShuo Chen    LOG_FATAL << "Another EventLoop " << t_loopInThisThread
34cc7f415cSShuo Chen              << " exists in this thread " << threadId_;
35cc7f415cSShuo Chen  }
36cc7f415cSShuo Chen  else
37cc7f415cSShuo Chen  {
38cc7f415cSShuo Chen    t_loopInThisThread = this;
39cc7f415cSShuo Chen  }
40cc7f415cSShuo Chen}
41cc7f415cSShuo Chen
42cc7f415cSShuo ChenEventLoop::~EventLoop()
43cc7f415cSShuo Chen{
44cc7f415cSShuo Chen  assert(!looping_);
45cc7f415cSShuo Chen  t_loopInThisThread = NULL;
46cc7f415cSShuo Chen}
47cc7f415cSShuo Chen
48cc7f415cSShuo Chenvoid EventLoop::loop()
49cc7f415cSShuo Chen{
50cc7f415cSShuo Chen  assert(!looping_);
51cc7f415cSShuo Chen  assertInLoopThread();
52cc7f415cSShuo Chen  looping_ = true;
53cc7f415cSShuo Chen  while (!quit_)
54cc7f415cSShuo Chen  {
55cc7f415cSShuo Chen    activeChannels_.clear();
56cc7f415cSShuo Chen    pollReturnTime_ = poller_->poll(kPollTimeMs, &activeChannels_);
57cc7f415cSShuo Chen    for (ChannelList::iterator it = activeChannels_.begin();
58cc7f415cSShuo Chen        it != activeChannels_.end(); ++it)
59cc7f415cSShuo Chen    {
60cc7f415cSShuo Chen      (*it)->handleEvent();
61cc7f415cSShuo Chen    }
62cc7f415cSShuo Chen  }
63cc7f415cSShuo Chen
64cc7f415cSShuo Chen  LOG_TRACE << "EventLoop " << this << " stop looping";
65cc7f415cSShuo Chen  looping_ = false;
66cc7f415cSShuo Chen}
67cc7f415cSShuo Chen
68cc7f415cSShuo Chenvoid EventLoop::quit()
69cc7f415cSShuo Chen{
70cc7f415cSShuo Chen  quit_ = true;
71cc7f415cSShuo Chen  // wakeup();
72cc7f415cSShuo Chen}
73cc7f415cSShuo Chen
74cc7f415cSShuo ChenTimerId EventLoop::runAt(const Timestamp& time, const TimerCallback& cb)
75cc7f415cSShuo Chen{
76cc7f415cSShuo Chen  return timerQueue_->addTimer(cb, time, 0.0);
77cc7f415cSShuo Chen}
78cc7f415cSShuo Chen
79cc7f415cSShuo ChenTimerId EventLoop::runAfter(double delay, const TimerCallback& cb)
80cc7f415cSShuo Chen{
81cc7f415cSShuo Chen  Timestamp time(addTime(Timestamp::now(), delay));
82cc7f415cSShuo Chen  return runAt(time, cb);
83cc7f415cSShuo Chen}
84cc7f415cSShuo Chen
85cc7f415cSShuo ChenTimerId EventLoop::runEvery(double interval, const TimerCallback& cb)
86cc7f415cSShuo Chen{
87cc7f415cSShuo Chen  Timestamp time(addTime(Timestamp::now(), interval));
88cc7f415cSShuo Chen  return timerQueue_->addTimer(cb, time, interval);
89cc7f415cSShuo Chen}
90cc7f415cSShuo Chen
91cc7f415cSShuo Chenvoid EventLoop::updateChannel(Channel* channel)
92cc7f415cSShuo Chen{
93cc7f415cSShuo Chen  assert(channel->ownerLoop() == this);
94cc7f415cSShuo Chen  assertInLoopThread();
95cc7f415cSShuo Chen  poller_->updateChannel(channel);
96cc7f415cSShuo Chen}
97cc7f415cSShuo Chen
98cc7f415cSShuo Chenvoid EventLoop::abortNotInLoopThread()
99cc7f415cSShuo Chen{
100cc7f415cSShuo Chen  LOG_FATAL << "EventLoop::abortNotInLoopThread - EventLoop " << this
101cc7f415cSShuo Chen            << " was created in threadId_ = " << threadId_
102cc7f415cSShuo Chen            << ", current thread id = " <<  CurrentThread::tid();
103cc7f415cSShuo Chen}
104cc7f415cSShuo Chen
105