Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 23 Aug 2017 21:40:42 -0700
From:      Matt Joras <mjoras@FreeBSD.org>
To:        Farhan Khan <khanzf@gmail.com>, freebsd-hackers@freebsd.org
Subject:   Re: taskqueue(9) guidance
Message-ID:  <34dfe18b-a3ca-515e-5270-43391b5769c0@FreeBSD.org>
In-Reply-To: <CAFd4kYCj7i5oLBvTNUZj1-oKEY88XjDNcVUbyUutszStRyt2zg@mail.gmail.com>
References:  <CAFd4kYCj7i5oLBvTNUZj1-oKEY88XjDNcVUbyUutszStRyt2zg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 08/23/2017 17:51, Farhan Khan wrote:
> Hi all,
>
> I am trying to understand taskqueue(9) through writing some code, but am
> unable to get a functioning example. The expected code I thought does not
> run.
>
> I reviewed some sample code within the kernel, mostly drivers. The general
> pattern I identified was taskqueue_create(9), taskqueue_start_threads(9),
> and finally the TASK_INIT macro.
This is more or less the correct steps to initialize a taskqueue and a
task. Note however that you often don't need to create your own
taskqueue. There are system-wide taskqueues that are available for use,
see taskqueue(9). The least specialized one is called taskqueue_thread,
and I would suggest you use it instead of making your own taskqueue. 
Additionally I will note that your taskqueue was declared inside the
initialization function, and thus will go out of scope after it returns.
This is not what you want.
> I also created some structures and place them within the "struct taskqueue"
> and "struct task". I am not certain why this done, rather than allocate
> those who structures on their own, but it appeared to be the standard.
This is a common C pattern that mimics the notion of object inheritance
in other languages. The basic idea being that you can make a
"specialized" version of a task or taskqueue (e.g. struct
my_driver_task) and still use the taskqueue(9) functions since the
structure's first member is a struct task or struct taskqueue. For your
use case this does not seem necessary.
> My code is below:
>
> https://pastebin.com/dFqPsA5A
>
> I am expecting to see the repeater_proc() function to execute, but that
> does not occur. Also, I did not specify the frequency, whether it should
> repeat, etc.
>
> I do not understand why the code is not running or what mistake I am making.

A couple things. Tasks are only run once they are enqueue'd to a
taskqueue. You've initialized your task but that simply fills out the
structure, it doesn't associate it with the queue. To do that you need
to use either taskqueue_enqueue(9) or taskqueue_enqueue_timeout(9). The
latter will enqueue the task after a period of time has passed. The task
will then eventually be run exactly once. There is no inherent notion of
repeating or frequency for tasks. To achieve repetition you are
responsible for repeatedly enqueueing your task to a taskqueue.

Hope that helps.

Matt Joras




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?34dfe18b-a3ca-515e-5270-43391b5769c0>