Simplify the scheduling code.
[paraslash.git] / write_common.c
1 /*
2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write_common.c common functions of para_audiod and para_write */
8
9 #include "para.h"
10 #include "string.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "write.h"
14 #include "error.h"
15
16 /** the array containing the names of all supported writers */
17 const char *writer_names[] ={WRITER_NAMES};
18
19 /** the array of supported writers */
20 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
21
22 static void wng_pre_select(__a_unused struct sched *s, struct task *t)
23 {
24 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
25 int i;
26
27 FOR_EACH_WRITER_NODE(i, g) {
28 struct writer_node *wn = &g->writer_nodes[i];
29 t->error = wn->writer->pre_select(s, wn);
30 if (t->error < 0)
31 return;
32 }
33 }
34
35 static void wng_post_select(struct sched *s, struct task *t)
36 {
37 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
38 int i;
39 size_t min_written = 0;
40
41 FOR_EACH_WRITER_NODE(i, g) {
42 struct writer_node *wn = &g->writer_nodes[i];
43 t->error = wn->writer->post_select(s, wn);
44 if (t->error < 0)
45 return;
46 if (!i)
47 min_written = wn->written;
48 else
49 min_written = PARA_MIN(min_written, wn->written);
50 }
51 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
52 if (min_written) {
53 *g->loaded -= min_written;
54 FOR_EACH_WRITER_NODE(i, g)
55 g->writer_nodes[i].written -= min_written;
56 }
57 if (!*g->loaded && *g->input_error) {
58 t->error = *g->input_error;
59 return;
60 }
61 if (*g->loaded && min_written) {
62 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
63 memmove(g->buf, g->buf + min_written, *g->loaded);
64 }
65 }
66
67 /**
68 * call the open function of each writer in the group
69 *
70 * \param g the writer node group
71 *
72 * \return If at least one open function returned an error, all successful
73 * writer notes get closed and this error value is returned. Upon success, a
74 * task associated with \a g is registered to the scheduler and the function
75 * returns a positive value.
76 * */
77 int wng_open(struct writer_node_group *g)
78 {
79 int i, ret = 1;
80
81 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
82 register_task(&g->task);
83 FOR_EACH_WRITER_NODE(i, g) {
84 struct writer_node *wn = &g->writer_nodes[i];
85 wn->wng = g;
86 ret = wn->writer->open(wn);
87 if (ret < 0)
88 goto err_out;
89 wn->chunk_bytes = ret;
90 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
91 }
92 sprintf(g->task.status, "%s", "writer node group");
93 return 1;
94 err_out:
95 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
96 unregister_task(&g->task);
97 while (i > 0) {
98 struct writer_node *wn = &g->writer_nodes[--i];
99 wn->writer->close(wn);
100 }
101 g->num_writers = 0;
102 return ret;
103 }
104
105
106 /**
107 * unregister a writer node group task
108 *
109 * \param g the group whose task is to be closed
110 */
111 void wng_unregister(struct writer_node_group *g)
112 {
113 unregister_task(&g->task);
114 }
115
116 /**
117 * call the close function of each writer in the given group
118 *
119 * \param g the writer node group to close
120 *
121 * This function also frees all resources of the given group.
122 */
123 void wng_close(struct writer_node_group *g)
124 {
125 int i;
126
127 if (!g)
128 return;
129 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
130 FOR_EACH_WRITER_NODE(i, g) {
131 struct writer_node *wn = &g->writer_nodes[i];
132 wn->writer->close(wn);
133 }
134 free(g->writer_nodes);
135 free(g);
136 }
137
138 /**
139 * allocate and initialize a new writer_node_group struct
140 *
141 * \param num_writers the number of writer nodes for the new group
142 *
143 * \return Pointer to the new writer node group
144 */
145 struct writer_node_group *wng_new(unsigned num_writers)
146 {
147 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
148 g->num_writers = num_writers;
149 g->writer_nodes = para_calloc(num_writers
150 * sizeof(struct writer_node));
151 g->task.post_select = wng_post_select;
152 g->task.pre_select = wng_pre_select;
153 return g;
154 }
155
156 /**
157 * call the init function of each supported paraslash writer
158 */
159 void init_supported_writers(void)
160 {
161 int i;
162
163 FOR_EACH_WRITER(i)
164 writers[i].init(&writers[i]);
165 }
166 /**
167 * check if given string is a valid command line for any writer
168 *
169 * \param \wa string of the form writer_name:options
170 * \param writer_num contains the number of the writer upon success
171 *
172 * This function checks whether \a wa starts with the name of a supported
173 * paraslash writer, optionally followed by a colon and any options for that
174 * writer. If a valid writer name was found and further are present, the
175 * remaining part of \a wa is passed to that writer's config parser.
176 *
177 * \return On success, a pointer to the gengetopt args info struct is returned
178 * and \a writer_num contains the number of the writer. Otherwise this function
179 * returns \p NULL.
180 */
181 void *check_writer_arg(const char *wa, int *writer_num)
182 {
183 int i;
184
185 *writer_num = -E_WRITE_COMMON_SYNTAX;
186 PARA_INFO_LOG("checking %s\n", wa);
187 FOR_EACH_WRITER(i) {
188 const char *name = writer_names[i];
189 size_t len = strlen(name);
190 char c;
191 if (strlen(wa) < len)
192 continue;
193 if (strncmp(name, wa, len))
194 continue;
195 c = wa[len];
196 if (c && c != ' ')
197 continue;
198 if (c && !writers[i].parse_config)
199 return NULL;
200 *writer_num = i;
201 return writers[i].parse_config(c? wa + len + 1 : "");
202 }
203 PARA_ERROR_LOG("writer not found\n");
204 return NULL;
205 }
206
207 /**
208 * setup a writer node group with only one writer, the default writer
209 *
210 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
211 * osx_write for OS X, file writer if neither of these is supported.
212 *
213 * \return pointer to the allocated writer node group
214 */
215 struct writer_node_group *setup_default_wng(void)
216 {
217 struct writer_node_group *wng = wng_new(1);
218 wng->writer_nodes[0].writer = &writers[DEFAULT_WRITER];
219 PARA_INFO_LOG("using default writer: %s %p\n",
220 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
221 wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
222 return wng;
223 }