Skip to content

Commit 4bef69f

Browse files
authored
Add comments in public header files (#100)
1 parent 8ca21f1 commit 4bef69f

23 files changed

+1302
-176
lines changed

trantor/net/Channel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
3-
* Channel.h
4-
* An Tao
3+
* @file Channel.h
4+
* @author An Tao
55
*
66
* Public header file in trantor lib.
77
*

trantor/net/EventLoop.h

Lines changed: 154 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,38 @@ enum
4141
InvalidTimerId = 0
4242
};
4343

44+
/**
45+
* @brief As the name implies, this class represents an event loop that runs in
46+
* a perticular thread. The event loop can handle network I/O events and timers
47+
* in asynchronous mode.
48+
* @note An event loop object always belongs to a separate thread, and there is
49+
* one event loop object at most in a thread. We can call an event loop object
50+
* the event loop of the thread it belongs to, or call that thread the thread of
51+
* the event loop.
52+
*/
4453
class EventLoop : NonCopyable
4554
{
4655
public:
4756
EventLoop();
4857
~EventLoop();
58+
59+
/**
60+
* @brief Run the event loop. This method will be blocked until the event
61+
* loop exits.
62+
*
63+
*/
4964
void loop();
65+
66+
/**
67+
* @brief Let the event loop quit.
68+
*
69+
*/
5070
void quit();
71+
72+
/**
73+
* @brief Assertion that the current thread is the thread to which the event
74+
* loop belongs. If the assertion fails, the program aborts.
75+
*/
5176
void assertInLoopThread()
5277
{
5378
if (!isInLoopThread())
@@ -56,47 +81,82 @@ class EventLoop : NonCopyable
5681
}
5782
};
5883
#ifdef __linux__
84+
/**
85+
* @brief Make the timer queue works after calling the fork() function.
86+
*
87+
*/
5988
void resetTimerQueue();
6089
#endif
90+
/**
91+
* @brief Make the event loop works after calling the fork() function.
92+
*
93+
*/
6194
void resetAfterFork();
95+
96+
/**
97+
* @brief Return true if the current thread is the thread to which the event
98+
* loop belongs.
99+
*
100+
* @return true
101+
* @return false
102+
*/
62103
bool isInLoopThread() const
63104
{
64105
return threadId_ == std::this_thread::get_id();
65106
};
107+
108+
/**
109+
* @brief Get the event loop of the current thread. Return nullptr if there
110+
* is no event loop in the current thread.
111+
*
112+
* @return EventLoop*
113+
*/
66114
static EventLoop *getEventLoopOfCurrentThread();
67-
void updateChannel(Channel *chl);
68-
void removeChannel(Channel *chl);
115+
116+
/**
117+
* @brief Run the function f in the thread of the event loop.
118+
*
119+
* @param f
120+
* @note If the current thread is the thread of the event loop, the function
121+
* f is executed directly before the method exiting.
122+
*/
69123
void runInLoop(const Func &f);
70124
void runInLoop(Func &&f);
125+
126+
/**
127+
* @brief Run the function f in the thread of the event loop.
128+
*
129+
* @param f
130+
* @note The difference between this method and the runInLoop() method is
131+
* that the function f is executed after the method exiting no matter if the
132+
* current thread is the thread of the event loop.
133+
*/
71134
void queueInLoop(const Func &f);
72135
void queueInLoop(Func &&f);
73-
void wakeup();
74-
void wakeupRead();
75-
size_t index()
76-
{
77-
return index_;
78-
}
79-
void setIndex(size_t index)
80-
{
81-
index_ = index;
82-
}
83136

84-
///
85-
/// Runs callback at 'time'.
86-
/// Safe to call from other threads.
87-
///
137+
/**
138+
* @brief Run a function at a time point.
139+
*
140+
* @param time The time to run the function.
141+
* @param cb The function to run.
142+
* @return TimerId The ID of the timer.
143+
*/
88144
TimerId runAt(const Date &time, const Func &cb);
89145
TimerId runAt(const Date &time, Func &&cb);
90-
///
91-
/// Runs callback after @c delay seconds.
92-
/// Safe to call from other threads.
93-
///
146+
147+
/**
148+
* @brief Run a function after a period of time.
149+
*
150+
* @param delay Represent the period of time in seconds.
151+
* @param cb The function to run.
152+
* @return TimerId The ID of the timer.
153+
*/
94154
TimerId runAfter(double delay, const Func &cb);
95155
TimerId runAfter(double delay, Func &&cb);
96156

97-
/// Runs @param cb after @param delay
98157
/**
99-
* Users could use chrono literals to represent a time duration
158+
* @brief Run a function after a period of time.
159+
* @note Users could use chrono literals to represent a time duration
100160
* For example:
101161
* @code
102162
runAfter(5s, task);
@@ -112,15 +172,19 @@ class EventLoop : NonCopyable
112172
{
113173
return runAfter(delay.count(), std::move(cb));
114174
}
115-
///
116-
/// Runs callback every @c interval seconds.
117-
/// Safe to call from other threads.
118-
///
175+
176+
/**
177+
* @brief Repeatedly run a function every period of time.
178+
*
179+
* @param interval The duration in seconds.
180+
* @param cb The function to run.
181+
* @return TimerId The ID of the timer.
182+
*/
119183
TimerId runEvery(double interval, const Func &cb);
120184
TimerId runEvery(double interval, Func &&cb);
121185

