notify.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qemu/notify.h"
  17. void notifier_list_init(NotifierList *list)
  18. {
  19. QLIST_INIT(&list->notifiers);
  20. }
  21. void notifier_list_add(NotifierList *list, Notifier *notifier)
  22. {
  23. QLIST_INSERT_HEAD(&list->notifiers, notifier, node);
  24. }
  25. void notifier_remove(Notifier *notifier)
  26. {
  27. QLIST_REMOVE(notifier, node);
  28. }
  29. void notifier_list_notify(NotifierList *list, void *data)
  30. {
  31. Notifier *notifier, *next;
  32. QLIST_FOREACH_SAFE(notifier, &list->notifiers, node, next) {
  33. notifier->notify(notifier, data);
  34. }
  35. }
  36. bool notifier_list_empty(NotifierList *list)
  37. {
  38. return QLIST_EMPTY(&list->notifiers);
  39. }
  40. void notifier_with_return_list_init(NotifierWithReturnList *list)
  41. {
  42. QLIST_INIT(&list->notifiers);
  43. }
  44. void notifier_with_return_list_add(NotifierWithReturnList *list,
  45. NotifierWithReturn *notifier)
  46. {
  47. QLIST_INSERT_HEAD(&list->notifiers, notifier, node);
  48. }
  49. void notifier_with_return_remove(NotifierWithReturn *notifier)
  50. {
  51. QLIST_REMOVE(notifier, node);
  52. }
  53. int notifier_with_return_list_notify(NotifierWithReturnList *list, void *data)
  54. {
  55. NotifierWithReturn *notifier, *next;
  56. int ret = 0;
  57. QLIST_FOREACH_SAFE(notifier, &list->notifiers, node, next) {
  58. ret = notifier->notify(notifier, data);
  59. if (ret != 0) {
  60. break;
  61. }
  62. }
  63. return ret;
  64. }