summaryrefslogtreecommitdiffstats
path: root/test/read.c
blob: 3d318a36fb0197998e3b2e61da9d8e1c6824269e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 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, TF_FD_STREAM_ORIENTED);
	while (1) {
		if (tf_read_fully(&fin, data, sizeof(data)) < 0)
			break;
		printf("Read: %8.8s\n", data);
	}
	printf("Exiting io fiber\n");
	tf_close(&fin);
}

int main(int argc, char **argv)
{
	tf_scheduler_enable(NULL);
	tf_fiber_put(tf_fiber_create(NULL, time_fiber, 0));
	tf_fiber_put(tf_fiber_create(NULL, io_fiber, 0));
	tf_scheduler_disable();
}