]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - sched.c
sched: Introduce task notifications.
[paraslash.git] / sched.c
diff --git a/sched.c b/sched.c
index 2b54ce194fae897842ac94b39ff597828c8de8c4..e67579b32339c17101bbe2e3cedc45c9410cc7bc 100644 (file)
--- a/sched.c
+++ b/sched.c
@@ -55,6 +55,10 @@ static void sched_preselect(struct sched *s)
                        unregister_task(t);
                        continue;
                }
+               if (t->notification != 0) {
+                       sched_min_delay(s);
+                       break;
+               }
                if (!t->pre_select)
                        continue;
                t->pre_select(s, t);
@@ -91,6 +95,7 @@ static void sched_post_select(struct sched *s)
                if (t->error >= 0)
                        call_post_select(s, t);
 //             PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
+               t->notification = 0;
                if (t->error >= 0)
                        continue;
                /*
@@ -175,6 +180,7 @@ again:
 void register_task(struct sched *s, struct task *t)
 {
        PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
+       t->notification = 0;
        if (!s->pre_select_list.next)
                INIT_LIST_HEAD(&s->pre_select_list);
        if (!s->post_select_list.next)
@@ -245,6 +251,50 @@ char *get_task_list(struct sched *s)
        return msg;
 }
 
+/**
+ * Set the notification value of a task.
+ *
+ * \param t The task to notify.
+ * \param err A positive error code.
+ *
+ * Tasks which honor notifications are supposed to call \ref
+ * task_get_notification() in their post_select function and act on the
+ * returned notification value.
+ *
+ * If the scheduler detects during its pre_select loop that at least one task
+ * has been notified, the loop terminates, and the post_select methods of all
+ * taks are immediately called again.
+ *
+ * The notification for a task is reset after the call to its post_select
+ * method.
+ *
+ * \sa \ref task_get_notification().
+ */
+void task_notify(struct task *t, int err)
+{
+       assert(err > 0);
+       if (t->notification == -err) /* ignore subsequent notifications */
+               return;
+       PARA_INFO_LOG("notifying task %s: %s\n", t->status, para_strerror(err));
+       t->notification = -err;
+}
+
+/**
+ * Return the notification value of a task.
+ *
+ * \param t The task to get the notification value from.
+ *
+ * \return The notification value. If this is negative, the task has been
+ * notified by another task. Tasks are supposed to check for notifications by
+ * calling this function from their post_select method.
+ *
+ * \sa \ref task_notify().
+ */
+int task_get_notification(struct task *t)
+{
+       return t->notification;
+}
+
 /**
  * Set the select timeout to the minimal possible value.
  *