Skip to content

Commit 4a7a987

Browse files
committed
Optimize iouring code, and uniformly change the error code after timeout to ETIMEDOUT.
1 parent e2ccbab commit 4a7a987

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

src/coroutine/iouring.cc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,30 +201,38 @@ bool Iouring::wakeup() {
201201

202202
uint32_t ready_count = 0;
203203
for (decltype(count) i = 0; i < count; i++) {
204-
auto *cqe = cqes[i];
205-
auto *event = static_cast<IouringEvent *>(io_uring_cqe_get_data(cqe));
204+
auto cqe = cqes[i];
205+
auto event = static_cast<IouringEvent *>(io_uring_cqe_get_data(cqe));
206206
// The user data for the timeout request is -1, this event should be ignored.
207207
if (event == reinterpret_cast<void *>(TIMEOUT_EVENT)) {
208208
swoole_trace(
209209
"timeout, cqe.flags=%d, ceq.res=%d, error=`%s`", cqe->flags, cqe->res, strerror(-cqe->res));
210210
continue;
211211
}
212-
if (cqe->res < 0) {
213-
errno = -(cqe->res);
214-
event->result = -1;
215-
} else {
216-
event->result = cqe->res;
217-
}
212+
213+
event->result = cqe->res;
214+
ready_events[ready_count++] = event;
215+
218216
swoole_trace("opcode=%s, cqe.flags=%d, ceq.res=%d, error=`%s`",
219217
get_opcode_name((io_uring_op) event->data.opcode),
220218
cqe->flags,
221219
cqe->res,
222220
strerror(cqe->res < 0 ? errno : 0));
223-
ready_events[ready_count++] = event;
224221
}
225222
io_uring_cq_advance(&ring, count);
226223

227224
for (decltype(ready_count) i = 0; i < ready_count; i++) {
225+
auto event = ready_events[i];
226+
if (event->result < 0) {
227+
errno = -(event->result);
228+
/**
229+
* After a timeout, iouring will set errno to `ECANCELED`, but in the async implementation,
230+
* the errno after a timeout is `ETIMEDOUT`.
231+
* To maintain compatibility, numerical conversion is necessary.
232+
*/
233+
errno = errno == ECANCELED ? ETIMEDOUT : errno;
234+
event->result = -1;
235+
}
228236
resume(ready_events[i]);
229237
if (!is_empty_waiting_tasks()) {
230238
waiting_task = waiting_tasks.front();
@@ -810,12 +818,6 @@ pid_t Iouring::waitpid(pid_t _pid, int *stat_loc, int options, double timeout) {
810818
*stat_loc = siginfo_to_status(&info);
811819
return info.si_pid;
812820
}
813-
/**
814-
* After a timeout, iouring will set errno to `ECANCELED`, but in the async implementation,
815-
* the errno after a timeout is `ETIMEDOUT`.
816-
* To maintain compatibility, numerical conversion is necessary.
817-
*/
818-
errno = errno == ECANCELED ? ETIMEDOUT : errno;
819821
return rc;
820822
}
821823

0 commit comments

Comments
 (0)