EventLoop.h 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#ifndef MUDUO_NET_EVENTLOOP_H 9cc7f415cSShuo Chen#define MUDUO_NET_EVENTLOOP_H 10cc7f415cSShuo Chen 11cc7f415cSShuo Chen#include "datetime/Timestamp.h" 12cc7f415cSShuo Chen#include "thread/Thread.h" 13cc7f415cSShuo Chen#include "Callbacks.h" 14cc7f415cSShuo Chen#include "TimerId.h" 15cc7f415cSShuo Chen 16cc7f415cSShuo Chen#include <boost/scoped_ptr.hpp> 17cc7f415cSShuo Chen#include <vector> 18cc7f415cSShuo Chen 19cc7f415cSShuo Chennamespace muduo 20cc7f415cSShuo Chen{ 21cc7f415cSShuo Chen 22cc7f415cSShuo Chenclass Channel; 23cc7f415cSShuo Chenclass Poller; 24cc7f415cSShuo Chenclass TimerQueue; 25cc7f415cSShuo Chen 26cc7f415cSShuo Chenclass EventLoop : boost::noncopyable 27cc7f415cSShuo Chen{ 28cc7f415cSShuo Chen public: 29cc7f415cSShuo Chen 30cc7f415cSShuo Chen EventLoop(); 31cc7f415cSShuo Chen 32cc7f415cSShuo Chen // force out-line dtor, for scoped_ptr members. 33cc7f415cSShuo Chen ~EventLoop(); 34cc7f415cSShuo Chen 35cc7f415cSShuo Chen /// 36cc7f415cSShuo Chen /// Loops forever. 37cc7f415cSShuo Chen /// 38cc7f415cSShuo Chen /// Must be called in the same thread as creation of the object. 39cc7f415cSShuo Chen /// 40cc7f415cSShuo Chen void loop(); 41cc7f415cSShuo Chen 42cc7f415cSShuo Chen void quit(); 43cc7f415cSShuo Chen 44cc7f415cSShuo Chen /// 45cc7f415cSShuo Chen /// Time when poll returns, usually means data arrivial. 46cc7f415cSShuo Chen /// 47cc7f415cSShuo Chen Timestamp pollReturnTime() const { return pollReturnTime_; } 48cc7f415cSShuo Chen 49cc7f415cSShuo Chen // timers 50cc7f415cSShuo Chen 51cc7f415cSShuo Chen /// 52cc7f415cSShuo Chen /// Runs callback at 'time'. 53cc7f415cSShuo Chen /// 54cc7f415cSShuo Chen TimerId runAt(const Timestamp& time, const TimerCallback& cb); 55cc7f415cSShuo Chen /// 56cc7f415cSShuo Chen /// Runs callback after @c delay seconds. 57cc7f415cSShuo Chen /// 58cc7f415cSShuo Chen TimerId runAfter(double delay, const TimerCallback& cb); 59cc7f415cSShuo Chen /// 60cc7f415cSShuo Chen /// Runs callback every @c interval seconds. 61cc7f415cSShuo Chen /// 62cc7f415cSShuo Chen TimerId runEvery(double interval, const TimerCallback& cb); 63cc7f415cSShuo Chen 64cc7f415cSShuo Chen // void cancel(TimerId timerId); 65cc7f415cSShuo Chen 66cc7f415cSShuo Chen // internal use only 67cc7f415cSShuo Chen void updateChannel(Channel* channel); 68cc7f415cSShuo Chen // void removeChannel(Channel* channel); 69cc7f415cSShuo Chen 70cc7f415cSShuo Chen void assertInLoopThread() 71cc7f415cSShuo Chen { 72cc7f415cSShuo Chen if (!isInLoopThread()) 73cc7f415cSShuo Chen { 74cc7f415cSShuo Chen abortNotInLoopThread(); 75cc7f415cSShuo Chen } 76cc7f415cSShuo Chen } 77cc7f415cSShuo Chen 78cc7f415cSShuo Chen bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); } 79cc7f415cSShuo Chen 80cc7f415cSShuo Chen private: 81cc7f415cSShuo Chen 82cc7f415cSShuo Chen typedef std::vector<Channel*> ChannelList; 83cc7f415cSShuo Chen 84cc7f415cSShuo Chen void abortNotInLoopThread(); 85cc7f415cSShuo Chen 86cc7f415cSShuo Chen bool looping_; /* atomic */ 87cc7f415cSShuo Chen bool quit_; /* atomic */ 88cc7f415cSShuo Chen const pid_t threadId_; 89cc7f415cSShuo Chen Timestamp pollReturnTime_; 90cc7f415cSShuo Chen boost::scoped_ptr<Poller> poller_; 91cc7f415cSShuo Chen boost::scoped_ptr<TimerQueue> timerQueue_; 92cc7f415cSShuo Chen ChannelList activeChannels_; 93cc7f415cSShuo Chen 94cc7f415cSShuo Chen}; 95cc7f415cSShuo Chen 96cc7f415cSShuo Chen} 97cc7f415cSShuo Chen 98cc7f415cSShuo Chen#endif // MUDUO_NET_EVENTLOOP_H 99