iothread: allow to create internal iothreads
v3 cover iothread: allow to create internal iothreads
v3:
- pick up r-bs (one missing for Fam's on last patch)
- fix patch 1 to create isolated internal container
v2:
- add one patch to provide object_get_internal_root() [Daniel]
- patch 2: use the new object_get_internal_root()
- patch 3: fix commit message, "reentrant" is wrongly used by me. it should be "called multiple times"; move iothread->ctx check into iothread_stop() [Fam]
- patch 4: add one paragraph in commit message, mention about the glib issue. [Fam]
When trying to support monitor OOB (out-of-band) commands, I found that the monitor IO thread I did looks just like iothread. It would be best if I can use iothread directly. However it seems that it was mostly used by "-object iothread" before but not friendly to internal usages. This series tries to export essential functions to do it.
Also, I think patch 2 also fixes a bug in iothread_stop().
Please review. Thanks.
Peter Xu (4):
qom: provide root container for internal objs
iothread: provide helpers for internal use
iothread: export iothread_stop()
iothread: delay the context release to finalize
include/qom/object.h | 10 ++++++++++ include/sysemu/iothread.h | 9 +++++++++ iothread.c | 46 ++++++++++++++++++++++++++++++++++++---------- qom/object.c | 11 +++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-)
Signature · 1 line
2.7.4
PATCH v3 1/4 qom: provide root container for internal objs
We have object_get_objects_root() to keep user created objects, however no place for objects that will be used internally. Create such a container for internal objects.
--- include/qom/object.h | 10 ++++++++++ qom/object.c | 11 +++++++++++ 2 files changed, 21 insertions(+)
include/qom/object.h +10 -0
@@ -1214,6 +1214,16 @@ Object *object_get_root(void);Object *object_get_objects_root (void );/** +* object_get_internal_root: +* +* Get the container object that holds internally used object +* instances. This is the object at path "/internal-objects" +* +* Returns: the internal object container +*/ +Object *object_get_internal_root (void ); + +/** * object_get_canonical_path_component: * * Returns: The final component in the object's canonical path. The canonical
qom/object.c +11 -0
@@ -1370,6 +1370,17 @@ Object *object_get_objects_root(void)return container_get (object_get_root (),"/objects" ); } +Object *object_get_internal_root (void ) +{ +static Object *internal_root; + +if (!internal_root) { + internal_root =object_new ("container" ); + } + +return internal_root; +} +static void object_get_child_property (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp)
Signature · 1 line
2.7.4
PATCH v3 2/4 iothread: provide helpers for internal use
In reply to Peter XuIOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.
Put all the internal used iothreads into the internal object container.
--- include/sysemu/iothread.h | 8 ++++++++ iothread.c | 16 ++++++++++++++++ 2 files changed, 24 insertions(+)
include/sysemu/iothread.h +8 -0
@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);void iothread_stop_all (void );GMainContext *iothread_get_g_main_context (IOThread *iothread); +/* +* Helpers used to allocate iothreads for internal use. These +* iothreads will not be seen by monitor clients when query using +* "query-iothreads". +*/ +IOThread *iothread_create (const char *id,Error **errp); +void iothread_destroy (IOThread *iothread); +#endif /* IOTHREAD_H */
iothread.c +16 -0
@@ -354,3 +354,19 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)return iothread->worker_context; } + +IOThread *iothread_create (const char *id,Error **errp) +{ +Object *obj; + + obj =object_new_with_props (TYPE_IOTHREAD , +object_get_internal_root (), + id, errp,NULL ); + +return IOTHREAD (obj); +} + +void iothread_destroy (IOThread *iothread) +{ +object_unparent (OBJECT (iothread)); +}
Signature · 1 line
2.7.4
PATCH v3 3/4 iothread: export iothread_stop()
In reply to Peter XuSo that internal iothread users can explicitly stop one iothread without destroying it.
Since at it, fix iothread_stop() to allow it to be called multiple times. Before this patch we may call iothread_stop() more than once on single iothread, while that may not be correct since qemu_thread_join() is not allowed to run twice. From manual of pthread_join():
Joining with a thread that has previously been joined results in undefined behavior.
--- include/sysemu/iothread.h | 1 + iothread.c | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-)
include/sysemu/iothread.h +1 -0
@@ -52,6 +52,7 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread);* "query-iothreads". */ IOThread *iothread_create (const char *id,Error **errp); +void iothread_stop (IOThread *iothread);void iothread_destroy (IOThread *iothread);#endif /* IOTHREAD_H */
iothread.c +16 -8
@@ -80,13 +80,10 @@ static void *iothread_run(void *opaque)return NULL ; } -static int iothread_stop (Object *object,void *opaque) +void iothread_stop (IOThread *iothread) { -IOThread *iothread; - - iothread = (IOThread *)object_dynamic_cast (object,TYPE_IOTHREAD ); -if (!iothread || !iothread->ctx) { -return 0 ; +if (!iothread->ctx || iothread->stopping) { +return ; } iothread->stopping = true;aio_notify (iothread->ctx);
@@ -94,6 +91,17 @@ static int iothread_stop(Object *object, void *opaque)g_main_loop_quit (iothread->main_loop); }qemu_thread_join (&iothread->thread); +} + +static int iothread_stop_iter (Object *object,void *opaque) +{ +IOThread *iothread; + + iothread = (IOThread *)object_dynamic_cast (object,TYPE_IOTHREAD ); +if (!iothread) { +return 0 ; + } +iothread_stop (iothread);return 0 ; }
@@ -108,7 +116,7 @@ static void iothread_instance_finalize(Object *obj) {IOThread *iothread =IOTHREAD (obj); -iothread_stop (obj,NULL ); +iothread_stop (iothread);qemu_cond_destroy (&iothread->init_done_cond);qemu_mutex_destroy (&iothread->init_done_lock);if (!iothread->ctx) {
@@ -328,7 +336,7 @@ void iothread_stop_all(void)aio_context_release (ctx); } -object_child_foreach (container, iothread_stop,NULL ); +object_child_foreach (container, iothread_stop_iter,NULL ); }static gpointer iothread_g_main_context_init (gpointer opaque)
Signature · 1 line
2.7.4
PATCH v3 4/4 iothread: delay the context release to finalize
In reply to Peter XuWhen gcontext is used with iothread, the context will be destroyed during iothread_stop(). That's not good since sometimes we would like to keep the resources until iothread is destroyed, but we may want to stop the thread before that point.
Delay the destruction of gcontext to iothread finalize. Then we can do:
iothread_stop(thread); some_cleanup_on_resources(); iothread_destroy(thread);
We may need this patch if we want to run chardev IOs in iothreads and hopefully clean them up correctly. For more specific information, please see 2b316774f6 ("qemu-char: do not operate on sources from finalize callbacks").
--- iothread.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
iothread.c +4 -2
@@ -71,8 +71,6 @@ static void *iothread_run(void *opaque)g_main_loop_unref (loop);g_main_context_pop_thread_default (iothread->worker_context); -g_main_context_unref (iothread->worker_context); - iothread->worker_context =NULL ; } }
@@ -117,6 +115,10 @@ static void iothread_instance_finalize(Object *obj)IOThread *iothread =IOTHREAD (obj);iothread_stop (iothread); +if (iothread->worker_context) { +g_main_context_unref (iothread->worker_context); + iothread->worker_context =NULL ; + }qemu_cond_destroy (&iothread->init_done_cond);qemu_mutex_destroy (&iothread->init_done_lock);if (!iothread->ctx) {
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v3 1/4] qom: provide root container for internal objs
In reply to Peter XuQuoted 19 lines · Peter Xu
We have
object_get_objects_root()to keep user created objects, however no place for objects that will be used internally. Create such a container for internal objects.CC Andreas Färber <[email protected]>CC Markus Armbruster <[email protected]>CC Paolo Bonzini <[email protected]>Suggested-by Daniel P. Berrange <[email protected]>Signed-off-by Peter Xu <[email protected]>--- include/qom/object.h | 10 ++++++++++ qom/object.c | 11 +++++++++++ 2 files changed, 21 insertions(+)include/qom/object.h +0
-0
include/qom/object.h Object *object_get_root(void); · line 1220
Quoted 7 lines
include/qom/object.h +4
-0@@ -1214,6 +1214,16 @@ Object *object_get_root(void);Object *object_get_objects_root (void );/** +* object_get_internal_root: +* +* Get the container object that holds internally used object +* instances. This is the object at path "/internal-objects"
Does this comment need update?
Quoted 34 lines
include/qom/object.h +6
-0+* +* Returns: the internal object container +*/ +Object *object_get_internal_root (void ); + +/** * object_get_canonical_path_component: * * Returns: The final component in the object's canonical path. The canonical qom/object.c +11
-0@@ -1370,6 +1370,17 @@ Object *object_get_objects_root(void)return container_get (object_get_root (),"/objects" ); } +Object *object_get_internal_root (void ) +{ +static Object *internal_root; + +if (!internal_root) { + internal_root =object_new ("container" ); + } + +return internal_root; +} +static void object_get_child_property (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp)Signature · 1 line
2.7.4
Fam
Re: [Qemu-devel] [PATCH v3 2/4] iothread: provide helpers for internal use
In reply to Peter XuQuoted 56 lines · Peter Xu
IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.
Put all the internal used iothreads into the internal object container.
Signed-off-by Peter Xu <[email protected]>--- include/sysemu/iothread.h | 8 ++++++++ iothread.c | 16 ++++++++++++++++ 2 files changed, 24 insertions(+)include/sysemu/iothread.h +8
-0@@ -46,4 +46,12 @@ AioContext *iothread_get_aio_context(IOThread *iothread);void iothread_stop_all (void );GMainContext *iothread_get_g_main_context (IOThread *iothread); +/* +* Helpers used to allocate iothreads for internal use. These +* iothreads will not be seen by monitor clients when query using +* "query-iothreads". +*/ +IOThread *iothread_create (const char *id,Error **errp); +void iothread_destroy (IOThread *iothread); +#endif /* IOTHREAD_H */ iothread.c +16
-0@@ -354,3 +354,19 @@ GMainContext *iothread_get_g_main_context(IOThread *iothread)return iothread->worker_context; } + +IOThread *iothread_create (const char *id,Error **errp) +{ +Object *obj; + + obj =object_new_with_props (TYPE_IOTHREAD , +object_get_internal_root (), + id, errp,NULL ); + +return IOTHREAD (obj); +} + +void iothread_destroy (IOThread *iothread) +{ +object_unparent (OBJECT (iothread)); +}Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH v3 1/4] qom: provide root container for internal objs
In reply to Fam ZhengQuoted 22 lines · Fam Zheng
Quoted 21 lines · Peter Xu
We have
object_get_objects_root()to keep user created objects, however no place for objects that will be used internally. Create such a container for internal objects.CC Andreas Färber <[email protected]>CC Markus Armbruster <[email protected]>CC Paolo Bonzini <[email protected]>Suggested-by Daniel P. Berrange <[email protected]>Signed-off-by Peter Xu <[email protected]>--- include/qom/object.h | 10 ++++++++++ qom/object.c | 11 +++++++++++ 2 files changed, 21 insertions(+)include/qom/object.h +0
-0@@ -1214,6 +1214,16 @@ Object *object_get_root(void);Object *object_get_objects_root (void );
Quoted 7 lines
include/qom/object.h Object *object_get_root(void); · line 1220
Quoted 5 lines
include/qom/object.h +4
-0/** +* object_get_internal_root: +* +* Get the container object that holds internally used object +* instances. This is the object at path "/internal-objects" Does this comment need update?
Definitely. :)
Signature · 1 line
Peter Xu
Re: [Qemu-devel] [PATCH v3 1/4] qom: provide root container for internal objs
In reply to Peter Xuinclude/qom/object.h Object *object_get_root(void); · line 1220
Quoted 12 lines · Peter Xu
include/qom/object.h +4
-0@@ -1214,6 +1214,16 @@ Object *object_get_root(void);Object *object_get_objects_root (void );/** +* object_get_internal_root: +* +* Get the container object that holds internally used object +* instances. This is the object at path "/internal-objects"
When you update the comment, please mention that this root is for objects that must not be user-visible. This root is not exposed in the qom tree.
I think mentioning this will help others understand the purpose of the internal root.
Re: [Qemu-devel] [PATCH v3 2/4] iothread: provide helpers for internal use
In reply to Peter XuQuoted 11 lines · Peter Xu
IOThread is a general framework that contains IO loop environment and a real thread behind. It's also good to be used internally inside qemu. Provide some helpers for it to create iothreads to be used internally.
Put all the internal used iothreads into the internal object container.
Signed-off-by Peter Xu <[email protected]>--- include/sysemu/iothread.h | 8 ++++++++ iothread.c | 16 ++++++++++++++++ 2 files changed, 24 insertions(+)
Re: [Qemu-devel] [PATCH v3 1/4] qom: provide root container for internal objs
In reply to Stefan HajnocziQuoted 18 lines · Stefan Hajnoczi
include/qom/object.h Object *object_get_root(void); · line 1220
Quoted 12 lines · Peter Xu
include/qom/object.h +4
-0@@ -1214,6 +1214,16 @@ Object *object_get_root(void);Object *object_get_objects_root (void );/** +* object_get_internal_root: +* +* Get the container object that holds internally used object +* instances. This is the object at path "/internal-objects" When you update the comment, please mention that this root is for objects that must not be user-visible. This root is not exposed in the qom tree.
Quoted 2 lines
I think mentioning this will help others understand the purpose of the internal root.
Sure. Will do.
Signature · 1 line
Peter Xu