trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::EventLoop Class Reference

As the name implies, this class represents an event loop that runs in a particular thread. The event loop can handle network I/O events and timers in asynchronous mode. More...

#include <trantor/net/EventLoop.h>

Inheritance diagram for trantor::EventLoop:
Collaboration diagram for trantor::EventLoop:

Public Member Functions

void loop ()
 Run the event loop. This method will be blocked until the event loop exits.
void quit ()
 Let the event loop quit.
void assertInLoopThread ()
 Assertion that the current thread is the thread to which the event loop belongs. If the assertion fails, the program aborts.
void resetAfterFork ()
 Make the event loop works after calling the fork() function.
bool isInLoopThread () const
 Return true if the current thread is the thread to which the event loop belongs.
template<typename Functor>
void runInLoop (Functor &&f)
 Run the function f in the thread of the event loop.
void queueInLoop (const Func &f)
 Run the function f in the thread of the event loop.
void queueInLoop (Func &&f)
TimerId runAt (const Date &time, const Func &cb)
 Run a function at a time point.
TimerId runAt (const Date &time, Func &&cb)
TimerId runAfter (double delay, const Func &cb)
 Run a function after a period of time.
TimerId runAfter (double delay, Func &&cb)
TimerId runAfter (const std::chrono::duration< double > &delay, const Func &cb)
 Run a function after a period of time.
TimerId runAfter (const std::chrono::duration< double > &delay, Func &&cb)
TimerId runEvery (double interval, const Func &cb)
 Repeatedly run a function every period of time.
TimerId runEvery (double interval, Func &&cb)
TimerId runEvery (const std::chrono::duration< double > &interval, const Func &cb)
 Repeatedly run a function every period of time. Users could use chrono literals to represent a time duration For example:
TimerId runEvery (const std::chrono::duration< double > &interval, Func &&cb)
void invalidateTimer (TimerId id)
 Invalidate the timer identified by the given ID.
void moveToCurrentThread ()
 Move the EventLoop to the current thread, this method must be called before the loop is running.
void updateChannel (Channel *chl)
 Update channel status. This method is usually used internally.
void removeChannel (Channel *chl)
 Remove a channel from the event loop. This method is usually used internally.
size_t index ()
 Return the index of the event loop.
void setIndex (size_t index)
 Set the index of the event loop.
bool isRunning ()
 Return true if the event loop is running.
bool isCallingFunctions ()
 Check if the event loop is calling a function.
void runOnQuit (Func &&cb)
 Run functions when the event loop quits.
void runOnQuit (const Func &cb)

Static Public Member Functions

static EventLoop * getEventLoopOfCurrentThread ()
 Get the event loop of the current thread. Return nullptr if there is no event loop in the current thread.

Detailed Description

As the name implies, this class represents an event loop that runs in a particular thread. The event loop can handle network I/O events and timers in asynchronous mode.

Note
An event loop object always belongs to a separate thread, and there is one event loop object at most in a thread. We can call an event loop object the event loop of the thread it belongs to, or call that thread the thread of the event loop.

Member Function Documentation

◆ getEventLoopOfCurrentThread()

EventLoop * trantor::EventLoop::getEventLoopOfCurrentThread ( )
static

Get the event loop of the current thread. Return nullptr if there is no event loop in the current thread.

Returns
EventLoop*

◆ index()

size_t trantor::EventLoop::index ( )
inline

Return the index of the event loop.

Returns
size_t

◆ invalidateTimer()

void trantor::EventLoop::invalidateTimer ( TimerId id)

Invalidate the timer identified by the given ID.

Parameters
idThe ID of the timer.

◆ isCallingFunctions()

bool trantor::EventLoop::isCallingFunctions ( )
inline

Check if the event loop is calling a function.

Returns
true
false

◆ isInLoopThread()

bool trantor::EventLoop::isInLoopThread ( ) const
inline

Return true if the current thread is the thread to which the event loop belongs.

Returns
true
false

◆ isRunning()

bool trantor::EventLoop::isRunning ( )
inline

Return true if the event loop is running.

Returns
true
false

◆ queueInLoop()

void trantor::EventLoop::queueInLoop ( const Func & f)

Run the function f in the thread of the event loop.

Parameters
f
Note
The difference between this method and the runInLoop() method is that the function f is executed after the method exiting no matter if the current thread is the thread of the event loop.

◆ removeChannel()

void trantor::EventLoop::removeChannel ( Channel * chl)

Remove a channel from the event loop. This method is usually used internally.

Parameters
chl

◆ runAfter() [1/2]

TimerId trantor::EventLoop::runAfter ( const std::chrono::duration< double > & delay,
const Func & cb )
inline

Run a function after a period of time.

Note
Users could use chrono literals to represent a time duration For example:
runAfter(5s, task);
runAfter(10min, task);
TimerId runAfter(double delay, const Func &cb)
Run a function after a period of time.

◆ runAfter() [2/2]

TimerId trantor::EventLoop::runAfter ( double delay,
const Func & cb )

Run a function after a period of time.

Parameters
delayRepresent the period of time in seconds.
cbThe function to run.
Returns
TimerId The ID of the timer.

◆ runAt()

TimerId trantor::EventLoop::runAt ( const Date & time,
const Func & cb )

Run a function at a time point.

Parameters
timeThe time to run the function.
cbThe function to run.
Returns
TimerId The ID of the timer.

◆ runEvery() [1/2]

TimerId trantor::EventLoop::runEvery ( const std::chrono::duration< double > & interval,
const Func & cb )
inline

Repeatedly run a function every period of time. Users could use chrono literals to represent a time duration For example:

runEvery(5s, task);
runEvery(10min, task);
runEvery(0.1h, task);
TimerId runEvery(double interval, const Func &cb)
Repeatedly run a function every period of time.

◆ runEvery() [2/2]

TimerId trantor::EventLoop::runEvery ( double interval,
const Func & cb )

Repeatedly run a function every period of time.

Parameters
intervalThe duration in seconds.
cbThe function to run.
Returns
TimerId The ID of the timer.

◆ runInLoop()

template<typename Functor>
void trantor::EventLoop::runInLoop ( Functor && f)
inline

Run the function f in the thread of the event loop.

Parameters
f
Note
If the current thread is the thread of the event loop, the function f is executed directly before the method exiting.

◆ runOnQuit()

void trantor::EventLoop::runOnQuit ( Func && cb)

Run functions when the event loop quits.

Parameters
cbthe function to run
Note
the function runs on the thread that quits the EventLoop

◆ setIndex()

void trantor::EventLoop::setIndex ( size_t index)
inline

Set the index of the event loop.

Parameters
index

◆ updateChannel()

void trantor::EventLoop::updateChannel ( Channel * chl)

Update channel status. This method is usually used internally.

Parameters
chl

The documentation for this class was generated from the following file: