mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
b8c4d042bd
This seems to be a knock-on effect of my recent reworking of the SSH code to be based around queues and callbacks. The loop iteration function in uxsftp.c (ssh_sftp_do_select) would keep going round its select loop until something had happened on one of its file descriptors, and then return to the caller in the assumption that the resulting data might have triggered whatever condition the caller was waiting for - and if not, then the caller checks, finds nothing interesting has happened, and resumes looping with no harm done. But now, when something happens on an fd, it doesn't _synchronously_ trigger the follow-up condition PSFTP was waiting for (which, at startup time, happens to be back->sendok() starting to return TRUE). Instead, it schedules a callback, which will schedule a callback, which ... ends up setting that flag. But by that time, the loop function has already returned, the caller has found nothing interesting and resumed looping, and _now_ the interesting thing happens but it's too late because ssh_sftp_do_select will wait until the next file descriptor activity before it next returns. Solution: give run_toplevel_callbacks a return value which says whether it's actually done something, and if so, return immediately in case that was the droid the caller was looking for. As it were.
132 lines
3.1 KiB
C
132 lines
3.1 KiB
C
/*
|
|
* Facility for queueing callback functions to be run from the
|
|
* top-level event loop after the current top-level activity finishes.
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "putty.h"
|
|
|
|
struct callback {
|
|
struct callback *next;
|
|
|
|
toplevel_callback_fn_t fn;
|
|
void *ctx;
|
|
};
|
|
|
|
struct callback *cbcurr = NULL, *cbhead = NULL, *cbtail = NULL;
|
|
|
|
toplevel_callback_notify_fn_t notify_frontend = NULL;
|
|
void *frontend = NULL;
|
|
|
|
void request_callback_notifications(toplevel_callback_notify_fn_t fn,
|
|
void *fr)
|
|
{
|
|
notify_frontend = fn;
|
|
frontend = fr;
|
|
}
|
|
|
|
static void run_idempotent_callback(void *ctx)
|
|
{
|
|
struct IdempotentCallback *ic = (struct IdempotentCallback *)ctx;
|
|
ic->queued = FALSE;
|
|
ic->fn(ic->ctx);
|
|
}
|
|
|
|
void queue_idempotent_callback(struct IdempotentCallback *ic)
|
|
{
|
|
if (ic->queued)
|
|
return;
|
|
ic->queued = TRUE;
|
|
queue_toplevel_callback(run_idempotent_callback, ic);
|
|
}
|
|
|
|
void delete_callbacks_for_context(void *ctx)
|
|
{
|
|
struct callback *newhead, *newtail;
|
|
|
|
newhead = newtail = NULL;
|
|
while (cbhead) {
|
|
struct callback *cb = cbhead;
|
|
cbhead = cbhead->next;
|
|
if (cb->ctx == ctx ||
|
|
(cb->fn == run_idempotent_callback &&
|
|
((struct IdempotentCallback *)cb->ctx)->ctx == ctx)) {
|
|
sfree(cb);
|
|
} else {
|
|
if (!newhead)
|
|
newhead = cb;
|
|
else
|
|
newtail->next = cb;
|
|
|
|
newtail = cb;
|
|
}
|
|
}
|
|
|
|
cbhead = newhead;
|
|
cbtail = newtail;
|
|
}
|
|
|
|
void queue_toplevel_callback(toplevel_callback_fn_t fn, void *ctx)
|
|
{
|
|
struct callback *cb;
|
|
|
|
cb = snew(struct callback);
|
|
cb->fn = fn;
|
|
cb->ctx = ctx;
|
|
|
|
/*
|
|
* If the front end has requested notification of pending
|
|
* callbacks, and we didn't already have one queued, let it know
|
|
* we do have one now.
|
|
*
|
|
* If cbcurr is non-NULL, i.e. we are actually in the middle of
|
|
* executing a callback right now, then we count that as the queue
|
|
* already having been non-empty. That saves the front end getting
|
|
* a constant stream of needless re-notifications if the last
|
|
* callback keeps re-scheduling itself.
|
|
*/
|
|
if (notify_frontend && !cbhead && !cbcurr)
|
|
notify_frontend(frontend);
|
|
|
|
if (cbtail)
|
|
cbtail->next = cb;
|
|
else
|
|
cbhead = cb;
|
|
cbtail = cb;
|
|
cb->next = NULL;
|
|
}
|
|
|
|
int run_toplevel_callbacks(void)
|
|
{
|
|
int done_something = FALSE;
|
|
|
|
if (cbhead) {
|
|
/*
|
|
* Transfer the head callback into cbcurr to indicate that
|
|
* it's being executed. Then operations which transform the
|
|
* queue, like delete_callbacks_for_context, can proceed as if
|
|
* it's not there.
|
|
*/
|
|
cbcurr = cbhead;
|
|
cbhead = cbhead->next;
|
|
if (!cbhead)
|
|
cbtail = NULL;
|
|
|
|
/*
|
|
* Now run the callback, and then clear it out of cbcurr.
|
|
*/
|
|
cbcurr->fn(cbcurr->ctx);
|
|
sfree(cbcurr);
|
|
cbcurr = NULL;
|
|
|
|
done_something = TRUE;
|
|
}
|
|
return done_something;
|
|
}
|
|
|
|
int toplevel_callback_pending(void)
|
|
{
|
|
return cbcurr != NULL || cbhead != NULL;
|
|
}
|