callback.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Facility for queueing callback functions to be run from the
  3. * top-level event loop after the current top-level activity finishes.
  4. */
  5. #include <stddef.h>
  6. #include "putty.h"
  7. struct callback {
  8. struct callback *next;
  9. toplevel_callback_fn_t fn;
  10. void *ctx;
  11. };
  12. struct callback *cbcurr = NULL, *cbhead = NULL, *cbtail = NULL;
  13. toplevel_callback_notify_fn_t notify_frontend = NULL;
  14. void *frontend = NULL;
  15. void request_callback_notifications(toplevel_callback_notify_fn_t fn,
  16. void *fr)
  17. {
  18. MPEXT_PUTTY_SECTION_ENTER;
  19. notify_frontend = fn;
  20. frontend = fr;
  21. MPEXT_PUTTY_SECTION_LEAVE;
  22. }
  23. static void run_idempotent_callback(void *ctx)
  24. {
  25. struct IdempotentCallback *ic = (struct IdempotentCallback *)ctx;
  26. ic->queued = FALSE;
  27. ic->fn(ic->ctx);
  28. }
  29. void queue_idempotent_callback(struct IdempotentCallback *ic)
  30. {
  31. if (ic->queued)
  32. return;
  33. ic->queued = TRUE;
  34. queue_toplevel_callback(run_idempotent_callback, ic);
  35. }
  36. void delete_callbacks_for_context(void *ctx)
  37. {
  38. struct callback *newhead, *newtail;
  39. newhead = newtail = NULL;
  40. while (cbhead) {
  41. struct callback *cb = cbhead;
  42. cbhead = cbhead->next;
  43. if (cb->ctx == ctx ||
  44. (cb->fn == run_idempotent_callback &&
  45. ((struct IdempotentCallback *)cb->ctx)->ctx == ctx)) {
  46. sfree(cb);
  47. } else {
  48. if (!newhead)
  49. newhead = cb;
  50. else
  51. newtail->next = cb;
  52. newtail = cb;
  53. }
  54. }
  55. cbhead = newhead;
  56. cbtail = newtail;
  57. }
  58. void queue_toplevel_callback(toplevel_callback_fn_t fn, void *ctx)
  59. {
  60. struct callback *cb;
  61. MPEXT_PUTTY_SECTION_ENTER;
  62. cb = snew(struct callback);
  63. cb->fn = fn;
  64. cb->ctx = ctx;
  65. /*
  66. * If the front end has requested notification of pending
  67. * callbacks, and we didn't already have one queued, let it know
  68. * we do have one now.
  69. *
  70. * If cbcurr is non-NULL, i.e. we are actually in the middle of
  71. * executing a callback right now, then we count that as the queue
  72. * already having been non-empty. That saves the front end getting
  73. * a constant stream of needless re-notifications if the last
  74. * callback keeps re-scheduling itself.
  75. */
  76. if (notify_frontend && !cbhead && !cbcurr)
  77. notify_frontend(frontend);
  78. if (cbtail)
  79. cbtail->next = cb;
  80. else
  81. cbhead = cb;
  82. cbtail = cb;
  83. cb->next = NULL;
  84. MPEXT_PUTTY_SECTION_LEAVE;
  85. }
  86. int run_toplevel_callbacks(void)
  87. {
  88. int done_something = FALSE;
  89. MPEXT_PUTTY_SECTION_ENTER;
  90. if (cbhead) {
  91. /*
  92. * Transfer the head callback into cbcurr to indicate that
  93. * it's being executed. Then operations which transform the
  94. * queue, like delete_callbacks_for_context, can proceed as if
  95. * it's not there.
  96. */
  97. cbcurr = cbhead;
  98. cbhead = cbhead->next;
  99. if (!cbhead)
  100. cbtail = NULL;
  101. /*
  102. * Now run the callback, and then clear it out of cbcurr.
  103. */
  104. cbcurr->fn(cbcurr->ctx);
  105. sfree(cbcurr);
  106. cbcurr = NULL;
  107. done_something = TRUE;
  108. }
  109. MPEXT_PUTTY_SECTION_LEAVE;
  110. return done_something;
  111. }
  112. int toplevel_callback_pending(void)
  113. {
  114. // MP does not have to be guarded
  115. return cbcurr != NULL || cbhead != NULL;
  116. }