handle-wait.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * handle-wait.c: Manage a collection of HANDLEs to wait for (in a
  3. * WaitFor{Single,Multiple}Objects sense), each with a callback to be
  4. * called when it's activated. Tracks the list, and provides an API to
  5. * event loops that let them get a list of things to wait for and a
  6. * way to call back to here when one of them does something.
  7. */
  8. /*
  9. * TODO: currently this system can't cope with more than
  10. * MAXIMUM_WAIT_OBJECTS (= 64) handles at a time. It enforces that by
  11. * assertion, so we'll at least find out if that assumption is ever
  12. * violated.
  13. *
  14. * It should be OK for the moment. As of 2021-05-24, the only uses of
  15. * this system are by the ConPTY backend (just once, to watch for its
  16. * subprocess terminating); by Pageant (for the event that the
  17. * WM_COPYDATA subthread uses to signal the main thread); and by
  18. * named-pipe-server.c (once per named-pipe server, of which there is
  19. * one in Pageant and one in connection-sharing upstreams). So the
  20. * total number of handles has a pretty small upper bound.
  21. *
  22. * But sooner or later, I'm sure we'll find a reason why we really
  23. * need to watch a squillion handles at once. When that happens, I
  24. * can't see any alternative to setting up some kind of tree of
  25. * subthreads in this module, each one condensing 64 of our handles
  26. * into one, by doing its own WaitForMultipleObjects and setting an
  27. * event object to indicate that one of them did something. It'll be
  28. * horribly ugly.
  29. */
  30. #include "putty.h"
  31. struct HandleWait {
  32. HANDLE handle;
  33. handle_wait_callback_fn_t callback;
  34. void *callback_ctx;
  35. int index; /* sort key for tree234 */
  36. };
  37. struct HandleWaitListInner {
  38. HandleWait *hws[MAXIMUM_WAIT_OBJECTS];
  39. HANDLE handles[MAXIMUM_WAIT_OBJECTS];
  40. struct HandleWaitList hwl;
  41. };
  42. static int handlewait_cmp(void *av, void *bv)
  43. {
  44. HandleWait *a = (HandleWait *)av, *b = (HandleWait *)bv;
  45. if (a->index < b->index)
  46. return -1;
  47. if (a->index > b->index)
  48. return +1;
  49. return 0;
  50. }
  51. static tree234 *handlewaits_tree_real;
  52. static inline tree234 *ensure_handlewaits_tree_exists(void)
  53. {
  54. if (!handlewaits_tree_real)
  55. handlewaits_tree_real = newtree234(handlewait_cmp);
  56. return handlewaits_tree_real;
  57. }
  58. static int allocate_index(void)
  59. {
  60. tree234 *t = ensure_handlewaits_tree_exists();
  61. search234_state st[1];
  62. search234_start(st, t);
  63. while (st->element) {
  64. HandleWait *hw = (HandleWait *)st->element;
  65. if (st->index < hw->index) {
  66. /* There are unused index slots to the left of this element */
  67. search234_step(st, -1);
  68. } else {
  69. assert(st->index == hw->index);
  70. search234_step(st, +1);
  71. }
  72. }
  73. return st->index;
  74. }
  75. HandleWait *add_handle_wait(HANDLE h, handle_wait_callback_fn_t callback,
  76. void *callback_ctx)
  77. {
  78. HandleWait *hw = snew(HandleWait);
  79. hw->handle = h;
  80. hw->callback = callback;
  81. hw->callback_ctx = callback_ctx;
  82. tree234 *t = ensure_handlewaits_tree_exists();
  83. hw->index = allocate_index();
  84. HandleWait *added = add234(t, hw);
  85. assert(added == hw);
  86. return hw;
  87. }
  88. void delete_handle_wait(HandleWait *hw)
  89. {
  90. tree234 *t = ensure_handlewaits_tree_exists();
  91. HandleWait *deleted = del234(t, hw);
  92. assert(deleted == hw);
  93. sfree(hw);
  94. }
  95. HandleWaitList *get_handle_wait_list(void)
  96. {
  97. tree234 *t = ensure_handlewaits_tree_exists();
  98. struct HandleWaitListInner *hwli = snew(struct HandleWaitListInner);
  99. size_t n = 0;
  100. HandleWait *hw;
  101. for (int i = 0; (hw = index234(t, i)) != NULL; i++) {
  102. assert(n < MAXIMUM_WAIT_OBJECTS);
  103. hwli->hws[n] = hw;
  104. hwli->hwl.handles[n] = hw->handle;
  105. n++;
  106. }
  107. hwli->hwl.nhandles = n;
  108. return &hwli->hwl;
  109. }
  110. void handle_wait_activate(HandleWaitList *hwl, int index)
  111. {
  112. struct HandleWaitListInner *hwli =
  113. container_of(hwl, struct HandleWaitListInner, hwl);
  114. assert(0 <= index);
  115. assert(index < hwli->hwl.nhandles);
  116. HandleWait *hw = hwli->hws[index];
  117. hw->callback(hw->callback_ctx);
  118. }
  119. void handle_wait_list_free(HandleWaitList *hwl)
  120. {
  121. struct HandleWaitListInner *hwli =
  122. container_of(hwl, struct HandleWaitListInner, hwl);
  123. sfree(hwli);
  124. }