stdout: Try to write as much as possible in one go.
[paraslash.git] / stdout.c
index 6539b68f3ff8f91befc8d99cf6b5c3002b2b9e98..34fe08cd6a46e8305fa990988b1408f056fad191 100644 (file)
--- a/stdout.c
+++ b/stdout.c
@@ -48,21 +48,17 @@ static void stdout_pre_select(struct sched *s, struct task *t)
 static void stdout_pre_select_btr(struct sched *s, struct task *t)
 {
        struct stdout_task *sot = container_of(t, struct stdout_task, task);
-       size_t sz = btr_get_input_queue_size(sot->btrn);
+       int ret;
 
        t->error = 0;
        sot->check_fd = 0;
-       if (sz == 0) {
-               if (btr_no_parent(sot->btrn)) {
-                       t->error = -E_ORPHAN;
-                       btr_del_node(sot->btrn);
-                       s->timeout.tv_sec = 0;
-                       s->timeout.tv_usec = 1;
-               }
-               return;
+       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;
        }
-       sot->check_fd = 1;
-       para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
 }
 
 /**
@@ -102,40 +98,40 @@ static void stdout_post_select(struct sched *s, struct task *t)
 static void stdout_post_select_btr(struct sched *s, struct task *t)
 {
        struct stdout_task *sot = container_of(t, struct stdout_task, task);
-       ssize_t ret;
-       size_t sz = btr_get_input_queue_size(sot->btrn);
-       bool orphan = btr_no_parent(sot->btrn);
+       struct btr_node *btrn = sot->btrn;
+       int ret;
        char *buf;
+       size_t sz;
 
        t->error = 0;
-       if (!sot->check_fd) {
-               if (sz == 0 && orphan) {
-                       t->error = -E_ORPHAN;
-                       goto err;
-               }
+       ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+       if (ret < 0)
+               goto out;
+       if (ret == 0)
                return;
-       }
        if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
                return;
-       sz = btr_next_buffer(sot->btrn, &buf);
-       if (sz == 0)
-               return;
-       ret = write(STDOUT_FILENO, buf, sz);
-       if (ret < 0) {
-               t->error = -ERRNO_TO_PARA_ERROR(errno);
-               goto err;
+
+       for (;;) {
+               sz = btr_next_buffer(btrn, &buf);
+               if (sz == 0)
+                       break;
+               ret = write_nonblock(STDOUT_FILENO, buf, sz, 0);
+               if (ret <= 0)
+                       break;
+               btr_consume(btrn, ret);
        }
-       btr_consume(sot->btrn, ret);
-       return;
-err:
-       btr_del_node(sot->btrn);
+out:
+       if (ret < 0)
+               btr_remove_node(btrn);
+       t->error = ret;
 }
 /**
  * Initialize a stdout task structure with default values.
  *
  * \param sot The stdout task structure.
  *
- * This fills in the pre/post select function poinzters of the task structure
+ * This fills in the pre/post select function pointers of the task structure
  * given by \a sot.
  */
 void stdout_set_defaults(struct stdout_task *sot)
@@ -149,7 +145,7 @@ void stdout_set_defaults(struct stdout_task *sot)
                sot->task.pre_select = stdout_pre_select;
                sot->task.post_select = stdout_post_select;
        }
-       sprintf(sot->task.status, "stdout writer");
+       sprintf(sot->task.status, "stdout");
        ret = mark_fd_nonblocking(STDOUT_FILENO);
        if (ret >= 0)
                return;