1 /*- 2 * Copyright (c) 2009 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/kernel.h> 29 #include <sys/kmem.h> 30 #include <sys/lock.h> 31 #include <sys/mutex.h> 32 #include <sys/queue.h> 33 #include <sys/taskqueue.h> 34 #include <sys/taskq.h> 35 36 #include <vm/uma.h> 37 38 static uma_zone_t taskq_zone; 39 40 taskq_t *system_taskq = NULL; 41 42 struct proc *system_proc; 43 44 static void 45 system_taskq_init(void *arg) 46 { 47 48 taskq_zone = uma_zcreate("taskq_zone", sizeof(taskq_ent_t), 49 NULL, NULL, NULL, NULL, 0, 0); 50 system_taskq = taskq_create("system_taskq", mp_ncpus, minclsyspri, 51 0, 0, 0); 52 } 53 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init, NULL); 54 55 static void 56 system_taskq_fini(void *arg) 57 { 58 59 taskq_destroy(system_taskq); 60 uma_zdestroy(taskq_zone); 61 } 62 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini, NULL); 63 64 static taskq_t * 65 taskq_create_impl(const char *name, int nthreads, pri_t pri, proc_t *proc, 66 uint_t flags) 67 { 68 taskq_t *tq; 69 70 if ((flags & TASKQ_THREADS_CPU_PCT) != 0) 71 nthreads = MAX((mp_ncpus * nthreads) / 100, 1); 72 73 tq = kmem_alloc(sizeof(*tq), KM_SLEEP); 74 tq->tq_queue = taskqueue_create(name, M_WAITOK, taskqueue_thread_enqueue, 75 &tq->tq_queue); 76 (void) taskqueue_start_threads_in_proc(&tq->tq_queue, nthreads, pri, 77 proc, "%s", name); 78 79 return ((taskq_t *)tq); 80 } 81 82 taskq_t * 83 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused, 84 int maxalloc __unused, uint_t flags) 85 { 86 return (taskq_create_impl(name, nthreads, pri, system_proc, flags)); 87 } 88 89 taskq_t * 90 taskq_create_proc(const char *name, int nthreads, pri_t pri, int minalloc, 91 int maxalloc, proc_t *proc, uint_t flags) 92 { 93 return (taskq_create_impl(name, nthreads, pri, proc, flags)); 94 } 95 96 void 97 taskq_destroy(taskq_t *tq) 98 { 99 100 taskqueue_free(tq->tq_queue); 101 kmem_free(tq, sizeof(*tq)); 102 } 103 104 int 105 taskq_member(taskq_t *tq, kthread_t *thread) 106 { 107 108 return (taskqueue_member(tq->tq_queue, thread)); 109 } 110 111 static void 112 taskq_run(void *arg, int pending __unused) 113 { 114 taskq_ent_t *task = arg; 115 116 task->tqent_func(task->tqent_arg); 117 118 uma_zfree(taskq_zone, task); 119 } 120 121 taskqid_t 122 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags) 123 { 124 taskq_ent_t *task; 125 int mflag, prio; 126 127 if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP) 128 mflag = M_WAITOK; 129 else 130 mflag = M_NOWAIT; 131 /* 132 * If TQ_FRONT is given, we want higher priority for this task, so it 133 * can go at the front of the queue. 134 */ 135 prio = !!(flags & TQ_FRONT); 136 137 task = uma_zalloc(taskq_zone, mflag); 138 if (task == NULL) 139 return (0); 140 141 task->tqent_func = func; 142 task->tqent_arg = arg; 143 144 TASK_INIT(&task->tqent_task, prio, taskq_run, task); 145 taskqueue_enqueue(tq->tq_queue, &task->tqent_task); 146 147 return ((taskqid_t)(void *)task); 148 } 149 150 static void 151 taskq_run_ent(void *arg, int pending __unused) 152 { 153 taskq_ent_t *task = arg; 154 155 task->tqent_func(task->tqent_arg); 156 } 157 158 void 159 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, u_int flags, 160 taskq_ent_t *task) 161 { 162 int prio; 163 164 /* 165 * If TQ_FRONT is given, we want higher priority for this task, so it 166 * can go at the front of the queue. 167 */ 168 prio = !!(flags & TQ_FRONT); 169 170 task->tqent_func = func; 171 task->tqent_arg = arg; 172 173 TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task); 174 taskqueue_enqueue(tq->tq_queue, &task->tqent_task); 175 } 176 177 void 178 taskq_wait(taskq_t *tq) 179 { 180 taskqueue_quiesce(tq->tq_queue); 181 } 182 183 void 184 taskq_wait_id(taskq_t *tq, taskqid_t id) 185 { 186 taskqueue_drain_all(tq->tq_queue); 187 } 188