I. Introduction to the reactor model
Reactor释义“反应堆”,是一种事件驱动机制。和普通函数调用的不同之处在于:应用程序不是主动的调用某个API完成处理,而是恰恰相反,Reactor逆置了事件处理流程,应用程序需要提供相应的接口并注册到Reactor上,如果相应的时间发生,Reactor将主动调用应用程序注册的接口,这些接口又称为“回调函数”。
Two. Implementation of Moduo Library reactor mode
Muduo mainly through 3 classes to achieve Reactor mode: Eventloop,channel,poller.
1. EventLoop
事件循环。moduo的线程模型为one loop per thread,即每个线程只能有一个EventLoop对象。EventLoop对象的生命周期通常和其所属的线程一样长。
Data members:
Const pid_t threadid_; Save current EventLoop thread ID
Boost::scoped_ptr Poller_; Implement I/O multiplexing boost::scoped_ptr timerqueue_;
int wakeupfd_;
Boost::scoped_ptr Wakeupchannel_; Used to process readable events on wakeupfd_, distributing events to Handlread () channellist Activechannels_; Channel channel* currentactivechannel_ with event readiness;
Mutexlock mutex_; Pendingfunctors_ back to other threads, so need to lock std::vectorpendingfunctors_;
Main function functions:
Loop (), in which the following procedure is looped: Call Poller::p oll (), get a Vector<channel*>activechannels_ Ready event collection by this call, and then traverse the container. Execute each channel's channel::handleevent () to complete the corresponding Ready event callback, and finally execute the Pendingfunctors_ queued function. The above cycle is a reactor mode completion.
Runinloop (Boost::function<void () >), implement user-specified task callback, if eventloop subordinate thread calls Eventloop::runinloop () then EventLoop execute immediately; If other thread calls execute Eventloop::queueinloop (boost::function<void () > Add the task to the queue (thread transfer). How does EventLoop get the fact that there is a task? Inter-thread communication can be achieved through EVENTFD: Other threads Add task T to eventloop::vector<boost::function<void () > >, and then through EventLoop:: Wakeup () writes to EVENTFD a INT,EVENTFD callback function eventloop::handleread () reads this int, which is equivalent to EventLoop being woken up, where loops traverse the queue to perform a stacked task. The use of channel management Eventfd,poller listening EVENTFD embodies the advantage that EVENTFD can unify event sources.
Queueinloop (functor& CB), the CB is placed in the queue and, if necessary, wakes up the IO thread. There are two situations in which the IO thread needs to be woken up, the thread that calls Queueinloop () is not an IO thread, and 2 of the thread that calls Queueinloop () is an IO thread, and pengding functor is being called.
2. Channel
事件分发器。每个Channel只属于一个EventLoop,每个Channel只负责一个文件描述符fd的IO事件分发,但其不拥有fd。
Data members:
int Fd_ file descriptor,
int Events_ File Descriptor Registration event,
int Revents_ ready event for file descriptor, set by Poller::p Oll
Readcallback_,writecallback ... Various event callbacks are registered in the constructor that owns the channel class, for example, Tcpconnction is registered with the constructor Tcpconnection::handlread () to Channel::readcallback
Main function functions:
SetCallback () series function that accepts the class to which the channel belongs registers the corresponding event callback function
enablereading (), update (), when an FD wants to register a readable event, first through Channel::enablereading ()-->channel::update (this), The Eventloop::updatechannel (Channel)->poller::updatechannel (channel*) call chain registers or modifies the registration event to the listening event table called by the poll system.
Handleevent (), channel as is the event dispatcher whose core structure is channel::handleevent (), the function called Channel::handleeventwithguard (), in which the channel is based: The Revents value distribution invokes the corresponding event callback.
3. Poller
Poller是IO multiplexing的封装,封装了poll和epoll。Poller是EventLoop的间接成员,只供拥有该Poller的EventLoop在IO线程调用。生命期与EventLoop相等。
Data members:
The vector POLLFDS_ event structure array is used for the first parameter of the poll;
map<int,channel*> channels_ for file descriptors FD to channel mapping facilitates quick discovery of the corresponding channel
Main function functions:
Updatechannel (channel*) is used to register an event of incoming Channel concern to Poller.
Poll (int timeoutms,vector<channel*> activechannels) calls the poll listener event collection to invoke the channel to which the Ready event belongs Fillactivechannels () Added to the Activechannels_.
Other classes
Eventloopthread: Initiates a thread that executes a eventloop that matches the semantics of "one loop per thread". Note that the mutex and condition variables are used, because thread a creates a Eventloopthread object after a thread that runs EventLoop is already created and can be Eventloopthread::startloop () Gets the EventLoop object, but an error occurs if the EventLoop thread has not been created yet. So when the EventLoop is created, condititon.notify () notifies thread A, thread A calls Eventloopthread::startloop () and calls Condition.wai () waits, This ensures that a eventloop is created. After all, a eventloop thread created by thread A may also invoke EventLoop to perform some task callbacks.
Transferred from: http://www.cnblogs.com/nicganon/p/3741609.html
Muduo Library Source Code Analysis (a) reactor mode