Skip to content

Commit 794f309

Browse files
committed
Optimize the delivery order of queued tasks
1 parent 8d1d5db commit 794f309

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/coroutine/iouring.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ bool Iouring::wakeup() {
195195
}
196196

197197
uint32_t ready_count = 0;
198-
for (decltype(count) i = 0; i < count; i++) {
198+
SW_LOOP_N(count) {
199199
auto cqe = cqes[i];
200200
auto event = static_cast<IouringEvent *>(io_uring_cqe_get_data(cqe));
201201
// The user data for the timeout request is -1, this event should be ignored.
@@ -216,7 +216,22 @@ bool Iouring::wakeup() {
216216
}
217217
io_uring_cq_advance(&ring, count);
218218

219-
for (decltype(ready_count) i = 0; i < ready_count; i++) {
219+
/**
220+
* After the harvest is completed, the operating system's unprocessed task queue is reduced.
221+
* Before resuming the coroutine ready for IO, it should extract the queued SQE to the SQEs queue for processing
222+
* by the operating system. This can achieve a relatively good balance. If the submission is made after resuming
223+
* the coroutine, the kernel may become idle.
224+
*/
225+
SW_LOOP_N(ready_count) {
226+
if (get_sq_space_left() == 0 || is_empty_waiting_tasks()) {
227+
break;
228+
}
229+
waiting_task = waiting_tasks.front();
230+
waiting_tasks.pop();
231+
dispatch(waiting_task);
232+
}
233+
234+
SW_LOOP_N(ready_count) {
220235
auto event = ready_events[i];
221236
if (event->result < 0) {
222237
errno = -(event->result);
@@ -229,11 +244,6 @@ bool Iouring::wakeup() {
229244
event->result = -1;
230245
}
231246
resume(ready_events[i]);
232-
if (!is_empty_waiting_tasks()) {
233-
waiting_task = waiting_tasks.front();
234-
waiting_tasks.pop();
235-
dispatch(waiting_task);
236-
}
237247
}
238248
}
239249

0 commit comments

Comments
 (0)