Date: Fri, 25 Aug 2017 00:32:21 -0400 From: Farhan Khan <khanzf@gmail.com> To: Matt Joras <mjoras@freebsd.org> Cc: freebsd-hackers@freebsd.org Subject: Re: taskqueue(9) guidance Message-ID: <CAFd4kYAN4uYuqFcrv-f9Ut9ywx64mZW9ZZKrqfW_4djDjhAqKw@mail.gmail.com> In-Reply-To: <34dfe18b-a3ca-515e-5270-43391b5769c0@FreeBSD.org> References: <CAFd4kYCj7i5oLBvTNUZj1-oKEY88XjDNcVUbyUutszStRyt2zg@mail.gmail.com> <34dfe18b-a3ca-515e-5270-43391b5769c0@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
Thank you for your explanation! As you mentioned, I just needed to use taskqueue_enqueue(9). I did not use the system-wide taskqueue, but I saw it in the manual. I personally did not find any examples on Google and found the man pages a bit too abstract, so I cleaned up my code example and put it online for the next person. Hope it helps! https://gist.github.com/khanzf/465eec49d5008f460aa0571604267d7b -- Farhan Khan PGP Fingerprint: 782F 342B 5B08 0D2F F4E8 82C3 FFA1 CAE1 6536 51CA On Thu, Aug 24, 2017 at 12:40 AM, Matt Joras <mjoras@freebsd.org> wrote: > 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?CAFd4kYAN4uYuqFcrv-f9Ut9ywx64mZW9ZZKrqfW_4djDjhAqKw>