Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 May 2000 21:24:13 +0100 (BST)
From:      Doug Rabson <dfr@nlsystems.com>
To:        arch@freebsd.org
Subject:   A new api for asynchronous task execution (3)
Message-ID:  <Pine.BSF.4.21.0005182120520.73457-400000@salmon.nlsystems.com>

index | next in thread | raw e-mail

[-- Attachment #1 --]
Ok, I think this version is starting to look worth committing. This one
adds priority sorted queueing, uses structure field prefixes and provides
convenience macros for declaring queues.


TASKQUEUE(9)	       FreeBSD Kernel Developer's Manual	  TASKQUEUE(9)

NAME
     taskqueue - asynchronous task execution

SYNOPSIS
     #include <sys/param.h>
     #include <sys/queue.h>
     #include <sys/taskqueue.h>

     typedef void (*task_fn)(void *context, int pending);

     typedef void (*taskqueue_enqueue_fn)(void *context);

     struct task {
	     STAILQ_ENTRY(task)      ta_link;	     /* link for queue */
	     int		     ta_pending;     /* count times queued */
	     int		     ta_priority;    /* priority of task in queue */
	     task_fn		     ta_func;	     /* task handler */
	     void		     *ta_context;    /* argument for handler */
     };


     struct taskqueue *
     taskqueue_create(const char *name, int mflags,
	     taskqueue_enqueue_fn enqueue, void *context)

     void
     taskqueue_free(struct taskqueue *queue)

     struct taskqueue *
     taskqueue_find(const char *name)

     void
     taskqueue_enqueue(struct taskqueue *queue, struct task *task)

     void
     taskqueue_run(struct taskqueue *queue)

     TASK_INIT(task, priority, func, context)

     TASKQUEUE_DECLARE(name)

     TASKQUEUE_DEFINE(name, enqueue, context, init)

DESCRIPTION
     These functions provide a simple interface for asynchronous execution of
     code.

     The function taskqueue_create() is used to create new queues.  The argu-
     ments to taskqueue_create() include a name which should be unique, a set
     of malloc(9) flags which specify whether the call to malloc() is allowed
     to sleep and a function which is called from taskqueue_enqueue() when a
     task is added to the queue to allow the queue to arrange to be run later
     (for instance by scheduling a software interrupt or waking a kernel
     thread).

     The function taskqueue_free() should be used to remove the queue from the
     global list of queues and free the memory used by the queue.  Any tasks
     which are on the queue will be executed at this time.

     The system maintains a list of all queues which can be searched using
     taskqueue_find().	The first queue whose name matches is returned, other-
     wise NULL.

     To add a task to the list of tasks queued on a taskqueue, call
     taskqueue_enqueue() with pointers to the queue and task.  If the task's
     ta_pending field is non-zero, then it is simply incremented to reflect
     the number of times the task was enqueued.  Otherwise, the task is added
     to the list before the first task which has a lower ta_priority value or
     at the end of the list if no tasks have a lower priority.	Enqueueing a
     task does not perform any memory allocation which makes it suitable for
     calling from an interrupt handler.

     To execute all the tasks on a queue, call taskqueue_run().  When a task
     is executed, first it is removed from the queue, the value of ta_pending
     is recorded and the field is zeroed.  The function ta_func from the task
     structure is called with the value of the field ta_context its first ar-
     gument and the value of ta_pending as its second argument.

     A convenience macro, TASK_INIT(task, priority, func, context) is provided
     to initialise a task structure.  The values of priority, func, and
     context are simply copied into the task structure fields and the
     ta_pending field is cleared.

     Two macros TASKQUEUE_DECLARE(name) and TASKQUEUE_DEFINE(name, enqueue,
     context, init) are used to declare a reference to a global queue and to
     define the implementation of the queue.  The TASKQUEUE_DEFINE() macro ar-
     ranges to call taskqueue_create() with the values of its name, enqueue
     and context arguments during system initialisation.  After calling
     taskqueue_create(), the init argument to the macro is executed as a C
     statement, allowing any further initialisation to be performed (such as
     registering an interrupt handler etc.)

     The system provides a global taskqueue, taskqueue_swi, which is run via a
     software interrupt mechanism.  To use this queue, call
     taskqueue_enqueue() with the value of the global variable taskqueue_swi.
     The queue will be run at splsofttq().

     This queue can be used, for instance, for implementing interrupt handlers
     which must perform a significant amount of processing in the handler.
     The hardware interrupt handler would perform minimal processing of the
     interrupt and then enqueue a task to finish the work.  This reduces the
     amount of time spent with interrupts disabled to a minimum.

HISTORY
     This interface first appeared in FreeBSD 5.0.  There is a similar facili-
     ty called tqueue in the Linux kernel.

AUTHORS
     This man page was written by Doug Rabson.

FreeBSD 			 May 12, 2000				     2

-- 
Doug Rabson				Mail:  dfr@nlsystems.com
Nonlinear Systems Ltd.			Phone: +44 20 8442 9037


[-- Attachment #2 --]
.\" -*- nroff -*-
.\"
.\" Copyright (c) 2000 Doug Rabson
.\"
.\" All rights reserved.
.\"
.\" This program is free software.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd May 12, 2000
.Dt TASKQUEUE 9
.Os FreeBSD
.Sh NAME
.Nm taskqueue
.Nd asynchronous task execution
.Sh SYNOPSIS
.Fd #include <sys/param.h>
.Fd #include <sys/queue.h>
.Fd #include <sys/taskqueue.h>
.Bd -literal

typedef void (*task_fn)(void *context, int pending);

typedef void (*taskqueue_enqueue_fn)(void *context);

struct task {
	STAILQ_ENTRY(task)	ta_link;	/* link for queue */
	int			ta_pending;	/* count times queued */
	int			ta_priority;	/* priority of task in queue */
	task_fn			ta_func;	/* task handler */
	void			*ta_context;	/* argument for handler */
};

.Ed
.Ft struct taskqueue *
.Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context"
.Ft void
.Fn taskqueue_free "struct taskqueue *queue"
.Ft struct taskqueue *
.Fn taskqueue_find "const char *name"
.Ft void
.Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task"
.Ft void
.Fn taskqueue_run "struct taskqueue *queue"
.Fn TASK_INIT "task" "priority" "func" "context"
.Fn TASKQUEUE_DECLARE "name"
.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init"
.Sh DESCRIPTION
.Pp
These functions provide a simple interface for asynchronous execution
of code.
.Pp
The function
.Fn taskqueue_create
is used to create new queues.
The arguments to
.Fn taskqueue_create
include a name which should be unique,
a set of
.Xr malloc 9
flags which specify whether the call to
.Fn malloc
is allowed to sleep
and a function which is called from
.Fn taskqueue_enqueue
when a task is added to the queue
to allow the queue to arrange to be run later
(for instance by scheduling a software interrupt or waking a kernel
thread).
.Pp
The function
.Fn taskqueue_free
should be used to remove the queue from the global list of queues
and free the memory used by the queue.
Any tasks which are on the queue will be executed at this time.
.Pp
The system maintains a list of all queues which can be searched using
.Fn taskqueue_find .
The first queue whose name matches is returned, otherwise
.Va NULL .
.Pp
To add a task to the list of tasks queued on a taskqueue, call
.Fn taskqueue_enqueue
with pointers to the queue and task.
If the task's
.Va ta_pending
field is non-zero,
then it is simply incremented to reflect the number of times the task
was enqueued.
Otherwise,
the task is added to the list before the first task which has a lower
.Va ta_priority
value or at the end of the list if no tasks have a lower priority.
Enqueueing a task does not perform any memory allocation which makes
it suitable for calling from an interrupt handler.
.Pp
To execute all the tasks on a queue,
call
.Fn taskqueue_run .
When a task is executed,
first it is removed from the queue,
the value of
.Va ta_pending
is recorded and the field is zeroed.
The function
.Va ta_func
from the task structure is called with the value of the field
.Va ta_context
its first argument
and the value of
.Va ta_pending
as its second argument.
.Pp
A convenience macro,
.Fn TASK_INIT "task" "priority" "func" "context"
is provided to initialise a
.Va task
structure.
The values of
.Va priority ,
.Va func ,
and
.Va context
are simply copied into the task structure fields and the
.Va ta_pending
field is cleared.
.Pp
Two macros
.Fn TASKQUEUE_DECLARE "name"
and
.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init"
are used to declare a reference to a global queue
and to define the implementation of the queue.
The
.Fn TASKQUEUE_DEFINE
macro arranges to call
.Fn taskqueue_create
with the values of its
.Va name ,
.Va enqueue
and
.Va context
arguments during system initialisation.
After calling
.Fn taskqueue_create ,
the
.Va init
argument to the macro is executed as a C statement,
allowing any further initialisation to be performed
(such as registering an interrupt handler etc.)
.Pp
The system provides a global taskqueue,
.Va taskqueue_swi ,
which is run via a software interrupt mechanism.
To use this queue,
call
.Fn taskqueue_enqueue
with the value of the global variable
.Va taskqueue_swi .
The queue will be run at
.Fn splsofttq .
.Pp
This queue can be used,
for instance, for implementing interrupt handlers which must perform a
significant amount of processing in the handler.
The hardware interrupt handler would perform minimal processing of the
interrupt and then enqueue a task to finish the work.
This reduces the amount of time spent with interrupts disabled
to a minimum.
.Sh HISTORY
This interface first appeared in
.Fx 5.0 .
There is a similar facility called tqueue in the Linux kernel.
.Sh AUTHORS
This man page was written by
.An Doug Rabson .

[-- Attachment #3 --]
/*-
 * Copyright (c) 2000 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

#ifndef _SYS_TASKQUEUE_H_
#define _SYS_TASKQUEUE_H_

#ifdef _KERNEL

struct taskqueue;

/*
 * Each task includes a function which is called from
 * taskqueue_run(). The first argument is taken from the 'ta_context'
 * field of struct task and the second argument is a count of how many
 * times the task was enqueued before the call to taskqueue_run().
 */
typedef void (*task_fn)(void *context, int pending);

/*
 * A notification callback function which is called from
 * taskqueue_enqueue(). The context argument is given in the call to
 * taskqueue_create(). This function would normally be used to allow the
 * queue to arrange to run itself later (e.g. by scheduling a software
 * interrupt or waking a kernel thread).
 */
typedef void (*taskqueue_enqueue_fn)(void *context);

struct task {
	STAILQ_ENTRY(task)	ta_link;	/* link for queue */
	int			ta_pending;	/* count times queued */
	int			ta_priority;	/* priority of task in queue */
	task_fn			ta_func;	/* task handler */
	void			*ta_context;	/* argument for handler */
};

#if 0				/* later */
/*
 * Flags for taskqueue_create() which will control the properties of a
 * mutex will be used to protect the queue.
 *
 * Note: this is not currently used but will be used in a future
 * implementation of SMP.
 */
#define TQ_DEF		0	/* use blocking mutex */
#define TQ_SPIN		1	/* use spin mutex */
#endif

struct taskqueue	*taskqueue_create(const char *name, int mflags,
					  taskqueue_enqueue_fn enqueue,
					  void *context);
void			taskqueue_free(struct taskqueue *queue);
struct taskqueue	*taskqueue_find(const char *name);
void			taskqueue_enqueue(struct taskqueue *queue,
					  struct task *task);
void			taskqueue_run(struct taskqueue *queue);

/*
 * Initialise a task structure.
 */
#define TASK_INIT(task, priority, func, context) do {	\
    task->ta_pending = 0;				\
    task->ta_priority = priority;			\
    task->ta_func = func;				\
    task->ta_context = context;				\
} while (0)

/*
 * Declare a reference to a taskqueue.
 */
#define TASKQUEUE_DECLARE(name)			\
						\
extern struct taskqueue *taskqueue_##name

/*
 * Define and initialise a taskqueue.
 */
#define TASKQUEUE_DEFINE(name, enqueue, context, init)		\
									\
struct taskqueue *taskqueue_##name;					\
									\
static void								\
taskqueue_define_##name(void *arg)					\
{									\
	taskqueue_##name =						\
		taskqueue_create(#name, M_NOWAIT, enqueue, context);	\
	init;								\
}									\
									\
SYSINIT(taskqueue_##name, SI_SUB_CONFIGURE, SI_ORDER_SECOND,		\
	taskqueue_define_##name, NULL);

/*
 * This queue is serviced by a software interrupt handler. To enqueue
 * a task, call taskqueue_enqueue(&taskqueue_swi, &task).
 */
TASKQUEUE_DECLARE(swi);

#endif /* _KERNEL */

#endif /* !_SYS_TASKQUEUE_H_ */

[-- Attachment #4 --]
/*-
 * Copyright (c) 2000 Doug Rabson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	$FreeBSD$
 */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/taskqueue.h>
#include <sys/interrupt.h>
#include <sys/malloc.h>
#include <machine/ipl.h>

MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");

static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;

struct taskqueue {
	STAILQ_ENTRY(taskqueue)	tq_link;
	STAILQ_HEAD(, task)	tq_queue;
	const char		*tq_name;
	taskqueue_enqueue_fn	tq_enqueue;
	void			*tq_context;
};

struct taskqueue *
taskqueue_create(const char *name, int mflags,
		 taskqueue_enqueue_fn enqueue, void *context)
{
	struct taskqueue *queue;
	static int once = 1;

	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags);
	if (!queue)
		return 0;

	if (once) {
		STAILQ_INIT(&taskqueue_queues);
		once = 0;
	}
	STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
	STAILQ_INIT(&queue->tq_queue);
	queue->tq_name = name;
	queue->tq_enqueue = enqueue;
	queue->tq_context = context;

	return queue;
}

void
taskqueue_free(struct taskqueue *queue)
{
	int s = splhigh();
	taskqueue_run(queue);
	STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
	splx(s);

	free(queue, M_TASKQUEUE);
}

struct taskqueue *
taskqueue_find(const char *name)
{
	struct taskqueue *queue;

	STAILQ_FOREACH(queue, &taskqueue_queues, tq_link)
		if (!strcmp(queue->tq_name, name))
			return queue;
	return 0;
}

void
taskqueue_enqueue(struct taskqueue *queue, struct task *task)
{
	struct task *ins;
	struct task *prev;

	int s = splhigh();

	/*
	 * Count multiple enqueues.
	 */
	if (task->ta_pending) {
		task->ta_pending++;
		splx(s);
		return;
	}

	/*
	 * Optimise the case when all tasks have the same priority.
	 */
	prev = STAILQ_LAST(&queue->tq_queue);
	if (!prev || prev->ta_priority >= task->ta_priority) {
		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
	} else {
		prev = 0;
		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
			if (ins->ta_priority < task->ta_priority)
				break;

		if (prev)
			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
		else
			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
	}

	task->ta_pending = 1;
	if (queue->tq_enqueue)
		queue->tq_enqueue(queue->tq_context);

	splx(s);
}

void
taskqueue_run(struct taskqueue *queue)
{
	int s;
	struct task *task;
	int pending;

	s = splhigh();
	while (STAILQ_FIRST(&queue->tq_queue)) {
		/*
		 * Carefully remove the first task from the queue and
		 * zero its pending count.
		 */
		task = STAILQ_FIRST(&queue->tq_queue);
		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
		pending = task->ta_pending;
		task->ta_pending = 0;
		splx(s);

		task->ta_func(task->ta_context, pending);

		s = splhigh();
	}
	splx(s);
}

static void
taskqueue_swi_enqueue(void *context)
{
	setsofttq();
}

static void
taskqueue_swi_run(void)
{
	taskqueue_run(taskqueue_swi);
}

TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
		 register_swi(SWI_TQ, taskqueue_swi_run));
home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0005182120520.73457-400000>