* of \a s.
*/
static void stdin_pre_select(struct sched *s, struct task *t)
-{
- struct stdin_task *sit = container_of(t, struct stdin_task, task);
-
- if (sit->output_error && *sit->output_error < 0) {
- t->error = *sit->output_error;
- return;
- }
- t->error = 0;
- sit->check_fd = 0;
- if (sit->loaded >= sit->bufsize)
- return;
- sit->check_fd = 1;
- para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
-}
-
-static void stdin_pre_select_btr(struct sched *s, struct task *t)
{
struct stdin_task *sit = container_of(t, struct stdin_task, task);
int ret;
*
* This function checks if \p STDIN_FILENO was included by in the read fd set
* of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
- * appears to be readable, data is read from stdin into the buffer of the
- * stdin task.
+ * appears to be readable, data is read from stdin and fed into the buffer
+ * tree.
*/
static void stdin_post_select(struct sched *s, struct task *t)
-{
- struct stdin_task *sit = container_of(t, struct stdin_task, task);
- ssize_t ret;
-
- if (sit->output_error && *sit->output_error < 0) {
- t->error = *sit->output_error;
- return;
- }
- t->error = 0;
- if (!sit->check_fd)
- return;
- if (!FD_ISSET(STDIN_FILENO, &s->rfds))
- return;
- ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
- if (ret < 0)
- t->error = ERRNO_TO_PARA_ERROR(errno);
- else if (ret > 0)
- sit->loaded += ret;
- else
- t->error = -E_STDIN_EOF;
-}
-
-static void stdin_post_select_btr(struct sched *s, struct task *t)
{
struct stdin_task *sit = container_of(t, struct stdin_task, task);
ssize_t ret;
*
* This fills in the pre/post select function pointers of the task structure
* given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
- * mode and \a bufsize is initialized (but no buffer is allocated).
+ * mode, and a buffer tree is created.
*/
void stdin_set_defaults(struct stdin_task *sit)
{
int ret;
- sit->bufsize = 32 * 1024;
- if (sit->btrn) {
- sit->task.pre_select = stdin_pre_select_btr;
- sit->task.post_select = stdin_post_select_btr;
- sit->btrp = btr_pool_new("stdin", 64 * 1024);
- } else {
- sit->task.pre_select = stdin_pre_select;
- sit->task.post_select = stdin_post_select;
- }
+ sit->task.pre_select = stdin_pre_select;
+ sit->task.post_select = stdin_post_select;
+ sit->btrp = btr_pool_new("stdin", 64 * 1024);
sprintf(sit->task.status, "stdin reader");
ret = mark_fd_nonblocking(STDIN_FILENO);
if (ret >= 0)
return;
- sit->output_error = NULL;
PARA_EMERG_LOG("%s\n", para_strerror(-ret));
exit(EXIT_FAILURE);
}
/** The task structure used for reading from stdin. */
struct stdin_task {
- /** Input buffer. */
- char *buf;
- /** The size of \a buf. */
- size_t bufsize;
- /** Number of bytes currently loaded in \a buf. */
- size_t loaded;
- /** Pointer to the error member of the consumer. */
- int *output_error;
- /** Whether \p STDIN_FILENO was included in the read fd set. */
- int check_fd;
/** The task structure. */
struct task task;
- /** Non-null if buffer tree API should be used. */
+ /** Stdin is always the root of a buffer tree. */
struct btr_node *btrn;
+ /* Use a buffer pool to minimize memcpy due to alignment problems. */
struct btr_pool *btrp;
};
* \param s The scheduler this task was registered to.
* \param t The task structure of the stdout task.
*
- * This function is always successful. If there is data available in the input
- * buffer, it adds \p STDOUT_FILENO to the write fd set of \a s.
+ * This function is always successful. If there is input data available, it
+ * adds \p STDOUT_FILENO to the write fd set of \a s.
*/
static void stdout_pre_select(struct sched *s, struct task *t)
-{
- struct stdout_task *sot = container_of(t, struct stdout_task, task);
-
- t->error = 0;
- sot->check_fd = 0;
- if (!*sot->loaded) {
- if (*sot->input_error < 0) {
- t->error = *sot->input_error;
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
- }
- return;
- }
- sot->check_fd = 1;
- para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
-}
-
-static void stdout_pre_select_btr(struct sched *s, struct task *t)
{
struct stdout_task *sot = container_of(t, struct stdout_task, task);
int ret;
t->error = 0;
- sot->check_fd = 0;
ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
if (ret > 0)
para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
- else if (ret < 0) {
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
- }
+ else if (ret < 0)
+ sched_min_delay(s);
}
/**
* \param s The scheduler this task was registered to.
* \param t The task structure of the stdout task.
*
- * This function checks if \p STDOUT_FILENO was included by in the write fd set
- * of \a s during the previous pre_select call. If yes, and \p STDOUT_FILENO
- * appeears to be writable, the data loaded in the input buffer is written to
- * stdout.
+ * This function writes input data from the buffer tree to stdout if \p
+ * STDOUT_FILENO is writable.
*/
static void stdout_post_select(struct sched *s, struct task *t)
-{
- struct stdout_task *sot = container_of(t, struct stdout_task, task);
- ssize_t ret;
-
- t->error = 0;
- if (!sot->check_fd) {
- if (!*sot->loaded && *sot->input_error < 0)
- t->error = *sot->input_error;
- return;
- }
- if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
- return;
- ret = write(STDOUT_FILENO, *sot->bufp, *sot->loaded);
- if (ret < 0) {
- t->error = -ERRNO_TO_PARA_ERROR(errno);
- return;
- }
- *sot->loaded -= ret;
- if (*sot->loaded)
- memmove(*sot->bufp, *sot->bufp + ret, *sot->loaded);
-}
-
-static void stdout_post_select_btr(struct sched *s, struct task *t)
{
struct stdout_task *sot = container_of(t, struct stdout_task, task);
struct btr_node *btrn = sot->btrn;
* \param sot The stdout task structure.
*
* This fills in the pre/post select function pointers of the task structure
- * given by \a sot.
+ * given by \a sot and sets the stdout file descriptor to nonblocking mode.
*/
void stdout_set_defaults(struct stdout_task *sot)
{
int ret;
- if (sot->btrn) {
- sot->task.pre_select = stdout_pre_select_btr;
- sot->task.post_select = stdout_post_select_btr;
- } else {
- sot->task.pre_select = stdout_pre_select;
- sot->task.post_select = stdout_post_select;
- }
+ sot->task.pre_select = stdout_pre_select;
+ sot->task.post_select = stdout_post_select;
sprintf(sot->task.status, "stdout");
ret = mark_fd_nonblocking(STDOUT_FILENO);
if (ret >= 0)
* Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file stdout.h The standard out task structure. */
+/** \file stdout.h Writing to stdout via buffer trees. */
/**
* The task structure used for writing to stdout.
+ *
+ * This is used by para_recv, para_filter and para_client.
*/
struct stdout_task {
- /** Pointer to the data buffer pointer. */
- char **bufp;
- /** Number of bytes loaded in \a buf. */
- size_t *loaded;
- /** Pointer to the error variable of the feeding task. */
- int *input_error;
- /** The task structure. */
+ /** The task structure used by the scheduler. */
struct task task;
- /** Whether \p STDOUT_FILENO was included in the write fd set. */
- int check_fd;
- /** Non-null if buffer tree API should be used. */
+ /** Stdout is always a leaf node in the buffer tree. */
struct btr_node *btrn;
};