]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - sched.c
sched: Introduce task_status().
[paraslash.git] / sched.c
diff --git a/sched.c b/sched.c
index 101cc4c0dad3706e841881b2a6d2989825fc58b0..fb0d4d63eacda82cb4e0779d72daa56c2634a324 100644 (file)
--- a/sched.c
+++ b/sched.c
@@ -45,8 +45,7 @@ static void unlink_and_free_task(struct task *t)
 {
        PARA_INFO_LOG("freeing task %s\n", t->status);
        list_del(&t->node);
-       if (t->owned_by_sched)
-               free(t);
+       free(t);
 }
 
 //#define SCHED_DEBUG 1
@@ -75,14 +74,14 @@ static unsigned sched_post_select(struct sched *s)
        unsigned num_running_tasks = 0;
 
        list_for_each_entry_safe(t, tmp, &s->task_list, node) {
-               if (t->error < 0)
+               if (t->error < 0) {
+                       if (t->dead) /* task has been reaped */
+                               unlink_and_free_task(t);
                        continue;
+               }
                call_post_select(s, t);
                t->notification = 0;
-               if (t->error < 0) {
-                       if (!t->owned_by_sched)
-                               list_del(&t->node);
-               } else
+               if (t->error >= 0)
                        num_running_tasks++;
        }
        return num_running_tasks;
@@ -138,6 +137,51 @@ again:
        goto again;
 }
 
+/**
+ * Obtain the error status of a task and deallocate its resources.
+ *
+ * \param tptr Identifies the task to reap.
+ *
+ * This function is similar to wait(2) in that it returns information about a
+ * terminated task and allows to release the resources associated with the
+ * task. Until this function is called, the terminated task remains in a zombie
+ * state.
+ *
+ * \return If \a tptr is \p NULL, or \a *tptr is \p NULL, the function does
+ * nothing and returns zero. Otherwise, it is checked whether the task
+ * identified by \a tptr is still running. If it is, the function returns zero
+ * and again, no action is taken. Otherwise the (negative) error code of the
+ * terminated task is returned and \a *tptr is set to \p NULL. The task will
+ * then be removed removed from the scheduler task list.
+ *
+ * \sa \ref sched_shutdown(), wait(2).
+ */
+int task_reap(struct task **tptr)
+{
+       struct task *t;
+
+       if (!tptr)
+               return 0;
+       t = *tptr;
+       if (!t)
+               return 0;
+       if (t->error >= 0)
+               return 0;
+       if (t->dead) /* will be freed in sched_post_select() */
+               return 0;
+       /*
+        * With list_for_each_entry_safe() it is only safe to remove the
+        * _current_ list item. Since we are being called from the loop in
+        * schedule() via some task's ->post_select() function, freeing the
+        * given task here would result in use-after-free bugs in schedule().
+        * So we only set t->dead which tells schedule() to free the task in
+        * the next iteration of its loop.
+        */
+       t->dead = true;
+       *tptr = NULL;
+       return t->error;
+}
+
 /**
  * Deallocate all resources of all tasks of a scheduler instance.
  *
@@ -158,25 +202,6 @@ void sched_shutdown(struct sched *s)
        }
 }
 
-/**
- * Add a task to the scheduler. Deprecated.
- *
- * \param t The task to add.
- * \param s The scheduler instance to add the task to.
- *
- * \sa task::pre_select, task::post_select
- */
-void register_task(struct sched *s, struct task *t)
-{
-       PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
-       assert(t->post_select);
-       t->notification = 0;
-       t->owned_by_sched = false;
-       if (!s->task_list.next)
-               INIT_LIST_HEAD(&s->task_list);
-       list_add_tail(&t->node, &s->task_list);
-}
-
 /**
  * Add a task to the scheduler task list.
  *
@@ -199,10 +224,10 @@ struct task *task_register(struct task_info *info, struct sched *s)
        t->status[sizeof(t->status) - 1] = '\0';
        t->notification = 0;
        t->error = 0;
+       t->dead = false;
        t->pre_select = info->pre_select;
        t->post_select = info->post_select;
        t->context = info->context;
-       t->owned_by_sched = true;
        list_add_tail(&t->node, &s->task_list);
        return t;
 }
@@ -217,7 +242,6 @@ struct task *task_register(struct task_info *info, struct sched *s)
  */
 void *task_context(struct task *t)
 {
-       assert(t->owned_by_sched);
        return t->context;
 }
 
@@ -239,7 +263,7 @@ char *get_task_list(struct sched *s)
        list_for_each_entry_safe(t, tmp, &s->task_list, node) {
                char *tmp_msg;
                tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
-                       t->error < 0? "zombie" : "running",
+                       t->error < 0? (t->dead? "dead" : "zombie") : "running",
                        t->status);
                free(msg);
                msg = tmp_msg;
@@ -291,6 +315,25 @@ int task_get_notification(const struct task *t)
        return t->notification;
 }
 
+/**
+ * Return the status value of a task.
+ *
+ * \param t The task to get the status value from.
+ *
+ * \return Zero if task does not exist, one if task is running, negative error
+ * code if task has terminated.
+ */
+int task_status(const struct task *t)
+{
+       if (!t)
+               return 0;
+       if (t->dead)
+               return 0;
+       if (t->error >= 0)
+               return 1;
+       return t->error;
+}
+
 /**
  * Set the notification value of all tasks of a scheduler instance.
  *