From owner-freebsd-hackers@freebsd.org Thu Aug 24 05:34:27 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6E8E0DD677B for ; Thu, 24 Aug 2017 05:34:27 +0000 (UTC) (envelope-from matt.joras@gmail.com) Received: from mail-qk0-f195.google.com (mail-qk0-f195.google.com [209.85.220.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3596C6B293 for ; Thu, 24 Aug 2017 05:34:26 +0000 (UTC) (envelope-from matt.joras@gmail.com) Received: by mail-qk0-f195.google.com with SMTP id p2so1421468qkf.3 for ; Wed, 23 Aug 2017 22:34:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=XLgvLQg2l/gFnJKm5H4uZffGgKgzZB7XZYrM3pBQZOI=; b=nRODspZ10jwBIawLw8BwoqpPXnHFAq2X9ywftMfywqQbnA58dueSJ+4PDDuftFhAA6 UzHgcGcdyKn9Ir8kN9Hc/CJmDk5bSQJHRMYdNjcH0Jd1BpxaKqvX/I0cNTIseeDYgIRC G/lMbvsZm6LxwyAQ8qos2QaX5sxI5Yk9mv5hB9PHutARXNaVcIPICCKqntgoHAIYWvYz GWxp43H8hpnH56RGogGKjP5q6hmhOBMCn4Fz6QdOQx4XjjmLvEiUaPnvbFqX0akhQv4H xevM4WC/1zMM0y5dpu0UbPKjhP2WHMSFBkvAW6JL7E16Tr22eRhGG7TgKb16+4xoo5tX yZLA== X-Gm-Message-State: AHYfb5gpIvWQuRijOWWs81Zqb3moOFv7N9bTtMiHdcC9nLA47galoqt+ XTIKiPWctvjJtX6XObo= X-Received: by 10.55.165.69 with SMTP id o66mr6445060qke.347.1503549645437; Wed, 23 Aug 2017 21:40:45 -0700 (PDT) Received: from [192.168.2.122] (71-212-20-168.tukw.qwest.net. [71.212.20.168]) by smtp.gmail.com with ESMTPSA id 26sm2067558qtp.60.2017.08.23.21.40.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Aug 2017 21:40:44 -0700 (PDT) Subject: Re: taskqueue(9) guidance To: Farhan Khan , freebsd-hackers@freebsd.org References: From: Matt Joras Message-ID: <34dfe18b-a3ca-515e-5270-43391b5769c0@FreeBSD.org> Date: Wed, 23 Aug 2017 21:40:42 -0700 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Aug 2017 05:34:27 -0000 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