muduo源码分析:EventLoop类 (核心)

4.EventLoop类的实现

EventLoop类实现了reactor的基本模式 ,它的数据定义如下:

  void abortNotInLoopThread();				//不在主I/O线程 
  void handleRead();  // waked up			//将事件通知描述符里的内容读走,以便让其继续检测事件通知
  void doPendingFunctors();			        //执行转交给I/O的任务 

  void printActiveChannels() const; // DEBUG	        //将发生的事件写入日志

  typedef std::vector<Channel*> ChannelList;		//事件分发器列表

  bool looping_; /* atomic */				//是否运行
  bool quit_; /* atomic and shared between threads, okay on x86, I guess. */	//是否退出事件循环
  bool eventHandling_; /* atomic */
  bool callingPendingFunctors_; /* atomic */
  int64_t iteration_;					//事件循环的次数
  const pid_t threadId_;				//运行loop的线程ID
  Timestamp pollReturnTime_;				//poll阻塞的时间
  boost::scoped_ptr<Poller> poller_;			//IO复用
  boost::scoped_ptr<TimerQueue> timerQueue_;		//定时器队列
  int wakeupFd_;					//唤醒套接字
  // unlike in TimerQueue, which is an internal class,
  // we don't expose Channel to client.
  boost::scoped_ptr<Channel> wakeupChannel_;		//封装事件描述符
  boost::any context_;

  // scratch variables
  ChannelList activeChannels_;				//活跃的事件集
  Channel* currentActiveChannel_;			//当前处理的事件集

  mutable MutexLock mutex_;				//互斥锁
  std::vector<Functor> pendingFunctors_ GUARDED_BY(mutex_);	//需要在主I/O线程执行的任务

EventLoop通过boost库下的智能指针scoped_ptr来管理Poller_,TimerQueue_,wakeupChannel_对象,这样不容易发生内存显露,其中变量pendingFunctors_为需要在I/O线程中执行的任务集,例如上面所讲的定时器的增删接口的执行,就会先放在此集合里,然后有主I/O线程来执行,那么主线程在调用loop函数之后会阻塞在poller函数中,此时我们应该如何唤醒I/O线程呢?muduo中采用了linux的新特性eventfd来唤醒I/O线程。

 

EventLoop的主要功能如下:

1.首先我们应该调用updateChannel来添加一些事件(内部调用poller->updateChannel()来添加注册事件)
2.接着调用loop函数来执行事件循环,在执行事件循环的过程中,会则色在poller->poll调用处,Poller类会把活跃的事件放在activeChannel集合中
3.然后调用Channel中的handleEvent来处理事件发生时对应的回调函数,处理完事件函数后还会处理必须由I/O线程来完成的doPendingFunctors函数

 

当然我们可以在中间的过程中注册一些普通事件或通过run*类函数来注册定时事件,我们也可以调用updateChannel和removeChannel来增删Channel。

 

EventLoop的完整源码如下:

EventLoop.h:

#ifndef MUDUO_NET_EVENTLOOP_H
#define MUDUO_NET_EVENTLOOP_H

#include <vector>

#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>

#include <muduo/base/Mutex.h>
#include <muduo/base/CurrentThread.h>
#include <muduo/base/Timestamp.h>
#incl
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值