summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGeoff Levand <geoffrey.levand@am.sony.com>2009-01-21 16:26:54 -0800
committerJeremy Kerr <jk@ozlabs.org>2009-02-01 11:41:41 +1100
commit812761a1f8ff94e4913529840b905360ff843fc4 (patch)
tree5ce899b8ef5e177ce075711ce2a911bf2237a768 /lib
parent61679084243c471053b0b1b9865b40d721586958 (diff)
downloadtalos-petitboot-812761a1f8ff94e4913529840b905360ff843fc4.tar.gz
talos-petitboot-812761a1f8ff94e4913529840b905360ff843fc4.zip
Move waiter to library
Move the waiter routines into the petitboot library. The waiter routines are generic enough to be used for both server and client. Does not change the waiter source. jk: move to lib/waiter/ instead of lib/ Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com> Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/waiter/waiter.c83
-rw-r--r--lib/waiter/waiter.h23
2 files changed, 106 insertions, 0 deletions
diff --git a/lib/waiter/waiter.c b/lib/waiter/waiter.c
new file mode 100644
index 0000000..21dd4a5
--- /dev/null
+++ b/lib/waiter/waiter.c
@@ -0,0 +1,83 @@
+
+#include <poll.h>
+#include <string.h>
+#include <assert.h>
+
+#include <talloc/talloc.h>
+
+#include "waiter.h"
+
+struct waiter {
+ int fd;
+ int events;
+ waiter_cb callback;
+ void *arg;
+};
+
+static struct waiter *waiters;
+static int n_waiters;
+
+struct waiter *waiter_register(int fd, int events,
+ waiter_cb callback, void *arg)
+{
+ struct waiter *waiter;
+
+ n_waiters++;
+
+ waiters = talloc_realloc(NULL, waiters, struct waiter, n_waiters);
+ waiter = &waiters[n_waiters - 1];
+
+ waiter->fd = fd;
+ waiter->events = events;
+ waiter->callback = callback;
+ waiter->arg = arg;
+
+ return 0;
+}
+
+void waiter_remove(struct waiter *waiter)
+{
+ int i;
+
+ i = waiter - waiters;
+ assert(i >= 0 && i < n_waiters);
+
+ n_waiters--;
+ memmove(&waiters[i], &waiters[i+1], n_waiters - i);
+
+ waiters = talloc_realloc(NULL, waiters, struct waiter, n_waiters);
+}
+
+int waiter_poll(void)
+{
+ 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);
+ }
+
+ for (i = 0; i < n_waiters; i++) {
+ pollfds[i].fd = waiters[i].fd;
+ pollfds[i].events = waiters[i].events;
+ pollfds[i].revents = 0;
+ }
+
+ rc = poll(pollfds, 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);
+
+ if (rc)
+ waiter_remove(&waiters[i]);
+ }
+ }
+
+ return 0;
+}
diff --git a/lib/waiter/waiter.h b/lib/waiter/waiter.h
new file mode 100644
index 0000000..ff8a5ff
--- /dev/null
+++ b/lib/waiter/waiter.h
@@ -0,0 +1,23 @@
+#ifndef _WAITER_H
+#define _WAITER_H
+
+#include <poll.h>
+
+struct waiter;
+
+enum events {
+ WAIT_IN = POLLIN,
+ WAIT_OUT = POLLOUT,
+};
+
+typedef int (*waiter_cb)(void *);
+
+struct waiter *waiter_register(int fd, int events,
+ waiter_cb callback, void *arg);
+
+void waiter_remove(struct waiter *waiter);
+
+int waiter_poll(void);
+#endif /* _WAITER_H */
+
+
OpenPOWER on IntegriCloud