@@ -34,19 +34,19 @@ class IgnoreSigPipe {
34
34
#pragma GCC diagnostic error "-Wold-style-cast"
35
35
IgnoreSigPipe initObj;
36
36
37
- // 和IgnoreSigPipe一样再全局中init
38
- class Log {
39
- public:
40
- Log () {
41
- if (!Logger::Instance ().init (" log" , " logs/test.log" , spdlog::level::trace)) {
42
- ERROR (" Logger init error" );
43
- } else {
44
- TRACE (" Logger setup" );
45
- }
46
- }
47
- };
48
-
49
- Log initLog;
37
+ // // 和IgnoreSigPipe一样再全局中init
38
+ // class Log {
39
+ // public:
40
+ // Log() {
41
+ // if (!Logger::Instance().init("log", "logs/test.log", spdlog::level::trace)) {
42
+ // ERROR("Logger init error");
43
+ // } else {
44
+ // TRACE("Logger setup");
45
+ // }
46
+ // }
47
+ // };
48
+
49
+ // Log initLog;
50
50
51
51
} // namespace
52
52
@@ -61,8 +61,10 @@ EventLoop::EventLoop() :
61
61
epoll_(util::make_unique<Epoll>(this )),
62
62
timerQueue_(util::make_unique<TimerQueue>(this )),
63
63
wakeupFd_(createEventfd()),
64
- wakeupChannel_(util::make_unique<Channel>(this , wakeupFd_)) {
65
- TRACE (" EventLoop created {} " , fmt::ptr (this ));
64
+ wakeupChannel_(util::make_unique<Channel>(this , wakeupFd_)),
65
+ currentActiveChannel_(nullptr ),
66
+ threadLocalLoopPtr_(&t_loopInthisThread) {
67
+ TRACE (" EventLoop created {}" , fmt::ptr (this ));
66
68
if (t_loopInthisThread) {
67
69
CRITICAL (" Another EventLoop {} exists in this Thread( tid = {} ) ..." , fmt::ptr (t_loopInthisThread), util::tid ());
68
70
} else {
@@ -100,13 +102,13 @@ void EventLoop::loop() {
100
102
// TODO : sort channel by priority
101
103
eventHandling_ = true ;
102
104
for (auto channel: activeChannels_) {
103
- // currentActiveChannel_ 在removeChannel()中需要做判断
105
+ // currentActiveChannel_ 在removeChannel()中需要做判断 todo 理
104
106
currentActiveChannel_ = channel;
105
107
currentActiveChannel_->handleEvent (epollWaitReturnTime_);
106
108
}
107
109
currentActiveChannel_ = nullptr ;
108
110
eventHandling_ = false ;
109
- doPendingFunctors ();
111
+ doQueueInLoopFuncs ();
110
112
}
111
113
TRACE (" EventLoop {} stop looping" , fmt::ptr (this ));
112
114
looping_ = false ;
@@ -132,33 +134,41 @@ void EventLoop::quit() {
132
134
// 为其他线程提供一个接口,其他线程将要执行的任务用这个接口交给当前线程
133
135
// 这样当前线程统一处理自己的资源,而不用加锁,唯一需要加锁的地方就是
134
136
// 通过接口添加任务的任务队列这个地方,大大减小了锁粒度
135
- void EventLoop::runInLoop (const Functor & func) {
137
+ void EventLoop::runInLoop (const Func & func) {
136
138
if (isInLoopThread ()) {
137
139
func (); // 如果在当前IO线程中调用,则同步调用
138
140
} else {
139
141
queueInLoop (func); // 如果在其他线程中调用该函数,则异步调用,用queueInLoop添加到任务队列中
140
142
}
141
143
}
142
- // 加入队列中,等待被执行,该函数可以跨线程调用,即其他线程可以给当前线程添加任务
143
- void EventLoop::queueInLoop (const Functor& func) {
144
- {
145
- std::lock_guard<std::mutex> lock (mutex_);
146
- pendingFunctors_.push_back (std::move (func));
144
+
145
+ void EventLoop::runInLoop (Func&& func) {
146
+ if (isInLoopThread ()) {
147
+ func ();
148
+ } else {
149
+ queueInLoop (std::move (func));
147
150
}
151
+ }
152
+ // 加入队列中,等待被执行,该函数可以跨线程调用,即其他线程可以给当前线程添加任务
153
+ void EventLoop::queueInLoop (const Func& func) {
154
+ queueFuncs_.enqueue (func); // 无锁队列入队
148
155
// 如果不是当前线程(可能阻塞在wait),需要唤醒
149
156
// 或者是当前线程但是在正在处理队列中的任务(使得处理完当前队列中的元素后立即在进行下一轮处理,因为在这里又添加了任务)需要唤醒
150
157
// 只有当前IO线程的事件回调中调用queueInLoop才不需要唤醒(因为执行完handleEvent会自然执行loop()中的doPendingFunctor)
151
- if (!isInLoopThread () || callingPendingFunctors_ ) {
158
+ if (!isInLoopThread () || !looping_ ) { // todo !looping
152
159
wakeup ();
153
160
}
154
161
}
155
162
156
- size_t EventLoop::queueSize () const {
157
- std::lock_guard<std::mutex> lock (mutex_);
158
- return pendingFunctors_.size ();
163
+ void EventLoop::queueInLoop (Func&& func) {
164
+ queueFuncs_.enqueue (std::move (func));
165
+ if (!isInLoopThread () || !looping_) { // todo !looping
166
+ wakeup ();
167
+ }
159
168
}
160
169
161
170
171
+
162
172
TimerId EventLoop::runAt (Timestamp time, TimerCallback timercb) {
163
173
return timerQueue_->addTimer (std::move (timercb), time, 0.0 );
164
174
}
@@ -187,6 +197,7 @@ bool EventLoop::isInLoopThread() const {
187
197
return threadId_ == util::tid ();
188
198
}
189
199
200
+ // 如果当前线程不是IO线程则为nullptr
190
201
EventLoop* EventLoop::getEventLoopOfCurrentThread () {
191
202
return t_loopInthisThread;
192
203
}
@@ -202,6 +213,26 @@ void EventLoop::wakeup() {
202
213
ERROR (" EventLoop::wakeup() writes {} bytes instead of 8" , n);
203
214
}
204
215
}
216
+ void EventLoop::moveToCurrentThread () {
217
+ if (isRunning ()) {
218
+ CRITICAL (" EventLoop cannot be moved when running" );
219
+ }
220
+ if (isInLoopThread ()) {
221
+ WARN (" This EventLoop is already in the current thread" );
222
+ return ;
223
+ }
224
+ if (t_loopInthisThread) {
225
+ CRITICAL (" There is already an EventLoop in this thread, you cannot move another in" );
226
+ }
227
+ // *threadLocalLoopPtr_ = nullptr;
228
+ t_loopInthisThread = this ;
229
+ threadLocalLoopPtr_ = &t_loopInthisThread;
230
+ threadId_ = util::tid ();
231
+ }
232
+
233
+ bool EventLoop::isRunning () {
234
+ return looping_ && (!quit_);
235
+ }
205
236
206
237
void EventLoop::updateChannel (Channel* channel) {
207
238
assert (channel->ownerLoop () == this );
@@ -227,7 +258,6 @@ bool EventLoop::hasChannel(Channel* channel) {
227
258
}
228
259
229
260
void EventLoop::abortNotInLoopThread () {
230
- std::cout << " 11" << std::endl;
231
261
CRITICAL (" EventLoop::abortNotInLoopThread - EventLoop {} was \
232
262
created in threadId_ = {}, and current id = {} ..." ,
233
263
fmt::ptr (this ), threadId_, util::tid ());
@@ -256,17 +286,15 @@ void EventLoop::printActiveChannels() const {
256
286
257
287
// 3. 没有反复执行doPendingFunctors()直到pendingFunctors为空,
258
288
// 这是有意的,否则IO线程可能陷入死循环,无法处理IO事件。
259
- void EventLoop::doPendingFunctors () {
260
- std::vector<Functor> functors;
261
- callingPendingFunctors_ = true ;
262
- {
263
- std::lock_guard<std::mutex> lock (mutex_);
264
- functors.swap (pendingFunctors_);
265
- }
266
- for (const auto & functor: functors) {
267
- functor ();
289
+ void EventLoop::doQueueInLoopFuncs () {
290
+ callingQueueFuncs_ = true ;
291
+ while (!queueFuncs_.empty ()) {
292
+ Func func;
293
+ while (queueFuncs_.dequeue (func)) {
294
+ func ();
295
+ }
268
296
}
269
- callingPendingFunctors_ = false ;
297
+ callingQueueFuncs_ = false ;
270
298
}
271
299
272
300
0 commit comments