- 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)
- INIT_LIST_HEAD(&s->post_select_list);
- if (t->pre_select) {
- PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
- list_add_tail(&t->pre_select_node, &s->pre_select_list);
- }
- if (t->post_select) {
- PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
- list_add_tail(&t->post_select_node, &s->post_select_list);
+ struct task *t;
+ int ret;
+
+ if (!tptr)
+ return 0;
+ t = *tptr;
+ if (!t)
+ return 0;
+ if (t->status >= 0)
+ return 0;
+ ret = t->status;
+ /*
+ * 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 the task status to TS_DEAD which tells schedule() to
+ * free the task in the next iteration of its loop.
+ */
+ t->status = TS_DEAD;
+
+ *tptr = NULL;
+ return ret;
+}
+
+/**
+ * Deallocate all resources of all tasks of a scheduler instance.
+ *
+ * \param s The scheduler instance.
+ *
+ * This should only be called after \ref schedule() has returned.
+ */
+void sched_shutdown(struct sched *s)
+{
+ struct task *t, *tmp;
+
+ list_for_each_entry_safe(t, tmp, &s->task_list, node) {
+ if (t->status == TS_RUNNING)
+ /* The task list should contain only terminated tasks. */
+ PARA_WARNING_LOG("shutting down running task %s\n",
+ t->name);
+ unlink_and_free_task(t);