event-poll.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Event loop with file descriptor polling
  3. *
  4. * Copyright 2012 IBM, Corp.
  5. * Copyright 2012 Red Hat, Inc. and/or its affiliates
  6. *
  7. * Authors:
  8. * Stefan Hajnoczi <stefanha@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. *
  13. */
  14. #include <sys/epoll.h>
  15. #include "hw/dataplane/event-poll.h"
  16. /* Add an event notifier and its callback for polling */
  17. void event_poll_add(EventPoll *poll, EventHandler *handler,
  18. EventNotifier *notifier, EventCallback *callback)
  19. {
  20. struct epoll_event event = {
  21. .events = EPOLLIN,
  22. .data.ptr = handler,
  23. };
  24. handler->notifier = notifier;
  25. handler->callback = callback;
  26. if (epoll_ctl(poll->epoll_fd, EPOLL_CTL_ADD,
  27. event_notifier_get_fd(notifier), &event) != 0) {
  28. fprintf(stderr, "failed to add event handler to epoll: %m\n");
  29. exit(1);
  30. }
  31. }
  32. /* Event callback for stopping event_poll() */
  33. static void handle_stop(EventHandler *handler)
  34. {
  35. /* Do nothing */
  36. }
  37. void event_poll_init(EventPoll *poll)
  38. {
  39. /* Create epoll file descriptor */
  40. poll->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
  41. if (poll->epoll_fd < 0) {
  42. fprintf(stderr, "epoll_create1 failed: %m\n");
  43. exit(1);
  44. }
  45. /* Set up stop notifier */
  46. if (event_notifier_init(&poll->stop_notifier, 0) < 0) {
  47. fprintf(stderr, "failed to init stop notifier\n");
  48. exit(1);
  49. }
  50. event_poll_add(poll, &poll->stop_handler,
  51. &poll->stop_notifier, handle_stop);
  52. }
  53. void event_poll_cleanup(EventPoll *poll)
  54. {
  55. event_notifier_cleanup(&poll->stop_notifier);
  56. close(poll->epoll_fd);
  57. poll->epoll_fd = -1;
  58. }
  59. /* Block until the next event and invoke its callback */
  60. void event_poll(EventPoll *poll)
  61. {
  62. EventHandler *handler;
  63. struct epoll_event event;
  64. int nevents;
  65. /* Wait for the next event. Only do one event per call to keep the
  66. * function simple, this could be changed later. */
  67. do {
  68. nevents = epoll_wait(poll->epoll_fd, &event, 1, -1);
  69. } while (nevents < 0 && errno == EINTR);
  70. if (unlikely(nevents != 1)) {
  71. fprintf(stderr, "epoll_wait failed: %m\n");
  72. exit(1); /* should never happen */
  73. }
  74. /* Find out which event handler has become active */
  75. handler = event.data.ptr;
  76. /* Clear the eventfd */
  77. event_notifier_test_and_clear(handler->notifier);
  78. /* Handle the event */
  79. handler->callback(handler);
  80. }
  81. /* Stop event_poll()
  82. *
  83. * This function can be used from another thread.
  84. */
  85. void event_poll_notify(EventPoll *poll)
  86. {
  87. event_notifier_set(&poll->stop_notifier);
  88. }