In order that people can use formats like #D in #() in the status line

and not have to wait for an update when they change pane, we allow
commands to run more than once a second if the expanded form
changes. Unfortunately this can mean them being run far too often
(pretty much continually) when multiple clients exist, because some
formats (including #D) will always differ between clients.

To avoid this, give each client its own tree of jobs which means that
the same command will be different instances for each client - similar
to how we have the tag to separate commands for different panes.

GitHub issue 889; test case reported by Paul Johnson.
This commit is contained in:
nicm
2017-05-01 12:20:55 +00:00
parent a2dd7daf4e
commit 0ccfb61bb0
17 changed files with 82 additions and 32 deletions

View File

@ -76,6 +76,7 @@ static void format_defaults_winlink(struct format_tree *,
/* Entry in format job tree. */
struct format_job {
struct client *client;
u_int tag;
const char *cmd;
const char *expanded;
@ -128,6 +129,7 @@ struct format_tree {
struct session *s;
struct window_pane *wp;
struct client *client;
u_int tag;
int flags;
@ -236,7 +238,6 @@ format_job_complete(struct job *job)
struct format_job *fj = job->data;
char *line, *buf;
size_t len;
struct client *c;
fj->job = NULL;
@ -258,8 +259,8 @@ format_job_complete(struct job *job)
free(buf);
if (fj->status) {
TAILQ_FOREACH(c, &clients, entry)
server_status_client(c);
if (fj->client != NULL)
server_status_client(fj->client);
fj->status = 0;
}
}
@ -268,22 +269,33 @@ format_job_complete(struct job *job)
static char *
format_job_get(struct format_tree *ft, const char *cmd)
{
struct format_job_tree *jobs;
struct format_job fj0, *fj;
time_t t;
char *expanded;
int force;
if (ft->client == NULL)
jobs = &format_jobs;
else if (ft->client->jobs != NULL)
jobs = ft->client->jobs;
else {
jobs = ft->client->jobs = xmalloc(sizeof *ft->client->jobs);
RB_INIT(jobs);
}
fj0.tag = ft->tag;
fj0.cmd = cmd;
if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL) {
if ((fj = RB_FIND(format_job_tree, jobs, &fj0)) == NULL) {
fj = xcalloc(1, sizeof *fj);
fj->client = ft->client;
fj->tag = ft->tag;
fj->cmd = xstrdup(cmd);
fj->expanded = NULL;
xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
RB_INSERT(format_job_tree, &format_jobs, fj);
RB_INSERT(format_job_tree, jobs, fj);
}
expanded = format_expand(ft, cmd);
@ -314,17 +326,16 @@ format_job_get(struct format_tree *ft, const char *cmd)
/* Remove old jobs. */
static void
format_job_timer(__unused int fd, __unused short events, __unused void *arg)
format_job_tidy(struct format_job_tree *jobs, int force)
{
struct format_job *fj, *fj1;
time_t now;
struct timeval tv = { .tv_sec = 60 };
now = time(NULL);
RB_FOREACH_SAFE(fj, format_job_tree, &format_jobs, fj1) {
if (fj->last > now || now - fj->last < 3600)
RB_FOREACH_SAFE(fj, format_job_tree, jobs, fj1) {
if (!force && (fj->last > now || now - fj->last < 3600))
continue;
RB_REMOVE(format_job_tree, &format_jobs, fj);
RB_REMOVE(format_job_tree, jobs, fj);
log_debug("%s: %s", __func__, fj->cmd);
@ -337,6 +348,29 @@ format_job_timer(__unused int fd, __unused short events, __unused void *arg)
free(fj);
}
}
/* Remove old jobs for client. */
void
format_lost_client(struct client *c)
{
if (c->jobs != NULL)
format_job_tidy(c->jobs, 1);
free(c->jobs);
}
/* Remove old jobs periodically. */
static void
format_job_timer(__unused int fd, __unused short events, __unused void *arg)
{
struct client *c;
struct timeval tv = { .tv_sec = 60 };
format_job_tidy(&format_jobs, 0);
TAILQ_FOREACH(c, &clients, entry) {
if (c->jobs != NULL)
format_job_tidy(c->jobs, 0);
}
evtimer_del(&format_job_event);
evtimer_add(&format_job_event, &tv);
@ -533,7 +567,7 @@ format_merge(struct format_tree *ft, struct format_tree *from)
/* Create a new tree. */
struct format_tree *
format_create(struct cmdq_item *item, int tag, int flags)
format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
{
struct format_tree *ft;
@ -545,6 +579,11 @@ format_create(struct cmdq_item *item, int tag, int flags)
ft = xcalloc(1, sizeof *ft);
RB_INIT(&ft->tree);
if (c != NULL) {
ft->client = c;
ft->client->references++;
}
ft->tag = tag;
ft->flags = flags;
@ -577,6 +616,8 @@ format_free(struct format_tree *ft)
free(fe);
}
if (ft->client != NULL)
server_client_unref(ft->client);
free(ft);
}
@ -1099,7 +1140,10 @@ format_single(struct cmdq_item *item, const char *fmt, struct client *c,
struct format_tree *ft;
char *expanded;
ft = format_create(item, FORMAT_NONE, 0);
if (item != NULL)
ft = format_create(item->client, item, FORMAT_NONE, 0);
else
ft = format_create(NULL, item, FORMAT_NONE, 0);
format_defaults(ft, c, s, wl, wp);
expanded = format_expand(ft, fmt);