callback.c 3.2 KB

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