gui: Move some variables into struct status_task.
authorAndre Noll <maan@systemlinux.org>
Sat, 4 Jan 2014 06:32:04 +0000 (06:32 +0000)
committerAndre Noll <maan@systemlinux.org>
Sun, 4 May 2014 13:48:55 +0000 (15:48 +0200)
pre_select and post_select methods should never define static variables
but store their state in the context structure which also contains
the task structure.

This commit moves the static variables of status_post_select() and the
global variables next_exec and stat_pipe into the context structure
of the status task.

gui.c

diff --git a/gui.c b/gui.c
index 182545c..c9ee17f 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -47,7 +47,6 @@ static unsigned scroll_position;
 static pid_t cmd_pid;
 
 static int command_fds[2] = {-1, -1};
-static int stat_pipe = -1;
 static struct gui_args_info conf;
 static int loglevel;
 
@@ -126,6 +125,10 @@ struct input_task {
 
 struct status_task {
        struct task task;
+       char *buf;
+       int bufsize, loaded;
+       struct timeval next_exec;
+       int fd;
 };
 
 struct cmd_task {
@@ -572,29 +575,28 @@ static void clear_all_items(void)
        }
 }
 
-static struct timeval next_exec;
-
-static void status_pre_select(struct sched *s, __a_unused struct task *t)
+static void status_pre_select(struct sched *s, struct task *t)
 {
-       if (stat_pipe >= 0)
-               return para_fd_set(stat_pipe, &s->rfds, &s->max_fileno);
-       sched_request_barrier_or_min_delay(&next_exec, s);
+       struct status_task *st = container_of(t, struct status_task, task);
+
+       if (st->fd >= 0)
+               return para_fd_set(st->fd, &s->rfds, &s->max_fileno);
+       sched_request_barrier_or_min_delay(&st->next_exec, s);
 }
 
-static int status_post_select(struct sched *s, __a_unused struct task *t)
+static int status_post_select(struct sched *s, struct task *t)
 {
-       static char *buf;
-       static int bufsize, loaded;
+       struct status_task *st = container_of(t, struct status_task, task);
        size_t sz;
        pid_t pid;
        int ret, ret2;
 
-       if (stat_pipe < 0) {
+       if (st->fd < 0) {
                int fds[3] = {0, 1, 0};
                /* Avoid busy loop */
-               if (tv_diff(&next_exec, now, NULL) > 0)
+               if (tv_diff(&st->next_exec, now, NULL) > 0)
                        return 0;
-               next_exec.tv_sec = now->tv_sec + 2;
+               st->next_exec.tv_sec = now->tv_sec + 2;
                ret = para_exec_cmdline_pid(&pid, conf.stat_cmd_arg, fds);
                if (ret < 0)
                        return 0;
@@ -603,28 +605,28 @@ static int status_post_select(struct sched *s, __a_unused struct task *t)
                        close(fds[1]);
                        return 0;
                }
-               stat_pipe = fds[1];
+               st->fd = fds[1];
                return 0;
        }
 
-       if (loaded >= bufsize) {
-               if (bufsize > 1000 * 1000) {
-                       loaded = 0;
+       if (st->loaded >= st->bufsize) {
+               if (st->bufsize > 1000 * 1000) {
+                       st->loaded = 0;
                        return 0;
                }
-               bufsize += bufsize + 1000;
-               buf = para_realloc(buf, bufsize);
+               st->bufsize += st->bufsize + 1000;
+               st->buf = para_realloc(st->buf, st->bufsize);
        }
-       assert(loaded < bufsize);
-       ret = read_nonblock(stat_pipe, buf + loaded, bufsize - loaded,
-               &s->rfds, &sz);
-       loaded += sz;
-       ret2 = for_each_stat_item(buf, loaded, update_item);
+       assert(st->loaded < st->bufsize);
+       ret = read_nonblock(st->fd, st->buf + st->loaded,
+               st->bufsize - st->loaded, &s->rfds, &sz);
+       st->loaded += sz;
+       ret2 = for_each_stat_item(st->buf, st->loaded, update_item);
        if (ret < 0 || ret2 < 0) {
-               loaded = 0;
+               st->loaded = 0;
                PARA_NOTICE_LOG("closing stat pipe: %s\n", para_strerror(-ret));
-               close(stat_pipe);
-               stat_pipe = -1;
+               close(st->fd);
+               st->fd = -1;
                clear_all_items();
                free(stat_content[SI_BASENAME]);
                stat_content[SI_BASENAME] =
@@ -633,9 +635,9 @@ static int status_post_select(struct sched *s, __a_unused struct task *t)
                return 0;
        }
        sz = ret2; /* what is left */
-       if (sz > 0 && sz < loaded)
-               memmove(buf, buf + loaded - sz, sz);
-       loaded = sz;
+       if (sz > 0 && sz < st->loaded)
+               memmove(st->buf, st->buf + st->loaded - sz, sz);
+       st->loaded = sz;
        return 0;
 }
 
@@ -1396,6 +1398,7 @@ static int setup_tasks_and_schedule(void)
                        .pre_select = status_pre_select,
                        .post_select = status_post_select,
                },
+               .fd = -1
        };
        struct input_task input_task = {
                .task = {