event_notifier.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * event notifier support
  3. *
  4. * Copyright Red Hat, Inc. 2010
  5. *
  6. * Authors:
  7. * Michael S. Tsirkin <mst@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu-common.h"
  13. #include "event_notifier.h"
  14. #include "qemu-char.h"
  15. #ifdef CONFIG_EVENTFD
  16. #include <sys/eventfd.h>
  17. #endif
  18. void event_notifier_init_fd(EventNotifier *e, int fd)
  19. {
  20. e->fd = fd;
  21. }
  22. int event_notifier_init(EventNotifier *e, int active)
  23. {
  24. #ifdef CONFIG_EVENTFD
  25. int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
  26. if (fd < 0)
  27. return -errno;
  28. e->fd = fd;
  29. return 0;
  30. #else
  31. return -ENOSYS;
  32. #endif
  33. }
  34. void event_notifier_cleanup(EventNotifier *e)
  35. {
  36. close(e->fd);
  37. }
  38. int event_notifier_get_fd(EventNotifier *e)
  39. {
  40. return e->fd;
  41. }
  42. int event_notifier_set_handler(EventNotifier *e,
  43. EventNotifierHandler *handler)
  44. {
  45. return qemu_set_fd_handler(e->fd, (IOHandler *)handler, NULL, e);
  46. }
  47. int event_notifier_set(EventNotifier *e)
  48. {
  49. uint64_t value = 1;
  50. int r = write(e->fd, &value, sizeof(value));
  51. return r == sizeof(value);
  52. }
  53. int event_notifier_test_and_clear(EventNotifier *e)
  54. {
  55. uint64_t value;
  56. int r = read(e->fd, &value, sizeof(value));
  57. return r == sizeof(value);
  58. }