ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_linuxaio.c
(Generate patch)

Comparing libev/ev_linuxaio.c (file contents):
Revision 1.29 by root, Tue Jun 25 06:43:04 2019 UTC vs.
Revision 1.33 by root, Tue Jun 25 22:41:23 2019 UTC

116 unsigned header_length; /* size of aio_ring */ 116 unsigned header_length; /* size of aio_ring */
117 117
118 struct io_event io_events[0]; 118 struct io_event io_events[0];
119}; 119};
120 120
121/*
122 * define some syscall wrappers for common architectures
123 * this is mostly for nice looks during debugging, not performance.
124 * our syscalls return < 0, not == -1, on error. which is good
125 * enough for linux aio.
126 * TODO: arm is also common nowadays, maybe even mips and x86
127 * TODO: after implementing this, it suddenly looks like overkill, but its hard to remove...
128 */
129#if __GNUC__ && __linux && ECB_AMD64 && !defined __OPTIMIZE_SIZE__
130 /* the costly errno access probably kills this for size optimisation */
131
132 #define ev_syscall(nr,narg,arg1,arg2,arg3,arg4,arg5) \
133 ({ \
134 long res; \
135 register unsigned long r5 __asm__ ("r8" ); \
136 register unsigned long r4 __asm__ ("r10"); \
137 register unsigned long r3 __asm__ ("rdx"); \
138 register unsigned long r2 __asm__ ("rsi"); \
139 register unsigned long r1 __asm__ ("rdi"); \
140 if (narg >= 5) r5 = (unsigned long)(arg5); \
141 if (narg >= 4) r4 = (unsigned long)(arg4); \
142 if (narg >= 3) r3 = (unsigned long)(arg3); \
143 if (narg >= 2) r2 = (unsigned long)(arg2); \
144 if (narg >= 1) r1 = (unsigned long)(arg1); \
145 __asm__ __volatile__ ( \
146 "syscall\n\t" \
147 : "=a" (res) \
148 : "0" (nr), "r" (r1), "r" (r2), "r" (r3), "r" (r4), "r" (r5) \
149 : "cc", "r11", "cx", "memory"); \
150 errno = -res; \
151 res; \
152 })
153
154#endif
155
156#ifdef ev_syscall
157 #define ev_syscall0(nr) ev_syscall (nr, 0, 0, 0, 0, 0, 0
158 #define ev_syscall1(nr,arg1) ev_syscall (nr, 1, arg1, 0, 0, 0, 0)
159 #define ev_syscall2(nr,arg1,arg2) ev_syscall (nr, 2, arg1, arg2, 0, 0, 0)
160 #define ev_syscall3(nr,arg1,arg2,arg3) ev_syscall (nr, 3, arg1, arg2, arg3, 0, 0)
161 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) ev_syscall (nr, 3, arg1, arg2, arg3, arg4, 0)
162 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) ev_syscall (nr, 5, arg1, arg2, arg3, arg4, arg5)
163#else
164 #define ev_syscall0(nr) syscall (nr)
165 #define ev_syscall1(nr,arg1) syscall (nr, arg1)
166 #define ev_syscall2(nr,arg1,arg2) syscall (nr, arg1, arg2)
167 #define ev_syscall3(nr,arg1,arg2,arg3) syscall (nr, arg1, arg2, arg3)
168 #define ev_syscall4(nr,arg1,arg2,arg3,arg4) syscall (nr, arg1, arg2, arg3, arg4)
169 #define ev_syscall5(nr,arg1,arg2,arg3,arg4,arg5) syscall (nr, arg1, arg2, arg3, arg4, arg5)
170#endif
171
121inline_size 172inline_size
122int 173int
123evsys_io_setup (unsigned nr_events, aio_context_t *ctx_idp) 174evsys_io_setup (unsigned nr_events, aio_context_t *ctx_idp)
124{ 175{
125 return syscall (SYS_io_setup, nr_events, ctx_idp); 176 return ev_syscall2 (SYS_io_setup, nr_events, ctx_idp);
126} 177}
127 178
128inline_size 179inline_size
129int 180int
130evsys_io_destroy (aio_context_t ctx_id) 181evsys_io_destroy (aio_context_t ctx_id)
131{ 182{
132 return syscall (SYS_io_destroy, ctx_id); 183 return ev_syscall1 (SYS_io_destroy, ctx_id);
133} 184}
134 185
135inline_size 186inline_size
136int 187int
137evsys_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) 188evsys_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[])
138{ 189{
139 return syscall (SYS_io_submit, ctx_id, nr, cbp); 190 return ev_syscall3 (SYS_io_submit, ctx_id, nr, cbp);
140} 191}
141 192
142inline_size 193inline_size
143int 194int
144evsys_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) 195evsys_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result)
145{ 196{
146 return syscall (SYS_io_cancel, ctx_id, cbp, result); 197 return ev_syscall3 (SYS_io_cancel, ctx_id, cbp, result);
147} 198}
148 199
149inline_size 200inline_size
150int 201int
151evsys_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) 202evsys_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout)
152{ 203{
153 return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); 204 return ev_syscall5 (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout);
154} 205}
155 206
156/*****************************************************************************/ 207/*****************************************************************************/
157/* actual backed implementation */ 208/* actual backed implementation */
158 209
240 if (iocb->io.aio_reqprio < 0) 291 if (iocb->io.aio_reqprio < 0)
241 { 292 {
242 /* we handed this fd over to epoll, so undo this first */ 293 /* we handed this fd over to epoll, so undo this first */
243 /* we do it manually because the optimisations on epoll_modfy won't do us any good */ 294 /* we do it manually because the optimisations on epoll_modfy won't do us any good */
244 epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); 295 epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0);
296 anfds [fd].emask = 0;
245 iocb->io.aio_reqprio = 0; 297 iocb->io.aio_reqprio = 0;
246 } 298 }
247 299
248 if (iocb->io.aio_buf) 300 if (iocb->io.aio_buf)
249 /* io_cancel always returns some error on relevant kernels, but works */ 301 /* io_cancel always returns some error on relevant kernels, but works */
373 /* ignored */; 425 /* ignored */;
374 else 426 else
375 ev_syserr ("(libev) linuxaio io_getevents"); 427 ev_syserr ("(libev) linuxaio io_getevents");
376 else if (res) 428 else if (res)
377 { 429 {
378 /* at least one event received, handle it and any remaining ones in the ring buffer */ 430 /* at least one event available, handle it and any remaining ones in the ring buffer */
379 linuxaio_parse_events (EV_A_ ioev, res); 431 linuxaio_parse_events (EV_A_ ioev, res);
380 linuxaio_get_events_from_ring (EV_A); 432 linuxaio_get_events_from_ring (EV_A);
381 } 433 }
382} 434}
383 435
384static int 436inline_size
437int
385linuxaio_io_setup (EV_P) 438linuxaio_io_setup (EV_P)
386{ 439{
387 linuxaio_ctx = 0; 440 linuxaio_ctx = 0;
388 return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx); 441 return evsys_io_setup (linuxaio_nr_events (EV_A), &linuxaio_ctx);
389} 442}
517void 570void
518linuxaio_destroy (EV_P) 571linuxaio_destroy (EV_P)
519{ 572{
520 epoll_destroy (EV_A); 573 epoll_destroy (EV_A);
521 linuxaio_free_iocbp (EV_A); 574 linuxaio_free_iocbp (EV_A);
522 evsys_io_destroy (linuxaio_ctx); 575 evsys_io_destroy (linuxaio_ctx); /* fails in child, aio context is destroyed */
523} 576}
524 577
525inline_size 578inline_size
526void 579void
527linuxaio_fork (EV_P) 580linuxaio_fork (EV_P)
533 linuxaio_iteration = 0; /* we start over in the child */ 586 linuxaio_iteration = 0; /* we start over in the child */
534 587
535 while (linuxaio_io_setup (EV_A) < 0) 588 while (linuxaio_io_setup (EV_A) < 0)
536 ev_syserr ("(libev) linuxaio io_setup"); 589 ev_syserr ("(libev) linuxaio io_setup");
537 590
591 /* forking epoll should also effectively unregister all fds from the backend */
538 epoll_fork (EV_A); 592 epoll_fork (EV_A);
539 593
540 ev_io_stop (EV_A_ &linuxaio_epoll_w); 594 ev_io_stop (EV_A_ &linuxaio_epoll_w);
541 ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ); 595 ev_io_set (EV_A_ &linuxaio_epoll_w, backend_fd, EV_READ);
542 ev_io_start (EV_A_ &linuxaio_epoll_w); 596 ev_io_start (EV_A_ &linuxaio_epoll_w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines