xref: /qemu/include/qemu/notify.h (revision 9c707525)
1 /*
2  * Notifier lists
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef QEMU_NOTIFY_H
15 #define QEMU_NOTIFY_H
16 
17 #include "qemu/queue.h"
18 
19 typedef struct Notifier Notifier;
20 
21 struct Notifier
22 {
23     void (*notify)(Notifier *notifier, void *data);
24     QLIST_ENTRY(Notifier) node;
25 };
26 
27 typedef struct NotifierList
28 {
29     QLIST_HEAD(, Notifier) notifiers;
30 } NotifierList;
31 
32 #define NOTIFIER_LIST_INITIALIZER(head) \
33     { QLIST_HEAD_INITIALIZER((head).notifiers) }
34 
35 void notifier_list_init(NotifierList *list);
36 
37 void notifier_list_add(NotifierList *list, Notifier *notifier);
38 
39 void notifier_remove(Notifier *notifier);
40 
41 void notifier_list_notify(NotifierList *list, void *data);
42 
43 bool notifier_list_empty(NotifierList *list);
44 
45 /* Same as Notifier but allows .notify() to return errors */
46 typedef struct NotifierWithReturn NotifierWithReturn;
47 
48 /* Return int to allow for different failure modes and recovery actions */
49 typedef int (*NotifierWithReturnFunc)(NotifierWithReturn *notifier, void *data,
50                                       Error **errp);
51 
52 struct NotifierWithReturn {
53     /**
54      * Return 0 on success (next notifier will be invoked), otherwise
55      * notifier_with_return_list_notify() will stop and return the value.
56      */
57     NotifierWithReturnFunc notify;
58     QLIST_ENTRY(NotifierWithReturn) node;
59 };
60 
61 typedef struct NotifierWithReturnList {
62     QLIST_HEAD(, NotifierWithReturn) notifiers;
63 } NotifierWithReturnList;
64 
65 #define NOTIFIER_WITH_RETURN_LIST_INITIALIZER(head) \
66     { QLIST_HEAD_INITIALIZER((head).notifiers) }
67 
68 void notifier_with_return_list_init(NotifierWithReturnList *list);
69 
70 void notifier_with_return_list_add(NotifierWithReturnList *list,
71                                    NotifierWithReturn *notifier);
72 
73 void notifier_with_return_remove(NotifierWithReturn *notifier);
74 
75 int notifier_with_return_list_notify(NotifierWithReturnList *list,
76                                      void *data, Error **errp);
77 
78 #endif
79