waiter: Don't rely on global variables to keep waiter state
authorJeremy Kerr <jk@ozlabs.org>
Wed, 10 Apr 2013 02:19:13 +0000 (12:19 +1000)
committerGeoff Levand <geoff@infradead.org>
Wed, 10 Apr 2013 17:49:20 +0000 (10:49 -0700)
Rather than defining the set of waiters (and pollfds) in waiter.c, add a
struct waitset to contain these.  A waitset is created with
waitset_create, which is passed to the waiter_* functions.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Geoff Levand <geoff@infradead.org>
16 files changed:
discover/discover-server.c
discover/discover-server.h
discover/pb-discover.c
discover/udev.c
discover/udev.h
discover/user-event.c
discover/user-event.h
lib/waiter/waiter.c
lib/waiter/waiter.h
ui/ncurses/nc-cui.c
ui/ncurses/nc-cui.h
ui/twin/main-generic.c
ui/twin/pbt-client.c
ui/twin/pbt-client.h
ui/twin/pbt-scr.c
ui/twin/pbt-scr.h

index f4e96c046927bd1e0709d7ea78a621fd3da4f26c..21e15da4c84e64219134274996ec9d09106cf2ba 100644 (file)
@@ -22,6 +22,7 @@
 
 struct discover_server {
        int socket;
+       struct waitset *waitset;
        struct waiter *waiter;
        struct list clients;
        struct device_handler *device_handler;
@@ -184,7 +185,7 @@ void discover_server_set_device_source(struct discover_server *server,
        server->device_handler = handler;
 }
 
-struct discover_server *discover_server_init(void)
+struct discover_server *discover_server_init(struct waitset *waitset)
 {
        struct discover_server *server;
        struct sockaddr_un addr;
@@ -194,6 +195,7 @@ struct discover_server *discover_server_init(void)
                return NULL;
 
        server->waiter = NULL;
+       server->waitset = waitset;
        list_init(&server->clients);
 
        unlink(PB_SOCKET_PATH);
@@ -219,8 +221,8 @@ struct discover_server *discover_server_init(void)
                goto out_err;
        }
 
-       server->waiter = waiter_register(server->socket, WAIT_IN,
-                       discover_server_process, server);
+       server->waiter = waiter_register(server->waitset, server->socket,
+                       WAIT_IN, discover_server_process, server);
 
        return server;
 
index 7c8993815ffdb11ca1b1e35d7573f4c251905413..6650bba638095c9d91306eb3ef9d89520b03d1fe 100644 (file)
@@ -1,11 +1,13 @@
 #ifndef _DISCOVER_SERVER_H
 #define _DISCOVER_SERVER_H
 
+#include <waiter/waiter.h>
+
 struct discover_server;
 struct device_handler;
 struct device;
 
-struct discover_server *discover_server_init(void);
+struct discover_server *discover_server_init(struct waitset *waitset);
 
 void discover_server_destroy(struct discover_server *server);
 
index aac50df332d4c47b636909ae7b7c0afbf3310c4a..536f6e65a7b34a88a916417f553eb7414a5773f3 100644 (file)
@@ -101,6 +101,7 @@ int main(int argc, char *argv[])
 {
        struct device_handler *handler;
        struct discover_server *server;
+       struct waitset *waitset;
        struct opts opts;
        struct udev *udev;
        struct user_event *uev;
@@ -138,7 +139,9 @@ int main(int argc, char *argv[])
 
        signal(SIGINT, sigint_handler);
 
-       server = discover_server_init();
+       waitset = waitset_create(NULL);
+
+       server = discover_server_init(waitset);
        if (!server)
                return EXIT_FAILURE;
 
@@ -148,11 +151,11 @@ int main(int argc, char *argv[])
 
        discover_server_set_device_source(server, handler);
 
-       udev = udev_init(handler);
+       udev = udev_init(waitset, handler);
        if (!udev)
                return EXIT_FAILURE;
 
-       uev = user_event_init(handler);
+       uev = user_event_init(waitset, handler);
        if (!uev)
                return EXIT_FAILURE;
 
@@ -160,11 +163,12 @@ int main(int argc, char *argv[])
        user_event_trigger(uev);
 
        for (running = 1; running;) {
-               if (waiter_poll())
+               if (waiter_poll(waitset))
                        break;
        }
 
        device_handler_destroy(handler);
+       waitset_destroy(waitset);
 
        pb_log("--- end ---\n");
 
index 28357c7e1b0b0441952aab16646ee1d1d67395c9..40b9f638c5b9d51b0c2c432cbe4a223e1873dd96 100644 (file)
@@ -116,7 +116,7 @@ static int udev_destructor(void *p)
        return 0;
 }
 
-struct udev *udev_init(struct device_handler *handler)
+struct udev *udev_init(struct waitset *waitset, struct device_handler *handler)
 {
        struct sockaddr_un addr;
        struct udev *udev;
@@ -144,7 +144,7 @@ struct udev *udev_init(struct device_handler *handler)
                goto out_err;
        }
 
-       waiter_register(udev->socket, WAIT_IN, udev_process, udev);
+       waiter_register(waitset, udev->socket, WAIT_IN, udev_process, udev);
 
        pb_log("%s: waiting on %s\n", __func__, PBOOT_DEVICE_SOCKET);
 
index db17c45e45f1cdc3f1b91bea01f4ef4d540297bb..590eed1a41617dcfa72c6c02c1ddd1973e533b24 100644 (file)
@@ -3,8 +3,9 @@
 
 struct udev;
 struct device_handler;
+struct waitset;
 
-struct udev *udev_init(struct device_handler *handler);
+struct udev *udev_init(struct waitset *waitset, struct device_handler *handler);
 int udev_trigger(struct udev *udev);
 void udev_destroy(struct udev *udev);
 
index afc4c437e8a7809f685a1a03a3e2bb5af31d6819..db12bd14135b3d84d4ea1d79bd38f5f36abb43fe 100644 (file)
@@ -118,7 +118,8 @@ static int user_event_destructor(void *arg)
        return 0;
 }
 
