diff --git a/src/coio.m b/src/coio.m index af950ed0361474f4119bd1ce6f423285c44f135a..799e4c68dae12113d468b25e6bd2f5ea61552e81 100644 --- a/src/coio.m +++ b/src/coio.m @@ -47,9 +47,24 @@ coio_init(struct ev_io *coio) } static inline void -coio_update_fiber(struct ev_io *coio) +coio_fiber_yield(struct ev_io *coio) { coio->data = fiber; + fiber_yield(); +#ifdef DEBUG + coio->data = NULL; +#endif +} + +static inline bool +coio_fiber_yield_timeout(struct ev_io *coio, ev_tstamp delay) +{ + coio->data = fiber; + bool is_timedout = fiber_yield_timeout(delay); +#ifdef DEBUG + coio->data = NULL; +#endif + return is_timedout; } /** @@ -78,10 +93,9 @@ coio_connect_timeout(struct ev_io *coio, struct sockaddr_in *addr, * Wait until socket is ready for writing or * timed out. */ - coio_update_fiber(coio); ev_io_set(coio, coio->fd, EV_WRITE); ev_io_start(coio); - bool is_timedout = fiber_yield_timeout(timeout); + bool is_timedout = coio_fiber_yield_timeout(coio, timeout); ev_io_stop(coio); fiber_testcancel(); if (is_timedout) { @@ -159,16 +173,16 @@ coio_accept(struct ev_io *coio, struct sockaddr_in *addr, evio_setsockopt_tcp(fd); return fd; } - /* update current coio fiber context */ - coio_update_fiber(coio); /* The socket is not ready, yield */ if (! ev_is_active(coio)) { ev_io_set(coio, coio->fd, EV_READ); ev_io_start(coio); } - /* Yield control to other fibers until the timeout - * is being reached. */ - bool is_timedout = fiber_yield_timeout(delay); + /* + * Yield control to other fibers until the + * timeout is reached. + */ + bool is_timedout = coio_fiber_yield_timeout(coio, delay); fiber_testcancel(); if (is_timedout) { errno = ETIMEDOUT; @@ -216,7 +230,6 @@ coio_read_ahead_timeout(struct ev_io *coio, void *buf, size_t sz, return sz - to_read; } - coio_update_fiber(coio); /* The socket is not ready, yield */ if (! ev_is_active(coio)) { ev_io_set(coio, coio->fd, EV_READ); @@ -226,7 +239,8 @@ coio_read_ahead_timeout(struct ev_io *coio, void *buf, size_t sz, * Yield control to other fibers until the * timeout is being reached. */ - bool is_timedout = fiber_yield_timeout(delay); + bool is_timedout = coio_fiber_yield_timeout(coio, + delay); fiber_testcancel(); if (is_timedout) { errno = ETIMEDOUT; @@ -309,20 +323,19 @@ coio_write_timeout(struct ev_io *coio, const void *buf, size_t sz, towrite -= nwr; buf += nwr; } - coio_update_fiber(coio); if (! ev_is_active(coio)) { ev_io_set(coio, coio->fd, EV_WRITE); ev_io_start(coio); } /* Yield control to other fibers. */ - fiber_yield(); fiber_testcancel(); /* * Yield control to other fibers until the * timeout is reached or the socket is * ready. */ - bool is_timedout = fiber_yield_timeout(delay); + bool is_timedout = coio_fiber_yield_timeout(coio, + delay); fiber_testcancel(); if (is_timedout) { @@ -381,13 +394,12 @@ coio_writev(struct ev_io *coio, struct iovec *iov, int iovcnt, break; } } - coio_update_fiber(coio); if (! ev_is_active(coio)) { ev_io_set(coio, coio->fd, EV_WRITE); ev_io_start(coio); } /* Yield control to other fibers. */ - fiber_yield(); + coio_fiber_yield(coio); fiber_testcancel(); } } @finally { @@ -420,7 +432,6 @@ coio_sendto_timeout(struct ev_io *coio, const void *buf, size_t sz, int flags, flags, dest_addr, addrlen); if (nwr > 0) return nwr; - coio_update_fiber(coio); if (! ev_is_active(coio)) { ev_io_set(coio, coio->fd, EV_WRITE); ev_io_start(coio); @@ -430,7 +441,8 @@ coio_sendto_timeout(struct ev_io *coio, const void *buf, size_t sz, int flags, * timeout is reached or the socket is * ready. */ - bool is_timedout = fiber_yield_timeout(delay); + bool is_timedout = coio_fiber_yield_timeout(coio, + delay); fiber_testcancel(); if (is_timedout) { errno = ETIMEDOUT; @@ -469,7 +481,6 @@ coio_recvfrom_timeout(struct ev_io *coio, void *buf, size_t sz, int flags, if (nrd >= 0) return nrd; - coio_update_fiber(coio); if (! ev_is_active(coio)) { ev_io_set(coio, coio->fd, EV_WRITE); ev_io_start(coio); @@ -479,7 +490,8 @@ coio_recvfrom_timeout(struct ev_io *coio, void *buf, size_t sz, int flags, * timeout is reached or the socket is * ready. */ - bool is_timedout = fiber_yield_timeout(delay); + bool is_timedout = coio_fiber_yield_timeout(coio, + delay); fiber_testcancel(); if (is_timedout) { errno = ETIMEDOUT;