pollhandler.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: Apache-2.0
  2. /*
  3. * Copyright (C) 2013 Intel Corporation
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <poll.h>
  9. #include "pollhandler.h"
  10. /*
  11. * Code that allows to poll multiply file descriptors for events
  12. * File descriptors can be added and removed at runtime
  13. *
  14. * Call poll_register_fd function first to add file descriptors to monitor
  15. * Then call poll_dispatch_loop that will poll all registered file descriptors
  16. * as long as they are not unregistered.
  17. *
  18. * When event happen on given fd appropriate user supplied handler is called
  19. */
  20. /* Maximum number of files to monitor */
  21. #define MAX_OPEN_FD 10
  22. /* Storage for pollfd structures for monitored file descriptors */
  23. static struct pollfd fds[MAX_OPEN_FD];
  24. static poll_handler fds_handler[MAX_OPEN_FD];
  25. /* Number of registered file descriptors */
  26. static int fds_count = 0;
  27. /*
  28. * Function polls file descriptor in loop and calls appropriate handler
  29. * on event. Function returns when there is no more file descriptor to
  30. * monitor
  31. */
  32. void poll_dispatch_loop(void)
  33. {
  34. while (fds_count > 0) {
  35. int i;
  36. int cur_fds_count = fds_count;
  37. int ready = poll(fds, fds_count, 1000);
  38. for (i = 0; i < fds_count && ready > 0; ++i) {
  39. if (fds[i].revents == 0)
  40. continue;
  41. fds_handler[i](fds + i);
  42. ready--;
  43. /*
  44. * If handler was remove from table
  45. * just skip the rest and poll again
  46. * This is due to reordering of tables in
  47. * register/unregister functions
  48. */
  49. if (cur_fds_count != fds_count)
  50. break;
  51. }
  52. }
  53. }
  54. /*
  55. * Registers file descriptor to be monitored for events (see man poll(2))
  56. * for events.
  57. *
  58. * return non negative value on success
  59. * -EMFILE when there are to much descriptors
  60. */
  61. int poll_register_fd(int fd, short events, poll_handler ph)
  62. {
  63. if (fds_count >= MAX_OPEN_FD)
  64. return -EMFILE;
  65. fds_handler[fds_count] = ph;
  66. fds[fds_count].fd = fd;
  67. fds[fds_count].events = events;
  68. fds_count++;
  69. return fds_count;
  70. }
  71. /*
  72. * Unregisters file descriptor
  73. * Both fd and ph must match previously registered data
  74. *
  75. * return 0 if unregister succeeded
  76. * -EBADF if arguments do not match any register handler
  77. */
  78. int poll_unregister_fd(int fd, poll_handler ph)
  79. {
  80. int i;
  81. for (i = 0; i < fds_count; ++i) {
  82. if (fds_handler[i] == ph && fds[i].fd == fd) {
  83. fds_count--;
  84. if (i < fds_count) {
  85. fds[i].fd = fds[fds_count].fd;
  86. fds[i].events = fds[fds_count].events;
  87. fds_handler[i] = fds_handler[fds_count];
  88. }
  89. return 0;
  90. }
  91. }
  92. return -EBADF;
  93. }