summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorTimo Teras <timo.teras@iki.fi>2009-11-25 10:52:15 +0200
committerTimo Teras <timo.teras@iki.fi>2009-11-25 10:52:15 +0200
commitfc1044daf51f32b9d85f8497e4e0bd5a3c1e7fe9 (patch)
tree52e11c88f17c47c0d086761e50b266f5c5ccd061 /test
parentcec85dedb7fd66cf2c23cafadd7c53eb7afed78f (diff)
downloadlibtf-fc1044daf51f32b9d85f8497e4e0bd5a3c1e7fe9.tar.bz2
libtf-fc1044daf51f32b9d85f8497e4e0bd5a3c1e7fe9.tar.xz
libtf: implement basic file i/o with epoll
some scetching of i/o api, and implement basic read and write functionality. integrate polling to scheduler and an epoll based polling mechanism.
Diffstat (limited to 'test')
-rw-r--r--test/TFbuild2
-rw-r--r--test/read.c44
2 files changed, 45 insertions, 1 deletions
diff --git a/test/TFbuild b/test/TFbuild
index d2648ed..430e132 100644
--- a/test/TFbuild
+++ b/test/TFbuild
@@ -1,3 +1,3 @@
-progs-$(TEST) += simple1 sleep
+progs-$(TEST) += simple1 sleep read
LIBS += $(objtree)src/libtf.a
diff --git a/test/read.c b/test/read.c
new file mode 100644
index 0000000..1921609
--- /dev/null
+++ b/test/read.c
@@ -0,0 +1,44 @@
+/* Read from stdin and have an active fiber in the background.
+ * Stdin needs to be redirected to FIFO or similar; mixing
+ * console and non-blocking I/O is not a good idea.
+ */
+
+#include <libtf/tf.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static void time_fiber(void *ptr)
+{
+ while (1) {
+ printf("Tick\n");
+ tf_msleep(1000);
+ printf("Tack\n");
+ tf_msleep(1000);
+ }
+}
+
+static void io_fiber(void *ptr)
+{
+ char data[8];
+ struct tf_fd fin;
+
+ tf_open_fd(&fin, STDIN_FILENO);
+ while (1) {
+ if (tf_read(&fin, data, sizeof(data), TF_INFINITE) < 0)
+ break;
+ printf("Read: %8.8s\n", data);
+ }
+ printf("Exiting io fiber\n");
+ tf_close(&fin);
+}
+
+static void init_fiber(void *ptr)
+{
+ tf_fiber_put(tf_fiber_create(time_fiber, 0));
+ tf_fiber_put(tf_fiber_create(io_fiber, 0));
+}
+
+int main(int argc, char **argv)
+{
+ return tf_main(init_fiber);
+}