sched: Introduce alternative task API.
[paraslash.git] / stdout.c
1 /*
2 * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file stdout.c Functions that deal with writing to stdout. */
8
9 #include <assert.h>
10
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "fd.h"
15 #include "error.h"
16 #include "stdout.h"
17 #include "buffer_tree.h"
18
19 /**
20 * The pre_select function of the stdout task.
21 *
22 * \param s The scheduler this task was registered to.
23 * \param t The task structure of the stdout task.
24 *
25 * This function is always successful. If there is input data available, it
26 * adds \p STDOUT_FILENO to the write fd set of \a s.
27 */
28 static void stdout_pre_select(struct sched *s, struct task *t)
29 {
30 struct stdout_task *sot = task_context(t);
31 int ret;
32
33 ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
34 if (ret > 0)
35 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
36 else if (ret < 0)
37 sched_min_delay(s);
38 }
39
40 /**
41 * The post select function of the stdout task.
42 *
43 * \param s The scheduler this task was registered to.
44 * \param t The task structure of the stdout task.
45 *
46 * This function writes input data from the buffer tree to stdout if \p
47 * STDOUT_FILENO is writable.
48 */
49 static int stdout_post_select(struct sched *s, struct task *t)
50 {
51 struct stdout_task *sot = task_context(t);
52 struct btr_node *btrn = sot->btrn;
53 int ret;
54 char *buf;
55 size_t sz;
56
57 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
58 if (ret < 0)
59 goto out;
60 if (ret == 0)
61 return 0;
62 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
63 return 0;
64
65 if (sot->must_set_nonblock_flag) {
66 ret = mark_fd_nonblocking(STDOUT_FILENO);
67 if (ret < 0)
68 goto out;
69 sot->must_set_nonblock_flag = false;
70 }
71 for (;;) {
72 sz = btr_next_buffer(btrn, &buf);
73 if (sz == 0)
74 break;
75 ret = xwrite(STDOUT_FILENO, buf, sz);
76 if (ret <= 0)
77 break;
78 btr_consume(btrn, ret);
79 }
80 out:
81 if (ret < 0) {
82 btr_remove_node(&sot->btrn);
83 /* Revert to blocking mode if necessary. */
84 fcntl(STDOUT_FILENO, F_SETFL, sot->fd_flags);
85 }
86 return ret;
87 }
88
89 /**
90 * Register a stdout task structure.
91 *
92 * \param sot The stdout task structure to register.
93 * \param s The task will be added to this scheduler's task list.
94 *
95 * This sets up \a sot and registers a task with \a sot as context pointer.
96 */
97 void stdout_task_register(struct stdout_task *sot, struct sched *s)
98 {
99 int ret;
100 struct task_info ti = {
101 .pre_select = stdout_pre_select,
102 .post_select = stdout_post_select,
103 .context = sot,
104 .name = "stdout",
105 };
106
107 /* See stdin.c for details. */
108 ret = fcntl(STDOUT_FILENO, F_GETFL);
109 if (ret < 0) {
110 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
111 exit(EXIT_FAILURE);
112 }
113 sot->fd_flags = ret;
114 sot->must_set_nonblock_flag = (sot->fd_flags & O_NONBLOCK) == 0;
115 sot->task = task_register(&ti, s);
116 }