-struct user_event *user_event_init(struct device_handler *handler)
+struct user_event *user_event_init(struct waitset *waitset,
+               struct device_handler *handler)
 {
        struct sockaddr_un addr;
        struct user_event *uev;
@@ -147,7 +148,7 @@ struct user_event *user_event_init(struct device_handler *handler)
                        strerror(errno));
        }
 
-       waiter_register(uev->socket, WAIT_IN, user_event_process, uev);
+       waiter_register(waitset, uev->socket, WAIT_IN, user_event_process, uev);
 
        pb_log("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
 
index e134d0c4801945223c0b3f9a67de12f48a75b8c2..383bdd0732239465f3696fb15d50c9ee57522917 100644 (file)
 #define PBOOT_USER_EVENT_SIZE (1 * 1024)
 
 struct user_event;
+struct waitset;
 
-struct user_event *user_event_init(struct device_handler *handler);
+struct user_event *user_event_init(struct waitset *waitset,
+               struct device_handler *handler);
 void user_event_trigger(struct user_event *uev);
 void user_event_destroy(struct user_event *uev);
 
index 0bb213cce2840b5ce82c2bab19db9cb8105e8a8e..ac284366745121436e748cdd58af75fa48d58732 100644 (file)
@@ -8,29 +8,48 @@
 #include "waiter.h"
 
 struct waiter {
+       struct waitset  *set;
        int             fd;
        int             events;
        waiter_cb       callback;
        void            *arg;
 };
 
-static struct waiter *waiters;
-static int n_waiters;
+struct waitset {
+       struct waiter   *waiters;
+       int             n_waiters;
+       struct pollfd   *pollfds;
+       int             n_pollfds;
+};
 
-struct waiter *waiter_register(int fd, int events,
+struct waitset *waitset_create(void *ctx)
+{
+       struct waitset *set = talloc_zero(ctx, struct waitset);
+       return set;
+}
+
+void waitset_destroy(struct waitset *set)
+{
+       talloc_free(set);
+}
+
+struct waiter *waiter_register(struct waitset *set, int fd, int events,
                waiter_cb callback, void *arg)
 {
-       struct waiter *waiter;
+       struct waiter *waiters, *waiter;
 
-       n_waiters++;
+       waiters = talloc_realloc(set, set->waiters,
+                       struct waiter, set->n_waiters + 1);
 
-       waiters = talloc_realloc(NULL, waiters, struct waiter, n_waiters);
-       
-       if(!waiters)
+       if (!waiters)
                return NULL;
-       
-       waiter = &waiters[n_waiters - 1];
 
+       set->n_waiters++;
+       set->waiters = waiters;
+
+       waiter = &set->waiters[set->n_waiters - 1];
+
+       waiter->set = set;
        waiter->fd = fd;
        waiter->events = events;
        waiter->callback = callback;
@@ -41,47 +60,47 @@ struct waiter *waiter_register(int fd, int events,
 
 void waiter_remove(struct waiter *waiter)
 {
+       struct waitset *set = waiter->set;
        int i;
 
-       i = waiter - waiters;
-       assert(i >= 0 && i < n_waiters);
+       i = waiter - set->waiters;
+       assert(i >= 0 && i < set->n_waiters);
 
-       n_waiters--;
-       memmove(&waiters[i], &waiters[i+1],
-               (n_waiters - i) * sizeof(waiters[0]));
+       set->n_waiters--;
+       memmove(&set->waiters[i], &set->waiters[i+1],
+               (set->n_waiters - i) * sizeof(set->waiters[0]));
 
-       waiters = talloc_realloc(NULL, waiters, struct waiter, n_waiters);
+       set->waiters = talloc_realloc(set->waiters, set->waiters, struct waiter,
+                       set->n_waiters);
 }
 
-int waiter_poll(void)
+int waiter_poll(struct waitset *set)
 {
-       static struct pollfd *pollfds;
-       static int n_pollfds;
        int i, rc;
 
-       if (n_waiters != n_pollfds) {
-               pollfds = talloc_realloc(NULL, pollfds,
-                               struct pollfd, n_waiters);
-               n_pollfds = n_waiters;
+       if (set->n_waiters != set->n_pollfds) {
+               set->pollfds = talloc_realloc(set, set->pollfds,
+                               struct pollfd, set->n_waiters);
+               set->n_pollfds = set->n_waiters;
        }
 
-       for (i = 0; i < n_waiters; i++) {
-               pollfds[i].fd = waiters[i].fd;
-               pollfds[i].events = waiters[i].events;
-               pollfds[i].revents = 0;
+       for (i = 0; i < set->n_waiters; i++) {
+               set->pollfds[i].fd = set->waiters[i].fd;
+               set->pollfds[i].events = set->waiters[i].events;
+               set->pollfds[i].revents = 0;
        }
 
-       rc = poll(pollfds, n_waiters, -1);
+       rc = poll(set->pollfds, set->n_waiters, -1);
 
        if (rc <= 0)
                return rc;
 
-       for (i = 0; i < n_waiters; i++) {
-               if (pollfds[i].revents) {
-                       rc = waiters[i].callback(waiters[i].arg);
+       for (i = 0; i < set->n_waiters; i++) {
+               if (set->pollfds[i].revents) {
+                       rc = set->waiters[i].callback(set->waiters[i].arg);
 
                        if (rc)
-                               waiter_remove(&waiters[i]);
+                               waiter_remove(&set->waiters[i]);
                }
        }
 
index ff8a5ff22e9193175a71ea70c5070b04c00b888d..ed7f6bbdb9994a4c673a4e3bf71bb552a9f45d69 100644 (file)
@@ -4,6 +4,7 @@
 #include <poll.h>
 
 struct waiter;
+struct waitset;
 
 enum events {
        WAIT_IN  = POLLIN,
@@ -12,12 +13,15 @@ enum events {
 
 typedef int (*waiter_cb)(void *);
 
-struct waiter *waiter_register(int fd, int events,
+struct waitset *waitset_create(void *ctx);
+void waitset_destroy(struct waitset *waitset);
+
+struct waiter *waiter_register(struct waitset *waitset, int fd, int events,
                waiter_cb callback, void *arg);
 
 void waiter_remove(struct waiter *waiter);
 
-int waiter_poll(void);
+int waiter_poll(struct waitset *waitset);
 #endif /* _WAITER_H */
 
 
index b9d8d79048639300b6d4d7667983be7b463627ed..a909b828d065f94c5a9129bd054f0c19bab6024e 100644 (file)
@@ -553,6 +553,7 @@ struct cui *cui_init(void* platform_info,
        cui->on_kexec = on_kexec;
        cui->timer.handle_timeout = cui_handle_timeout;
        cui->dry_run = dry_run;
+       cui->waitset = waitset_create(cui);
 
        /* Loop here for scripts that just started the server. */
 
@@ -595,18 +596,19 @@ retry_start:
        atexit(nc_atexit);
        nc_start();
 
-       waiter_register(discover_client_get_fd(client), WAIT_IN,
-               cui_client_process_socket, client);
+       waiter_register(cui->waitset, discover_client_get_fd(client), WAIT_IN,
+                       cui_client_process_socket, client);
 
-       waiter_register(STDIN_FILENO, WAIT_IN, cui_process_key, cui);
+       waiter_register(cui->waitset, STDIN_FILENO, WAIT_IN,
+                       cui_process_key, cui);
 
        if (js_map) {
 
                cui->pjs = pjs_init(cui, js_map);
 
                if (cui->pjs)
-                       waiter_register(pjs_get_fd(cui->pjs), WAIT_IN,
-                               cui_process_js, cui);
+                       waiter_register(cui->waitset, pjs_get_fd(cui->pjs),
+                                       WAIT_IN, cui_process_js, cui);
        }
 
        return cui;
@@ -638,7 +640,7 @@ int cui_run(struct cui *cui, struct pmenu *main, unsigned int default_item)
        cui->current->post(cui->current);
 
        while (1) {
-               int result = waiter_poll();
+               int result = waiter_poll(cui->waitset);
 
                if (result < 0 && errno != EINTR) {
                        pb_log("%s: poll: %s\n", __func__, strerror(errno));
index 90eef1cdfd7cd264cef80eebde5110fb202b9eb4..ddbf27f074ec53d3bc45c01559fb0b2ab3ecbe2e 100644 (file)
@@ -55,6 +55,7 @@ struct cui {
        struct nc_scr *current;
        struct pmenu *main;
        struct ui_timer timer;
+       struct waitset *waitset;
        struct pjs *pjs;
        void *platform_info;
        unsigned int default_item;
index 91a525b9f82cf9106b5f04b3b9f315d5f4e63ccd..8ed46b72c41d113a120de88847e770321606cc95 100644 (file)
@@ -215,7 +215,7 @@ static int kexec_cb(struct pbt_client *client, struct pb_opt_data *opt_data)
 static int run(struct pbt_client *client)
 {
        while (1) {
-               int result = waiter_poll();
+               int result = waiter_poll(client->waitset);
 
                if (result < 0 && errno != EINTR) {
                        pb_log("%s: poll: %s\n", __func__, strerror(errno));
index 6a4d86308e9fc6910d06b2868c1c8ac522da683c..8eba0a23209246d8edb7b505c223d23ff6814d69 100644 (file)
@@ -281,11 +281,14 @@ struct pbt_client *pbt_client_init(enum pbt_twin_backend backend,
 
        talloc_set_destructor(pbt_client, (void *)pbt_client_destructor);
 
+       pbt_client->waitset = waitset_create(pbt_client);
+
        pbt_client->sig = "pbt_client";
        pbt_client->kexec_cb = kexec_cb;
        pbt_client->dry_run = dry_run;
-       pbt_client->frame.scr = pbt_scr_init(pbt_client, backend, width, height,
-               NULL, NULL);
+       pbt_client->frame.scr = pbt_scr_init(pbt_client, pbt_client->waitset,
+                       backend, width, height, NULL, NULL);
+
 
        if (!pbt_client->frame.scr)
                goto fail_scr_init;
@@ -329,9 +332,10 @@ retry_start:
                goto fail_client_init;
        }
 
-       waiter_register(discover_client_get_fd(pbt_client->discover_client),
-               WAIT_IN, (waiter_cb)discover_client_process,
-               pbt_client->discover_client);
+       waiter_register(pbt_client->waitset,
+                       discover_client_get_fd(pbt_client->discover_client),
+                       WAIT_IN, (waiter_cb)discover_client_process,
+                       pbt_client->discover_client);
 
        return pbt_client;
 
index 014d5b2dc5f94f8b103da90d21eb65515f33a83c..6c359208a312fc146b67387c8cae39bef5f2eab4 100644 (file)
@@ -49,6 +49,7 @@ struct pbt_client {
 
        struct pbt_frame frame;
        struct discover_client *discover_client;
+       struct waitset *waitset;
 };
 
 struct pbt_client *pbt_client_init(enum pbt_twin_backend backend,
index 3a5a70bc48128662170cc0e393d7a37b952b8b70..8532bc10bfb79cfb79338fae96dea0bbcc035670 100644 (file)
@@ -368,7 +368,9 @@ static void pbt_scr_destructor(struct pbt_scr *scr)
        memset(scr, 0, sizeof(*scr));
 }
 
-struct pbt_scr *pbt_scr_init(void *talloc_ctx, enum pbt_twin_backend backend,
+struct pbt_scr *pbt_scr_init(void *talloc_ctx,
+       struct waitset *waitset,
+       enum pbt_twin_backend backend,
        unsigned int width, unsigned int height,
        const char *filename_background,
        twin_bool_t (*scr_event_cb)(twin_screen_t *tscreen,
@@ -441,7 +443,7 @@ struct pbt_scr *pbt_scr_init(void *talloc_ctx, enum pbt_twin_backend backend,
 
        assert(waiter_fd != -1);
 
-       waiter_register(waiter_fd, WAIT_IN, (void *)pbt_twin_waiter_cb,
+       waiter_register(waitset, waiter_fd, WAIT_IN, (void *)pbt_twin_waiter_cb,
                &scr->twin_ctx);
 
        return scr;
index c075aadfe7092d4c0bebab5d90ff8da6b62286b1..1198a5924bf59722d0791412a186b7f08b863a97 100644 (file)
@@ -18,6 +18,8 @@
 #if !defined(_PBT_SCR_H)
 #define _PBT_SCR_H
 
+#include <waiter/waiter.h>
+
 #include <libtwin/twin.h>
 #include <libtwin/twin_jpeg.h>
 #include <libtwin/twin_linux_mouse.h>
@@ -137,7 +139,8 @@ struct pbt_scr {
        twin_pixmap_t *cursor;
 };
 
-struct pbt_scr *pbt_scr_init(void *talloc_ctx, enum pbt_twin_backend backend,
+struct pbt_scr *pbt_scr_init(void *talloc_ctx, struct waitset *waitset,
+       enum pbt_twin_backend backend,
        unsigned int width, unsigned int height,
        const char *filename_background,
        twin_bool_t (*scr_event_cb)(twin_screen_t *tscreen,
@@ -171,4 +174,4 @@ void pbt_window_redraw(twin_window_t *twindow);
                _p->height)
 
 
-#endif
\ No newline at end of file
+#endif