@@ -81,9 +81,11 @@ struct QIOChannel {
8181 Object parent ;
8282 unsigned int features ; /* bitmask of QIOChannelFeatures */
8383 char * name ;
84- AioContext * ctx ;
84+ AioContext * read_ctx ;
8585 Coroutine * read_coroutine ;
86+ AioContext * write_ctx ;
8687 Coroutine * write_coroutine ;
88+ bool follow_coroutine_ctx ;
8789#ifdef _WIN32
8890 HANDLE event ; /* For use with GSource on Win32 */
8991#endif
@@ -140,8 +142,9 @@ struct QIOChannelClass {
140142 int whence ,
141143 Error * * errp );
142144 void (* io_set_aio_fd_handler )(QIOChannel * ioc ,
143- AioContext * ctx ,
145+ AioContext * read_ctx ,
144146 IOHandler * io_read ,
147+ AioContext * write_ctx ,
145148 IOHandler * io_write ,
146149 void * opaque );
147150 int (* io_flush )(QIOChannel * ioc ,
@@ -498,6 +501,21 @@ int qio_channel_set_blocking(QIOChannel *ioc,
498501 bool enabled ,
499502 Error * * errp );
500503
504+ /**
505+ * qio_channel_set_follow_coroutine_ctx:
506+ * @ioc: the channel object
507+ * @enabled: whether or not to follow the coroutine's AioContext
508+ *
509+ * If @enabled is true, calls to qio_channel_yield() use the current
510+ * coroutine's AioContext. Usually this is desirable.
511+ *
512+ * If @enabled is false, calls to qio_channel_yield() use the global iohandler
513+ * AioContext. This is may be used by coroutines that run in the main loop and
514+ * do not wish to respond to I/O during nested event loops. This is the
515+ * default for compatibility with code that is not aware of AioContexts.
516+ */
517+ void qio_channel_set_follow_coroutine_ctx (QIOChannel * ioc , bool enabled );
518+
501519/**
502520 * qio_channel_close:
503521 * @ioc: the channel object
@@ -703,41 +721,6 @@ GSource *qio_channel_add_watch_source(QIOChannel *ioc,
703721 GDestroyNotify notify ,
704722 GMainContext * context );
705723
706- /**
707- * qio_channel_attach_aio_context:
708- * @ioc: the channel object
709- * @ctx: the #AioContext to set the handlers on
710- *
711- * Request that qio_channel_yield() sets I/O handlers on
712- * the given #AioContext. If @ctx is %NULL, qio_channel_yield()
713- * uses QEMU's main thread event loop.
714- *
715- * You can move a #QIOChannel from one #AioContext to another even if
716- * I/O handlers are set for a coroutine. However, #QIOChannel provides
717- * no synchronization between the calls to qio_channel_yield() and
718- * qio_channel_attach_aio_context().
719- *
720- * Therefore you should first call qio_channel_detach_aio_context()
721- * to ensure that the coroutine is not entered concurrently. Then,
722- * while the coroutine has yielded, call qio_channel_attach_aio_context(),
723- * and then aio_co_schedule() to place the coroutine on the new
724- * #AioContext. The calls to qio_channel_detach_aio_context()
725- * and qio_channel_attach_aio_context() should be protected with
726- * aio_context_acquire() and aio_context_release().
727- */
728- void qio_channel_attach_aio_context (QIOChannel * ioc ,
729- AioContext * ctx );
730-
731- /**
732- * qio_channel_detach_aio_context:
733- * @ioc: the channel object
734- *
735- * Disable any I/O handlers set by qio_channel_yield(). With the
736- * help of aio_co_schedule(), this allows moving a coroutine that was
737- * paused by qio_channel_yield() to another context.
738- */
739- void qio_channel_detach_aio_context (QIOChannel * ioc );
740-
741724/**
742725 * qio_channel_yield:
743726 * @ioc: the channel object
@@ -785,19 +768,27 @@ void qio_channel_wait(QIOChannel *ioc,
785768/**
786769 * qio_channel_set_aio_fd_handler:
787770 * @ioc: the channel object
788- * @ctx : the AioContext to set the handlers on
771+ * @read_ctx : the AioContext to set the read handler on or NULL
789772 * @io_read: the read handler
773+ * @write_ctx: the AioContext to set the write handler on or NULL
790774 * @io_write: the write handler
791775 * @opaque: the opaque value passed to the handler
792776 *
793777 * This is used internally by qio_channel_yield(). It can
794778 * be used by channel implementations to forward the handlers
795779 * to another channel (e.g. from #QIOChannelTLS to the
796780 * underlying socket).
781+ *
782+ * When @read_ctx is NULL, don't touch the read handler. When @write_ctx is
783+ * NULL, don't touch the write handler. Note that setting the read handler
784+ * clears the write handler, and vice versa, if they share the same AioContext.
785+ * Therefore the caller must pass both handlers together when sharing the same
786+ * AioContext.
797787 */
798788void qio_channel_set_aio_fd_handler (QIOChannel * ioc ,
799- AioContext * ctx ,
789+ AioContext * read_ctx ,
800790 IOHandler * io_read ,
791+ AioContext * write_ctx ,
801792 IOHandler * io_write ,
802793 void * opaque );
803794
0 commit comments