122-
/// Runs @param cb every @param interval time
123186
/**
187+
* @brief Repeatedly run a function every period of time.
124188
* Users could use chrono literals to represent a time duration
125189
* For example:
126190
* @code
@@ -139,27 +203,82 @@ class EventLoop : NonCopyable
139203
{
140204
return runEvery(interval.count(), std::move(cb));
141205
}
142-
// int getAioEventFd();
143-
// io_context_t getAioContext() {return ctx_;};
206+
207+
/**
208+
* @brief Invalidate the timer identified by the given ID.
209+
*
210+
* @param id The ID of the timer.
211+
*/
144212
void invalidateTimer(TimerId id);
145213

214+
/**
215+
* @brief Move the EventLoop to the current thread, this method must be
216+
* called before the loop is running.
217+
*
218+
*/
219+
void moveToCurrentThread();
220+
221+
/**
222+
* @brief Update channel status. This method is usually used internally.
223+
*
224+
* @param chl
225+
*/
226+
void updateChannel(Channel *chl);
227+
228+
/**
229+
* @brief Remove a channel from the event loop. This method is usually used
230+
* internally.
231+
*
232+
* @param chl
233+
*/
234+
void removeChannel(Channel *chl);
235+
236+
/**
237+
* @brief Return the index of the event loop.
238+
*
239+
* @return size_t
240+
*/
241+
size_t index()
242+
{
243+
return index_;
244+
}
245+
246+
/**
247+
* @brief Set the index of the event loop.
248+
*
249+
* @param index
250+
*/
251+
void setIndex(size_t index)
252+
{
253+
index_ = index;
254+
}
255+
256+
/**
257+
* @brief Return true if the event loop is running.
258+
*
259+
* @return true
260+
* @return false
261+
*/
146262
bool isRunning()
147263
{
148264
return looping_ && (!quit_);
149265
}
266+
267+
/**
268+
* @brief Check if the event loop is calling a function.
269+
*
270+
* @return true
271+
* @return false
272+
*/
150273
bool isCallingFunctions()
151274
{
152275
return callingFuncs_;
153276
}
154-
/**
155-
* @brief Move the EventLoop to the current thread, this method must be
156-
* called before the loop is running.
157-
*
158-
*/
159-
void moveToCurrentThread();
160277

161278
private:
162279
void abortNotInLoopThread();
280+
void wakeup();
281+
void wakeupRead();
163282
bool looping_;
164283
std::thread::id threadId_;
165284
bool quit_;

trantor/net/EventLoopThread.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
3-
* EventLoopThread.h
4-
* An Tao
3+
* @file EventLoopThread.h
4+
* @author An Tao
55
*
66
* Public header file in trantor lib.
77
*
@@ -24,16 +24,37 @@
2424

2525
namespace trantor
2626
{
27+
/**
28+
* @brief This class represents an event loop thread.
29+
*
30+
*/
2731
class EventLoopThread : NonCopyable
2832
{
2933
public:
3034
explicit EventLoopThread(const std::string &threadName = "EventLoopThread");
3135
~EventLoopThread();
36+
37+
/**
38+
* @brief Wait for the event loop to exit.
39+
* @note This method blocks the current thread until the event loop exits.
40+
*/
3241
void wait();
42+
43+
/**
44+
* @brief Get the pointer of the event loop of the thread.
45+
*
46+
* @return EventLoop*
47+
*/
3348
EventLoop *getLoop() const
3449
{
3550
return loop_;
3651
}
52+
53+
/**
54+
* @brief Run the event loop of the thread. This method doesn't block the
55+
* current thread.
56+
*
57+
*/
3758
void run();
3859

3960
private:

trantor/net/EventLoopThreadPool.h

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
*
3-
* EventLoopThreadPool.h
4-
* An Tao
3+
* @file EventLoopThreadPool.h
4+
* @author An Tao
55
*
66
* Public header file in trantor lib.
77
*
@@ -20,21 +20,68 @@
2020

2121
namespace trantor
2222
{
23+
/**
24+
* @brief This class represents a pool of EventLoopThread objects
25+
*
26+
*/
2327
class EventLoopThreadPool : NonCopyable
2428
{
2529
public:
2630
EventLoopThreadPool() = delete;
31+
32+
/**
33+
* @brief Construct a new event loop thread pool instance.
34+
*
35+
* @param threadNum The number of threads
36+
* @param name The name of the EventLoopThreadPool object.
37+
*/
2738
EventLoopThreadPool(size_t threadNum,
2839
const std::string &name = "EventLoopThreadPool");
40+
41+
/**
42+
* @brief Run all event loops in the pool.
43+
* @note This function doesn't block the current thread.
44+
*/
2945
void start();
30-
// void stop();
46+
47+
/**
48+
* @brief Wait for all event loops in the pool to quit.
49+
*
50+
* @note This function blocks the current thread.
51+
*/
3152
void wait();
53+
54+
/**
55+
* @brief Return the number of the event loop.
56+
*
57+
* @return size_t
58+
*/
3259
size_t size()
3360
{
3461
return loopThreadVector_.size();
3562
}
63+
64+
/**
65+
* @brief Get the next event loop in the pool.
66+
*
67+
* @return EventLoop*
68+
*/
3669
EventLoop *getNextLoop();
70+
71+
/**
72+
* @brief Get the event loop in the `id` position in the pool.
73+
*
74+
* @param id The id of the first event loop is zero. If the id >= the number
75+
* of event loops, nullptr is returned.
76+
* @return EventLoop*
77+
*/
3778
EventLoop *getLoop(size_t id);
79+
80+
/**
81+
* @brief Get all event loops in the pool.
82+
*
83+
* @return std::vector<EventLoop *>
84+
*/
3885
std::vector<EventLoop *> getLoops() const;
3986

4087
private:

0 commit comments

Comments
 (0)