182323455SDoug Rabson.\" -*- nroff -*- 282323455SDoug Rabson.\" 382323455SDoug Rabson.\" Copyright (c) 2000 Doug Rabson 482323455SDoug Rabson.\" 582323455SDoug Rabson.\" All rights reserved. 682323455SDoug Rabson.\" 782323455SDoug Rabson.\" This program is free software. 882323455SDoug Rabson.\" 982323455SDoug Rabson.\" Redistribution and use in source and binary forms, with or without 1082323455SDoug Rabson.\" modification, are permitted provided that the following conditions 1182323455SDoug Rabson.\" are met: 1282323455SDoug Rabson.\" 1. Redistributions of source code must retain the above copyright 1382323455SDoug Rabson.\" notice, this list of conditions and the following disclaimer. 1482323455SDoug Rabson.\" 2. Redistributions in binary form must reproduce the above copyright 1582323455SDoug Rabson.\" notice, this list of conditions and the following disclaimer in the 1682323455SDoug Rabson.\" documentation and/or other materials provided with the distribution. 1782323455SDoug Rabson.\" 1882323455SDoug Rabson.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 1982323455SDoug Rabson.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2082323455SDoug Rabson.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2182323455SDoug Rabson.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 2282323455SDoug Rabson.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2382323455SDoug Rabson.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2482323455SDoug Rabson.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2582323455SDoug Rabson.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2682323455SDoug Rabson.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2782323455SDoug Rabson.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2882323455SDoug Rabson.\" 2982323455SDoug Rabson.\" $FreeBSD$ 3082323455SDoug Rabson.\" 31*f37b7fc2SIan Lepore.Dd July 30, 2017 3282323455SDoug Rabson.Dt TASKQUEUE 9 3382323455SDoug Rabson.Os 3482323455SDoug Rabson.Sh NAME 3582323455SDoug Rabson.Nm taskqueue 3682323455SDoug Rabson.Nd asynchronous task execution 3782323455SDoug Rabson.Sh SYNOPSIS 3832eef9aeSRuslan Ermilov.In sys/param.h 39f16b3c0dSChad David.In sys/kernel.h 40f16b3c0dSChad David.In sys/malloc.h 4132eef9aeSRuslan Ermilov.In sys/queue.h 4232eef9aeSRuslan Ermilov.In sys/taskqueue.h 4382323455SDoug Rabson.Bd -literal 44f674e945SBruce M Simpsontypedef void (*task_fn_t)(void *context, int pending); 4582323455SDoug Rabson 4682323455SDoug Rabsontypedef void (*taskqueue_enqueue_fn)(void *context); 4782323455SDoug Rabson 4882323455SDoug Rabsonstruct task { 4982323455SDoug Rabson STAILQ_ENTRY(task) ta_link; /* link for queue */ 50cf82599dSSam Leffler u_short ta_pending; /* count times queued */ 51cf82599dSSam Leffler u_short ta_priority; /* priority of task in queue */ 52f674e945SBruce M Simpson task_fn_t ta_func; /* task handler */ 5382323455SDoug Rabson void *ta_context; /* argument for handler */ 5482323455SDoug Rabson}; 55c3bd10b4SKonstantin Belousov 56fdbc7174SWill Andrewsenum taskqueue_callback_type { 57fdbc7174SWill Andrews TASKQUEUE_CALLBACK_TYPE_INIT, 58fdbc7174SWill Andrews TASKQUEUE_CALLBACK_TYPE_SHUTDOWN, 59fdbc7174SWill Andrews}; 60fdbc7174SWill Andrews 61fdbc7174SWill Andrewstypedef void (*taskqueue_callback_fn)(void *context); 62fdbc7174SWill Andrews 63c3bd10b4SKonstantin Belousovstruct timeout_task; 6482323455SDoug Rabson.Ed 6582323455SDoug Rabson.Ft struct taskqueue * 668f668ffaSOleksandr Tymoshenko.Fn taskqueue_create "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context" 672eb30874SOleksandr Tymoshenko.Ft struct taskqueue * 682eb30874SOleksandr Tymoshenko.Fn taskqueue_create_fast "const char *name" "int mflags" "taskqueue_enqueue_fn enqueue" "void *context" 69fdbc7174SWill Andrews.Ft int 70fdbc7174SWill Andrews.Fn taskqueue_start_threads "struct taskqueue **tqp" "int count" "int pri" "const char *name" "..." 71ec5d37f4SBenjamin Kaduk.Ft int 72ec5d37f4SBenjamin Kaduk.Fo taskqueue_start_threads_pinned 73ec5d37f4SBenjamin Kaduk.Fa "struct taskqueue **tqp" "int count" "int pri" "int cpu_id" 74ec5d37f4SBenjamin Kaduk.Fa "const char *name" "..." 75ec5d37f4SBenjamin Kaduk.Fc 76fdbc7174SWill Andrews.Ft void 77fdbc7174SWill Andrews.Fn taskqueue_set_callback "struct taskqueue *queue" "enum taskqueue_callback_type cb_type" "taskqueue_callback_fn callback" "void *context" 7882323455SDoug Rabson.Ft void 7982323455SDoug Rabson.Fn taskqueue_free "struct taskqueue *queue" 8082323455SDoug Rabson.Ft int 8182323455SDoug Rabson.Fn taskqueue_enqueue "struct taskqueue *queue" "struct task *task" 8224b4e9d1SScott Long.Ft int 83c3bd10b4SKonstantin Belousov.Fn taskqueue_enqueue_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "int ticks" 84c3bd10b4SKonstantin Belousov.Ft int 85*f37b7fc2SIan Lepore.Fn taskqueue_enqueue_timeout_sbt "struct taskqueue *queue" "struct timeout_task *timeout_task" "sbintime_t sbt" "sbintime_t pr" "int flags" 86*f37b7fc2SIan Lepore.Ft int 87f46276a9SMatthew D Fleming.Fn taskqueue_cancel "struct taskqueue *queue" "struct task *task" "u_int *pendp" 88c3bd10b4SKonstantin Belousov.Ft int 89c3bd10b4SKonstantin Belousov.Fn taskqueue_cancel_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" "u_int *pendp" 9082323455SDoug Rabson.Ft void 91fff7ff71SGleb Smirnoff.Fn taskqueue_drain "struct taskqueue *queue" "struct task *task" 92c3bd10b4SKonstantin Belousov.Ft void 93c3bd10b4SKonstantin Belousov.Fn taskqueue_drain_timeout "struct taskqueue *queue" "struct timeout_task *timeout_task" 94cb03508fSAndriy Gapon.Ft void 95cb03508fSAndriy Gapon.Fn taskqueue_drain_all "struct taskqueue *queue" 96cb03508fSAndriy Gapon.Ft void 97cb03508fSAndriy Gapon.Fn taskqueue_block "struct taskqueue *queue" 98cb03508fSAndriy Gapon.Ft void 99cb03508fSAndriy Gapon.Fn taskqueue_unblock "struct taskqueue *queue" 100159ef108SPawel Jakub Dawidek.Ft int 101159ef108SPawel Jakub Dawidek.Fn taskqueue_member "struct taskqueue *queue" "struct thread *td" 102a92f0ee8SMatthew D Fleming.Ft void 103bf73d4d2SMatthew D Fleming.Fn taskqueue_run "struct taskqueue *queue" 104c3bd10b4SKonstantin Belousov.Fn TASK_INIT "struct task *task" "int priority" "task_fn_t func" "void *context" 105a7f5f794SJohn Baldwin.Fn TASK_INITIALIZER "int priority" "task_fn_t func" "void *context" 10682323455SDoug Rabson.Fn TASKQUEUE_DECLARE "name" 107f16b3c0dSChad David.Fn TASKQUEUE_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init" 1082eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE "name" "taskqueue_enqueue_fn enqueue" "void *context" "init" 109227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE_THREAD "name" 1102eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE_THREAD "name" 111c3bd10b4SKonstantin Belousov.Fn TIMEOUT_TASK_INIT "struct taskqueue *queue" "struct timeout_task *timeout_task" "int priority" "task_fn_t func" "void *context" 11282323455SDoug Rabson.Sh DESCRIPTION 11382323455SDoug RabsonThese functions provide a simple interface for asynchronous execution 11482323455SDoug Rabsonof code. 11582323455SDoug Rabson.Pp 11682323455SDoug RabsonThe function 11782323455SDoug Rabson.Fn taskqueue_create 11882323455SDoug Rabsonis used to create new queues. 11982323455SDoug RabsonThe arguments to 12082323455SDoug Rabson.Fn taskqueue_create 121cf82599dSSam Lefflerinclude a name that should be unique, 12282323455SDoug Rabsona set of 12382323455SDoug Rabson.Xr malloc 9 124cf82599dSSam Lefflerflags that specify whether the call to 12582323455SDoug Rabson.Fn malloc 126cf82599dSSam Leffleris allowed to sleep, 127cf82599dSSam Lefflera function that is called from 12882323455SDoug Rabson.Fn taskqueue_enqueue 129cf82599dSSam Lefflerwhen a task is added to the queue, 130cf82599dSSam Lefflerand a pointer to the memory location where the identity of the 131cf82599dSSam Lefflerthread that services the queue is recorded. 13282323455SDoug Rabson.\" XXX The rest of the sentence gets lots in relation to the first part. 133cf82599dSSam LefflerThe function called from 134cf82599dSSam Leffler.Fn taskqueue_enqueue 135cf82599dSSam Lefflermust arrange for the queue to be processed 13682323455SDoug Rabson(for instance by scheduling a software interrupt or waking a kernel 13782323455SDoug Rabsonthread). 138cf82599dSSam LefflerThe memory location where the thread identity is recorded is used 139cf82599dSSam Lefflerto signal the service thread(s) to terminate--when this value is set to 140cf82599dSSam Lefflerzero and the thread is signaled it will terminate. 1412eb30874SOleksandr TymoshenkoIf the queue is intended for use in fast interrupt handlers 1422eb30874SOleksandr Tymoshenko.Fn taskqueue_create_fast 1432eb30874SOleksandr Tymoshenkoshould be used in place of 1442eb30874SOleksandr Tymoshenko.Fn taskqueue_create . 14582323455SDoug Rabson.Pp 14682323455SDoug RabsonThe function 14782323455SDoug Rabson.Fn taskqueue_free 148e477e4feSPawel Jakub Dawidekshould be used to free the memory used by the queue. 149cf82599dSSam LefflerAny tasks that are on the queue will be executed at this time after 150cf82599dSSam Lefflerwhich the thread servicing the queue will be signaled that it should exit. 15182323455SDoug Rabson.Pp 152fdbc7174SWill AndrewsOnce a taskqueue has been created, its threads should be started using 153ec5d37f4SBenjamin Kaduk.Fn taskqueue_start_threads 154ec5d37f4SBenjamin Kadukor 155ec5d37f4SBenjamin Kaduk.Fn taskqueue_start_threads_pinned . 156ec5d37f4SBenjamin Kaduk.Fn taskqueue_start_threads_pinned 157ec5d37f4SBenjamin Kaduktakes a 158ec5d37f4SBenjamin Kaduk.Va cpu_id 159ec5d37f4SBenjamin Kadukargument which will cause the threads which are started for the taskqueue 160ec5d37f4SBenjamin Kadukto be pinned to run on the given CPU. 161fdbc7174SWill AndrewsCallbacks may optionally be registered using 162fdbc7174SWill Andrews.Fn taskqueue_set_callback . 163fdbc7174SWill AndrewsCurrently, callbacks may be registered for the following purposes: 164fdbc7174SWill Andrews.Bl -tag -width TASKQUEUE_CALLBACK_TYPE_SHUTDOWN 165fdbc7174SWill Andrews.It Dv TASKQUEUE_CALLBACK_TYPE_INIT 166fdbc7174SWill AndrewsThis callback is called by every thread in the taskqueue, before it executes 167fdbc7174SWill Andrewsany tasks. 168fdbc7174SWill AndrewsThis callback must be set before the taskqueue's threads are started. 169fdbc7174SWill Andrews.It Dv TASKQUEUE_CALLBACK_TYPE_SHUTDOWN 170fdbc7174SWill AndrewsThis callback is called by every thread in the taskqueue, after it executes 171fdbc7174SWill Andrewsits last task. 172fdbc7174SWill AndrewsThis callback will always be called before the taskqueue structure is 173fdbc7174SWill Andrewsreclaimed. 174fdbc7174SWill Andrews.El 175fdbc7174SWill Andrews.Pp 17682323455SDoug RabsonTo add a task to the list of tasks queued on a taskqueue, call 17782323455SDoug Rabson.Fn taskqueue_enqueue 17882323455SDoug Rabsonwith pointers to the queue and task. 17982323455SDoug RabsonIf the task's 18082323455SDoug Rabson.Va ta_pending 18182323455SDoug Rabsonfield is non-zero, 18282323455SDoug Rabsonthen it is simply incremented to reflect the number of times the task 183d2849f27SAdrian Chaddwas enqueued, up to a cap of USHRT_MAX. 18482323455SDoug RabsonOtherwise, 18582323455SDoug Rabsonthe task is added to the list before the first task which has a lower 18682323455SDoug Rabson.Va ta_priority 18782323455SDoug Rabsonvalue or at the end of the list if no tasks have a lower priority. 18882323455SDoug RabsonEnqueueing a task does not perform any memory allocation which makes 18982323455SDoug Rabsonit suitable for calling from an interrupt handler. 19082323455SDoug RabsonThis function will return 191b92a189eSRuslan Ermilov.Er EPIPE 19282323455SDoug Rabsonif the queue is being freed. 19382323455SDoug Rabson.Pp 19482323455SDoug RabsonWhen a task is executed, 19582323455SDoug Rabsonfirst it is removed from the queue, 19682323455SDoug Rabsonthe value of 19782323455SDoug Rabson.Va ta_pending 19882323455SDoug Rabsonis recorded and then the field is zeroed. 19982323455SDoug RabsonThe function 20082323455SDoug Rabson.Va ta_func 20182323455SDoug Rabsonfrom the task structure is called with the value of the field 20282323455SDoug Rabson.Va ta_context 20382323455SDoug Rabsonas its first argument 20482323455SDoug Rabsonand the value of 20582323455SDoug Rabson.Va ta_pending 20682323455SDoug Rabsonas its second argument. 207f616cf33SJohn-Mark GurneyAfter the function 208f616cf33SJohn-Mark Gurney.Va ta_func 209f616cf33SJohn-Mark Gurneyreturns, 210f616cf33SJohn-Mark Gurney.Xr wakeup 9 211f616cf33SJohn-Mark Gurneyis called on the task pointer passed to 212f616cf33SJohn-Mark Gurney.Fn taskqueue_enqueue . 21382323455SDoug Rabson.Pp 214fff7ff71SGleb SmirnoffThe 215c3bd10b4SKonstantin Belousov.Fn taskqueue_enqueue_timeout 216*f37b7fc2SIan Leporefunction is used to schedule the enqueue after the specified number of 217c3bd10b4SKonstantin Belousov.Va ticks . 218*f37b7fc2SIan LeporeThe 219*f37b7fc2SIan Lepore.Fn taskqueue_enqueue_timeout_sbt 220*f37b7fc2SIan Leporefunction provides finer control over the scheduling based on 221*f37b7fc2SIan Lepore.Va sbt , 222*f37b7fc2SIan Lepore.Va pr , 223*f37b7fc2SIan Leporeand 224*f37b7fc2SIan Lepore.Va flags , 225*f37b7fc2SIan Leporeas detailed in 226*f37b7fc2SIan Lepore.Xr timeout 9 . 227c3bd10b4SKonstantin BelousovOnly non-fast task queues can be used for 228c3bd10b4SKonstantin Belousov.Va timeout_task 229c3bd10b4SKonstantin Belousovscheduling. 230471af3a8SKonstantin BelousovIf the 231471af3a8SKonstantin Belousov.Va ticks 232471af3a8SKonstantin Belousovargument is negative, the already scheduled enqueueing is not re-scheduled. 233c1e231bcSKonstantin BelousovOtherwise, the task is scheduled for enqueueing in the future, 234471af3a8SKonstantin Belousovafter the absolute value of 235471af3a8SKonstantin Belousov.Va ticks 236471af3a8SKonstantin Belousovis passed. 2376a3536aaSHans Petter SelaskyThis function returns -1 if the task is being drained. 2386a3536aaSHans Petter SelaskyOtherwise, the number of pending calls is returned. 239c3bd10b4SKonstantin Belousov.Pp 240c3bd10b4SKonstantin BelousovThe 241f46276a9SMatthew D Fleming.Fn taskqueue_cancel 242f46276a9SMatthew D Flemingfunction is used to cancel a task. 243f46276a9SMatthew D FlemingThe 244f46276a9SMatthew D Fleming.Va ta_pending 245f46276a9SMatthew D Flemingcount is cleared, and the old value returned in the reference 246f46276a9SMatthew D Flemingparameter 247f46276a9SMatthew D Fleming.Fa pendp , 24873bbeaa5SGlen Barberif it is 24973bbeaa5SGlen Barber.Pf non- Dv NULL . 250f46276a9SMatthew D FlemingIf the task is currently running, 251f46276a9SMatthew D Fleming.Dv EBUSY 252f46276a9SMatthew D Flemingis returned, otherwise 0. 253f46276a9SMatthew D FlemingTo implement a blocking 254f46276a9SMatthew D Fleming.Fn taskqueue_cancel 255f46276a9SMatthew D Flemingthat waits for a running task to finish, it could look like: 256f46276a9SMatthew D Fleming.Bd -literal -offset indent 257f46276a9SMatthew D Flemingwhile (taskqueue_cancel(tq, task, NULL) != 0) 258f46276a9SMatthew D Fleming taskqueue_drain(tq, task); 259f46276a9SMatthew D Fleming.Ed 260f46276a9SMatthew D Fleming.Pp 261f46276a9SMatthew D FlemingNote that, as with 262f46276a9SMatthew D Fleming.Fn taskqueue_drain , 263f46276a9SMatthew D Flemingthe caller is responsible for ensuring that the task is not re-enqueued 264f46276a9SMatthew D Flemingafter being canceled. 265f46276a9SMatthew D Fleming.Pp 266c3bd10b4SKonstantin BelousovSimilarly, the 267c3bd10b4SKonstantin Belousov.Fn taskqueue_cancel_timeout 268c3bd10b4SKonstantin Belousovfunction is used to cancel the scheduled task execution. 269c3bd10b4SKonstantin Belousov.Pp 270f46276a9SMatthew D FlemingThe 271fff7ff71SGleb Smirnoff.Fn taskqueue_drain 272c3bd10b4SKonstantin Belousovfunction is used to wait for the task to finish, and 273c3bd10b4SKonstantin Belousovthe 274c3bd10b4SKonstantin Belousov.Fn taskqueue_drain_timeout 275c3bd10b4SKonstantin Belousovfunction is used to wait for the scheduled task to finish. 276fff7ff71SGleb SmirnoffThere is no guarantee that the task will not be 277fff7ff71SGleb Smirnoffenqueued after call to 278fff7ff71SGleb Smirnoff.Fn taskqueue_drain . 279cb03508fSAndriy GaponIf the caller wants to put the task into a known state, 280cb03508fSAndriy Gaponthen before calling 281cb03508fSAndriy Gapon.Fn taskqueue_drain 282cb03508fSAndriy Gaponthe caller should use out-of-band means to ensure that the task 283cb03508fSAndriy Gaponwould not be enqueued. 284cb03508fSAndriy GaponFor example, if the task is enqueued by an interrupt filter, then 285cb03508fSAndriy Gaponthe interrupt could be disabled. 286cb03508fSAndriy Gapon.Pp 287cb03508fSAndriy GaponThe 288cb03508fSAndriy Gapon.Fn taskqueue_drain_all 289cb03508fSAndriy Gaponfunction is used to wait for all pending and running tasks that 290cb03508fSAndriy Gaponare enqueued on the taskqueue to finish. 2915b326a32SJustin T. GibbsTasks posted to the taskqueue after 292cb03508fSAndriy Gapon.Fn taskqueue_drain_all 2935b326a32SJustin T. Gibbsbegins processing, 2945b326a32SJustin T. Gibbsincluding pending enqueues scheduled by a previous call to 2955b326a32SJustin T. Gibbs.Fn taskqueue_enqueue_timeout , 2965b326a32SJustin T. Gibbsdo not extend the wait time of 2975b326a32SJustin T. Gibbs.Fn taskqueue_drain_all 2985b326a32SJustin T. Gibbsand may complete after 2995b326a32SJustin T. Gibbs.Fn taskqueue_drain_all 3005b326a32SJustin T. Gibbsreturns. 301cb03508fSAndriy Gapon.Pp 302cb03508fSAndriy GaponThe 303cb03508fSAndriy Gapon.Fn taskqueue_block 304cb03508fSAndriy Gaponfunction blocks the taskqueue. 305cb03508fSAndriy GaponIt prevents any enqueued but not running tasks from being executed. 306cb03508fSAndriy GaponFuture calls to 307cb03508fSAndriy Gapon.Fn taskqueue_enqueue 308cb03508fSAndriy Gaponwill enqueue tasks, but the tasks will not be run until 309cb03508fSAndriy Gapon.Fn taskqueue_unblock 310cb03508fSAndriy Gaponis called. 311cb03508fSAndriy GaponPlease note that 312cb03508fSAndriy Gapon.Fn taskqueue_block 313cb03508fSAndriy Gapondoes not wait for any currently running tasks to finish. 314cb03508fSAndriy GaponThus, the 315cb03508fSAndriy Gapon.Fn taskqueue_block 316cb03508fSAndriy Gapondoes not provide a guarantee that 317cb03508fSAndriy Gapon.Fn taskqueue_run 318cb03508fSAndriy Gaponis not running after 319cb03508fSAndriy Gapon.Fn taskqueue_block 320cb03508fSAndriy Gaponreturns, but it does provide a guarantee that 321cb03508fSAndriy Gapon.Fn taskqueue_run 322cb03508fSAndriy Gaponwill not be called again 323cb03508fSAndriy Gaponuntil 324cb03508fSAndriy Gapon.Fn taskqueue_unblock 325cb03508fSAndriy Gaponis called. 326cb03508fSAndriy GaponIf the caller requires a guarantee that 327cb03508fSAndriy Gapon.Fn taskqueue_run 328cb03508fSAndriy Gaponis not running, then this must be arranged by the caller. 329cb03508fSAndriy GaponNote that if 330cb03508fSAndriy Gapon.Fn taskqueue_drain 331cb03508fSAndriy Gaponis called on a task that is enqueued on a taskqueue that is blocked by 332cb03508fSAndriy Gapon.Fn taskqueue_block , 333cb03508fSAndriy Gaponthen 334cb03508fSAndriy Gapon.Fn taskqueue_drain 335cb03508fSAndriy Gaponcan not return until the taskqueue is unblocked. 336cb03508fSAndriy GaponThis can result in a deadlock if the thread blocked in 337cb03508fSAndriy Gapon.Fn taskqueue_drain 338cb03508fSAndriy Gaponis the thread that is supposed to call 339cb03508fSAndriy Gapon.Fn taskqueue_unblock . 340cb03508fSAndriy GaponThus, use of 341cb03508fSAndriy Gapon.Fn taskqueue_drain 342cb03508fSAndriy Gaponafter 343cb03508fSAndriy Gapon.Fn taskqueue_block 344cb03508fSAndriy Gaponis discouraged, because the state of the task can not be known in advance. 345cb03508fSAndriy GaponThe same caveat applies to 346cb03508fSAndriy Gapon.Fn taskqueue_drain_all . 347cb03508fSAndriy Gapon.Pp 348cb03508fSAndriy GaponThe 349cb03508fSAndriy Gapon.Fn taskqueue_unblock 350cb03508fSAndriy Gaponfunction unblocks the previously blocked taskqueue. 351cb03508fSAndriy GaponAll enqueued tasks can be run after this call. 352fff7ff71SGleb Smirnoff.Pp 353159ef108SPawel Jakub DawidekThe 354159ef108SPawel Jakub Dawidek.Fn taskqueue_member 355159ef108SPawel Jakub Dawidekfunction returns 356159ef108SPawel Jakub Dawidek.No 1 3579c1a8ce4SPawel Jakub Dawidekif the given thread 358159ef108SPawel Jakub Dawidek.Fa td 3599ba47352SJoel Dahlis part of the given taskqueue 360159ef108SPawel Jakub Dawidek.Fa queue 361159ef108SPawel Jakub Dawidekand 362159ef108SPawel Jakub Dawidek.No 0 363159ef108SPawel Jakub Dawidekotherwise. 364159ef108SPawel Jakub Dawidek.Pp 365a92f0ee8SMatthew D FlemingThe 366a92f0ee8SMatthew D Fleming.Fn taskqueue_run 367a92f0ee8SMatthew D Flemingfunction will run all pending tasks in the specified 368a92f0ee8SMatthew D Fleming.Fa queue . 369a92f0ee8SMatthew D FlemingNormally this function is only used internally. 370a92f0ee8SMatthew D Fleming.Pp 37182323455SDoug RabsonA convenience macro, 37282323455SDoug Rabson.Fn TASK_INIT "task" "priority" "func" "context" 37382323455SDoug Rabsonis provided to initialise a 37482323455SDoug Rabson.Va task 37582323455SDoug Rabsonstructure. 376a7f5f794SJohn BaldwinThe 377a7f5f794SJohn Baldwin.Fn TASK_INITIALIZER 378a7f5f794SJohn Baldwinmacro generates an initializer for a task structure. 379c3bd10b4SKonstantin BelousovA macro 380c3bd10b4SKonstantin Belousov.Fn TIMEOUT_TASK_INIT "queue" "timeout_task" "priority" "func" "context" 381a7f5f794SJohn Baldwininitializes the 382a7f5f794SJohn Baldwin.Va timeout_task 383a7f5f794SJohn Baldwinstructure. 38482323455SDoug RabsonThe values of 38582323455SDoug Rabson.Va priority , 38682323455SDoug Rabson.Va func , 38782323455SDoug Rabsonand 38882323455SDoug Rabson.Va context 38982323455SDoug Rabsonare simply copied into the task structure fields and the 39082323455SDoug Rabson.Va ta_pending 39182323455SDoug Rabsonfield is cleared. 39282323455SDoug Rabson.Pp 3932eb30874SOleksandr TymoshenkoFive macros 394227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DECLARE "name" , 395227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE "name" "enqueue" "context" "init" , 3962eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE "name" "enqueue" "context" "init" , 39782323455SDoug Rabsonand 398227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE_THREAD "name" 3992eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE_THREAD "name" 400227559d1SJohn-Mark Gurneyare used to declare a reference to a global queue, to define the 401c0854fb7SRuslan Ermilovimplementation of the queue, and declare a queue that uses its own thread. 40282323455SDoug RabsonThe 40382323455SDoug Rabson.Fn TASKQUEUE_DEFINE 40482323455SDoug Rabsonmacro arranges to call 40582323455SDoug Rabson.Fn taskqueue_create 40682323455SDoug Rabsonwith the values of its 40782323455SDoug Rabson.Va name , 40882323455SDoug Rabson.Va enqueue 40982323455SDoug Rabsonand 41082323455SDoug Rabson.Va context 41182323455SDoug Rabsonarguments during system initialisation. 41282323455SDoug RabsonAfter calling 41382323455SDoug Rabson.Fn taskqueue_create , 41482323455SDoug Rabsonthe 41582323455SDoug Rabson.Va init 41682323455SDoug Rabsonargument to the macro is executed as a C statement, 41782323455SDoug Rabsonallowing any further initialisation to be performed 4182e17a50fSBenjamin Kaduk(such as registering an interrupt handler, etc.). 41982323455SDoug Rabson.Pp 420227559d1SJohn-Mark GurneyThe 421227559d1SJohn-Mark Gurney.Fn TASKQUEUE_DEFINE_THREAD 422c0854fb7SRuslan Ermilovmacro defines a new taskqueue with its own kernel thread to serve tasks. 423c0854fb7SRuslan ErmilovThe variable 424227559d1SJohn-Mark Gurney.Vt struct taskqueue *taskqueue_name 425227559d1SJohn-Mark Gurneyis used to enqueue tasks onto the queue. 4262eb30874SOleksandr Tymoshenko.Pp 4272eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE 4282eb30874SOleksandr Tymoshenkoand 4292eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_FAST_DEFINE_THREAD 4302eb30874SOleksandr Tymoshenkoact just like 4312eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_DEFINE 4322eb30874SOleksandr Tymoshenkoand 4332eb30874SOleksandr Tymoshenko.Fn TASKQUEUE_DEFINE_THREAD 4342eb30874SOleksandr Tymoshenkorespectively but taskqueue is created with 4352eb30874SOleksandr Tymoshenko.Fn taskqueue_create_fast . 4364c49b002SJoseph Koshy.Ss Predefined Task Queues 4374c49b002SJoseph KoshyThe system provides four global taskqueues, 4384c49b002SJoseph Koshy.Va taskqueue_fast , 43982323455SDoug Rabson.Va taskqueue_swi , 440cb32189eSKenneth D. Merry.Va taskqueue_swi_giant , 441cb32189eSKenneth D. Merryand 442cb32189eSKenneth D. Merry.Va taskqueue_thread . 4434c49b002SJoseph KoshyThe 4444c49b002SJoseph Koshy.Va taskqueue_fast 4454c49b002SJoseph Koshyqueue is for swi handlers dispatched from fast interrupt handlers, 4464c49b002SJoseph Koshywhere sleep mutexes cannot be used. 447cb32189eSKenneth D. MerryThe swi taskqueues are run via a software interrupt mechanism. 4484c49b002SJoseph KoshyThe 4494c49b002SJoseph Koshy.Va taskqueue_swi 4504c49b002SJoseph Koshyqueue runs without the protection of the 4514c49b002SJoseph Koshy.Va Giant 4524c49b002SJoseph Koshykernel lock, and the 4534c49b002SJoseph Koshy.Va taskqueue_swi_giant 4544c49b002SJoseph Koshyqueue runs with the protection of the 4554c49b002SJoseph Koshy.Va Giant 456cb32189eSKenneth D. Merrykernel lock. 4574c49b002SJoseph KoshyThe thread taskqueue 4584c49b002SJoseph Koshy.Va taskqueue_thread 4594c49b002SJoseph Koshyruns in a kernel thread context, and tasks run from this thread do 4604c49b002SJoseph Koshynot run under the 4614c49b002SJoseph Koshy.Va Giant 4624c49b002SJoseph Koshykernel lock. 4634c49b002SJoseph KoshyIf the caller wants to run under 4644c49b002SJoseph Koshy.Va Giant , 4654c49b002SJoseph Koshyhe should explicitly acquire and release 4664c49b002SJoseph Koshy.Va Giant 4674c49b002SJoseph Koshyin his taskqueue handler routine. 468bf7f20c2SRuslan Ermilov.Pp 469cb32189eSKenneth D. MerryTo use these queues, 47082323455SDoug Rabsoncall 47182323455SDoug Rabson.Fn taskqueue_enqueue 472cb32189eSKenneth D. Merrywith the value of the global taskqueue variable for the queue you wish to 473cbc4d2dbSJohn Baldwinuse. 47482323455SDoug Rabson.Pp 475e0254f10SKenneth D. MerryThe software interrupt queues can be used, 47682323455SDoug Rabsonfor instance, for implementing interrupt handlers which must perform a 47782323455SDoug Rabsonsignificant amount of processing in the handler. 47882323455SDoug RabsonThe hardware interrupt handler would perform minimal processing of the 47982323455SDoug Rabsoninterrupt and then enqueue a task to finish the work. 48082323455SDoug RabsonThis reduces to a minimum 48182323455SDoug Rabsonthe amount of time spent with interrupts disabled. 482cb32189eSKenneth D. Merry.Pp 483cb32189eSKenneth D. MerryThe thread queue can be used, for instance, by interrupt level routines 484cb32189eSKenneth D. Merrythat need to call kernel functions that do things that can only be done 485cb32189eSKenneth D. Merryfrom a thread context. 486cb32189eSKenneth D. Merry(e.g., call malloc with the M_WAITOK flag.) 487cf82599dSSam Leffler.Pp 488cf82599dSSam LefflerNote that tasks queued on shared taskqueues such as 489cf82599dSSam Leffler.Va taskqueue_swi 490cf82599dSSam Lefflermay be delayed an indeterminate amount of time before execution. 491cf82599dSSam LefflerIf queueing delays cannot be tolerated then a private taskqueue should 492cf82599dSSam Lefflerbe created with a dedicated processing thread. 4933bbf58f9SJoseph Koshy.Sh SEE ALSO 4943bbf58f9SJoseph Koshy.Xr ithread 9 , 4953bbf58f9SJoseph Koshy.Xr kthread 9 , 4963bbf58f9SJoseph Koshy.Xr swi 9 497*f37b7fc2SIan Lepore.Xr timeout 9 49882323455SDoug Rabson.Sh HISTORY 49982323455SDoug RabsonThis interface first appeared in 50082323455SDoug Rabson.Fx 5.0 . 501c3bd10b4SKonstantin BelousovThere is a similar facility called work_queue in the Linux kernel. 50282323455SDoug Rabson.Sh AUTHORS 503571dba6eSHiten PandyaThis manual page was written by 50482323455SDoug Rabson.An Doug Rabson . 505