From owner-freebsd-hackers@freebsd.org Fri Aug 25 04:32:42 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 F158ADEF55C for ; Fri, 25 Aug 2017 04:32:42 +0000 (UTC) (envelope-from khanzf@gmail.com) Received: from mail-pg0-x22c.google.com (mail-pg0-x22c.google.com [IPv6:2607:f8b0:400e:c05::22c]) (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 C2A627283C; Fri, 25 Aug 2017 04:32:42 +0000 (UTC) (envelope-from khanzf@gmail.com) Received: by mail-pg0-x22c.google.com with SMTP id b8so8496205pgn.5; Thu, 24 Aug 2017 21:32:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=SWDSmc2vO+nX6n8K0HDFi5JRF3ei/955B0IOPA/KU/I=; b=Ld1BM+fdWnOpOd/dQf++JyLw1bi1zT3KJ8NQAuaYEmzQPoELcInqScsGnVfUJkAItv L8zZ/CxhlpGOAbfW2J0XRwGr9bbjoEAL3e6DJjrJFRSrm+hAATkeFHWMSbsrkMZRPDnc LW7B806oXCnU6OKxvVvL1/9MsbQeQIPdlXkfgmnxjIMhM6bC+SG8FDhnH/ji0MsYS5I0 SkmVK3GJ0RuooKRfvK+hMnK/cHiRLqPO3t+BMatp+9P2nN0r++vnhMKOXuvfGKEAhwsW OWn6Iy/jzc+M+vl+lsOlmFMd57L3qezwCjImeY+xae5cCaFYiuskn6fppn50hL8qSNdI vReg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=SWDSmc2vO+nX6n8K0HDFi5JRF3ei/955B0IOPA/KU/I=; b=lbZIiEXxnv2a8FS4xUAyBQM1OIdA4K9EqL8Ugyf3wKJ+rogsRqEa1z5RzjyknGiQK/ VZbPVYIYVgZjT551pieBaUV+2NXwneEgwHKl3Kg9AjgdmGmOUC7kMQJJg+1xDeE3P+9P 0+Vil8oVVYzem88u6BYfxOdNujSGYUthFf8q2K0GlnWdZhLLTk3P3Zbz9bikqVHgWrP6 TYMuiiwEaOU3P8deoEHI0EgQdMRAonyb6zsdkgS42dTFN7C2Qqc5YxkM/98HIuojcm68 8gILkULn5oOAZm3G5qoryX4NL4nuY8UdVlAn+jeLKaAyAibGDHqRo3ConxcZCyxFKlou J9Gg== X-Gm-Message-State: AHYfb5hFkNoxlzB/nGcpZH5RgQ/uksDiHtSV5Sg0K6Sx9H8eioSc6ho9 SuaNR+vZvxYAd3mGALA5J/wPh7bKAlJLq+M= X-Received: by 10.84.232.204 with SMTP id x12mr9510018plm.364.1503635562098; Thu, 24 Aug 2017 21:32:42 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.183.208 with HTTP; Thu, 24 Aug 2017 21:32:21 -0700 (PDT) In-Reply-To: <34dfe18b-a3ca-515e-5270-43391b5769c0@FreeBSD.org> References: <34dfe18b-a3ca-515e-5270-43391b5769c0@FreeBSD.org> From: Farhan Khan Date: Fri, 25 Aug 2017 00:32:21 -0400 Message-ID: Subject: Re: taskqueue(9) guidance To: Matt Joras Cc: freebsd-hackers@freebsd.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.23 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: Fri, 25 Aug 2017 04:32:43 -0000 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 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 > >