22#include <trantor/exports.h>
38using ChannelList = std::vector<Channel *>;
39using Func = std::function<void()>;
40using TimerId = uint64_t;
55class TRANTOR_EXPORT EventLoop : NonCopyable
82 abortNotInLoopThread();
90 void resetTimerQueue();
107 return threadId_ == std::this_thread::get_id();
125 template <
typename Functor>
157 TimerId
runAt(
const Date &time, Func &&cb);
167 TimerId
runAfter(
double delay, Func &&cb);
178 TimerId
runAfter(
const std::chrono::duration<double> &delay,
const Func &cb)
182 TimerId runAfter(
const std::chrono::duration<double> &delay, Func &&cb)
184 return runAfter(delay.count(), std::move(cb));
195 TimerId
runEvery(
double interval, Func &&cb);
207 TimerId
runEvery(
const std::chrono::duration<double> &interval,
210 return runEvery(interval.count(), cb);
212 TimerId runEvery(
const std::chrono::duration<double> &interval, Func &&cb)
214 return runEvery(interval.count(), std::move(cb));
274 return looping_.load(std::memory_order_acquire) &&
275 (!quit_.load(std::memory_order_acquire));
286 return callingFuncs_;
299 void abortNotInLoopThread();
302 std::atomic<bool> looping_;
303 std::thread::id threadId_;
304 std::atomic<bool> quit_;
305 std::unique_ptr<Poller> poller_;
307 ChannelList activeChannels_;
308 Channel *currentActiveChannel_;
312 std::unique_ptr<TimerQueue> timerQueue_;
314 bool callingFuncs_{
false};
317 std::unique_ptr<Channel> wakeupChannelPtr_;
321 std::unique_ptr<Channel> wakeupChannelPtr_;
324 void doRunInLoopFuncs();
326 size_t index_{size_t(-1)};
328 size_t index_{std::numeric_limits<size_t>::max()};
330 EventLoop **threadLocalLoopPtr_;
This class is used to implement reactor pattern. A Channel object manages a socket fd....
Definition Channel.h:33
This class represents a time point.
Definition Date.h:28
void loop()
Run the event loop. This method will be blocked until the event loop exits.
void resetAfterFork()
Make the event loop works after calling the fork() function.
void moveToCurrentThread()
Move the EventLoop to the current thread, this method must be called before the loop is running.
void setIndex(size_t index)
Set the index of the event loop.
Definition EventLoop.h:261
TimerId runAfter(double delay, const Func &cb)
Run a function after a period of time.
void quit()
Let the event loop quit.
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 d...
Definition EventLoop.h:207
void invalidateTimer(TimerId id)
Invalidate the timer identified by the given ID.
static EventLoop * getEventLoopOfCurrentThread()
Get the event loop of the current thread. Return nullptr if there is no event loop in the current thr...
bool isRunning()
Return true if the event loop is running.
Definition EventLoop.h:272
void runInLoop(Functor &&f)
Run the function f in the thread of the event loop.
Definition EventLoop.h:126
TimerId runAfter(const std::chrono::duration< double > &delay, const Func &cb)
Run a function after a period of time.
Definition EventLoop.h:178
TimerId runAt(const Date &time, const Func &cb)
Run a function at a time point.
bool isInLoopThread() const
Return true if the current thread is the thread to which the event loop belongs.
Definition EventLoop.h:105
size_t index()
Return the index of the event loop.
Definition EventLoop.h:251
void queueInLoop(const Func &f)
Run the function f in the thread of the event loop.
bool isCallingFunctions()
Check if the event loop is calling a function.
Definition EventLoop.h:284
void updateChannel(Channel *chl)
Update channel status. This method is usually used internally.
void runOnQuit(Func &&cb)
Run functions when the event loop quits.
void removeChannel(Channel *chl)
Remove a channel from the event loop. This method is usually used internally.
TimerId runEvery(double interval, const Func &cb)
Repeatedly run a function every period of time.
void assertInLoopThread()
Assertion that the current thread is the thread to which the event loop belongs. If the assertion fai...
Definition EventLoop.h:78
This class template represents a lock-free multiple producers single consumer queue.
Definition LockFreeQueue.h:31
Definition EventLoop.h:34