Skip to content

Commit 70ac26b

Browse files
vianplstefanhaRH
authored andcommitted
util/main-loop: Introduce the main loop into QOM
'event-loop-base' provides basic property handling for all 'AioContext' based event loops. So let's define a new 'MainLoopClass' that inherits from it. This will permit tweaking the main loop's properties through qapi as well as through the command line using the '-object' keyword[1]. Only one instance of 'MainLoopClass' might be created at any time. 'EventLoopBaseClass' learns a new callback, 'can_be_deleted()' so as to mark 'MainLoop' as non-deletable. [1] For example: -object main-loop,id=main-loop,aio-max-batch=<value> Signed-off-by: Nicolas Saenz Julienne <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Acked-by: Markus Armbruster <[email protected]> Message-id: [email protected] Signed-off-by: Stefan Hajnoczi <[email protected]>
1 parent 7d5983e commit 70ac26b

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

event-loop-base.c

+13
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,23 @@ static void event_loop_base_complete(UserCreatable *uc, Error **errp)
7373
}
7474
}
7575

76+
static bool event_loop_base_can_be_deleted(UserCreatable *uc)
77+
{
78+
EventLoopBaseClass *bc = EVENT_LOOP_BASE_GET_CLASS(uc);
79+
EventLoopBase *backend = EVENT_LOOP_BASE(uc);
80+
81+
if (bc->can_be_deleted) {
82+
return bc->can_be_deleted(backend);
83+
}
84+
85+
return true;
86+
}
87+
7688
static void event_loop_base_class_init(ObjectClass *klass, void *class_data)
7789
{
7890
UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
7991
ucc->complete = event_loop_base_complete;
92+
ucc->can_be_deleted = event_loop_base_can_be_deleted;
8093

8194
object_class_property_add(klass, "aio-max-batch", "int",
8295
event_loop_base_get_param,

include/qemu/main-loop.h

+10
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@
2626
#define QEMU_MAIN_LOOP_H
2727

2828
#include "block/aio.h"
29+
#include "qom/object.h"
30+
#include "sysemu/event-loop-base.h"
2931

3032
#define SIG_IPI SIGUSR1
3133

34+
#define TYPE_MAIN_LOOP "main-loop"
35+
OBJECT_DECLARE_TYPE(MainLoop, MainLoopClass, MAIN_LOOP)
36+
37+
struct MainLoop {
38+
EventLoopBase parent_obj;
39+
};
40+
typedef struct MainLoop MainLoop;
41+
3242
/**
3343
* qemu_init_main_loop: Set up the process so that it can run the main loop.
3444
*

include/sysemu/event-loop-base.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct EventLoopBaseClass {
2525

2626
void (*init)(EventLoopBase *base, Error **errp);
2727
void (*update_params)(EventLoopBase *base, Error **errp);
28+
bool (*can_be_deleted)(EventLoopBase *base);
2829
};
2930

3031
struct EventLoopBase {

meson.build

+2-1
Original file line numberDiff line numberDiff line change
@@ -3053,7 +3053,8 @@ libqemuutil = static_library('qemuutil',
30533053
sources: util_ss.sources() + stub_ss.sources() + genh,
30543054
dependencies: [util_ss.dependencies(), libm, threads, glib, socket, malloc, pixman])
30553055
qemuutil = declare_dependency(link_with: libqemuutil,
3056-
sources: genh + version_res)
3056+
sources: genh + version_res,
3057+
dependencies: [event_loop_base])
30573058

30583059
if have_system or have_user
30593060
decodetree = generator(find_program('scripts/decodetree.py'),

qapi/qom.json

+13
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,17 @@
540540
'*poll-grow': 'int',
541541
'*poll-shrink': 'int' } }
542542

543+
##
544+
# @MainLoopProperties:
545+
#
546+
# Properties for the main-loop object.
547+
#
548+
# Since: 7.1
549+
##
550+
{ 'struct': 'MainLoopProperties',
551+
'base': 'EventLoopBaseProperties',
552+
'data': {} }
553+
543554
##
544555
# @MemoryBackendProperties:
545556
#
@@ -830,6 +841,7 @@
830841
{ 'name': 'input-linux',
831842
'if': 'CONFIG_LINUX' },
832843
'iothread',
844+
'main-loop',
833845
{ 'name': 'memory-backend-epc',
834846
'if': 'CONFIG_LINUX' },
835847
'memory-backend-file',
@@ -895,6 +907,7 @@
895907
'input-linux': { 'type': 'InputLinuxProperties',
896908
'if': 'CONFIG_LINUX' },
897909
'iothread': 'IothreadProperties',
910+
'main-loop': 'MainLoopProperties',
898911
'memory-backend-epc': { 'type': 'MemoryBackendEpcProperties',
899912
'if': 'CONFIG_LINUX' },
900913
'memory-backend-file': 'MemoryBackendFileProperties',

util/main-loop.c

+56
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qemu/error-report.h"
3434
#include "qemu/queue.h"
3535
#include "qemu/compiler.h"
36+
#include "qom/object.h"
3637

3738
#ifndef _WIN32
3839
#include <sys/wait.h>
@@ -184,6 +185,61 @@ int qemu_init_main_loop(Error **errp)
184185
return 0;
185186
}
186187

188+
static void main_loop_update_params(EventLoopBase *base, Error **errp)
189+
{
190+
if (!qemu_aio_context) {
191+
error_setg(errp, "qemu aio context not ready");
192+
return;
193+
}
194+
195+
aio_context_set_aio_params(qemu_aio_context, base->aio_max_batch, errp);
196+
}
197+
198+
MainLoop *mloop;
199+
200+
static void main_loop_init(EventLoopBase *base, Error **errp)
201+
{
202+
MainLoop *m = MAIN_LOOP(base);
203+
204+
if (mloop) {
205+
error_setg(errp, "only one main-loop instance allowed");
206+
return;
207+
}
208+
209+
main_loop_update_params(base, errp);
210+
211+
mloop = m;
212+
return;
213+
}
214+
215+
static bool main_loop_can_be_deleted(EventLoopBase *base)
216+
{
217+
return false;
218+
}
219+
220+
static void main_loop_class_init(ObjectClass *oc, void *class_data)
221+
{
222+
EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(oc);
223+
224+
bc->init = main_loop_init;
225+
bc->update_params = main_loop_update_params;
226+
bc->can_be_deleted = main_loop_can_be_deleted;
227+
}
228+
229+
static const TypeInfo main_loop_info = {
230+
.name = TYPE_MAIN_LOOP,
231+
.parent = TYPE_EVENT_LOOP_BASE,
232+
.class_init = main_loop_class_init,
233+
.instance_size = sizeof(MainLoop),
234+
};
235+
236+
static void main_loop_register_types(void)
237+
{
238+
type_register_static(&main_loop_info);
239+
}
240+
241+
type_init(main_loop_register_types)
242+
187243
static int max_priority;
188244

189245
#ifndef _WIN32

0 commit comments

Comments
 (0)