taskqueue_drain_all

Andriy Gapon avg at FreeBSD.org
Wed Oct 9 08:56:19 UTC 2013


I would like to propose to extend taskqueue API with taskqueue_drain_all.
A potential use case: I have a private taskqueue, several kinds of tasks get
executed via it and then I want to make sure that all of them are completed.
Obviously, I have a way to ensure that no new ones get enqueued.

Is this a good addition?
Or should like consider looping over all of my tasks externally to taskqueue
implementation?

A quick prototype:

--- a/sys/kern/subr_taskqueue.c
+++ b/sys/kern/subr_taskqueue.c
@@ -316,6 +316,7 @@ taskqueue_run_locked(struct taskqueue *queue)
 		wakeup(task);
 	}
 	TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
+	wakeup(&queue->tq_active);
 }

 void
@@ -402,6 +403,22 @@ taskqueue_drain(struct taskqueue *queue, struct task *task)
 }

 void
+taskqueue_drain_all(struct taskqueue *queue)
+{
+	struct task *task;
+
+	TQ_LOCK(queue);
+	while ((task = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
+		while (task->ta_pending != 0 || task_is_running(queue, task))
+			TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
+	}
+	while (TAILQ_FIRST(&queue->tq_active) != NULL)
+			TQ_SLEEP(queue, &queue->tq_active,
+			    &queue->tq_mutex, PWAIT, "-", 0);
+	TQ_UNLOCK(queue);
+}
+
+void
 taskqueue_drain_timeout(struct taskqueue *queue,
     struct timeout_task *timeout_task)
 {


-- 
Andriy Gapon


More information about the freebsd-hackers mailing list