Print this page
9017 Introduce taskq_empty()
Reviewed by: Bryan Cantrill <bryan@joyent.com>
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Yuri Pankov <yuripv@yuripv.net>


   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
  27  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  28  * Copyright 2017 RackTop Systems.

  29  */
  30 
  31 #include <sys/taskq_impl.h>
  32 
  33 #include <sys/class.h>
  34 #include <sys/debug.h>
  35 #include <sys/ksynch.h>
  36 #include <sys/kmem.h>
  37 #include <sys/time.h>
  38 #include <sys/systm.h>
  39 #include <sys/sysmacros.h>
  40 #include <sys/unistd.h>
  41 
  42 /* avoid <sys/disp.h> */
  43 #define maxclsyspri     99
  44 
  45 /* avoid <unistd.h> */
  46 extern long sysconf(int);
  47 
  48 /* avoiding <thread.h> */


 195         /*
 196          * Enqueue the task to the underlying queue.
 197          */
 198         mutex_enter(&tq->tq_lock);
 199 
 200         if (flags & TQ_FRONT) {
 201                 t->tqent_next = tq->tq_task.tqent_next;
 202                 t->tqent_prev = &tq->tq_task;
 203         } else {
 204                 t->tqent_next = &tq->tq_task;
 205                 t->tqent_prev = tq->tq_task.tqent_prev;
 206         }
 207         t->tqent_next->tqent_prev = t;
 208         t->tqent_prev->tqent_next = t;
 209         t->tqent_func = func;
 210         t->tqent_arg = arg;
 211         cv_signal(&tq->tq_dispatch_cv);
 212         mutex_exit(&tq->tq_lock);
 213 }
 214 












 215 void
 216 taskq_wait(taskq_t *tq)
 217 {
 218         mutex_enter(&tq->tq_lock);
 219         while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
 220                 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
 221         mutex_exit(&tq->tq_lock);
 222 }
 223 
 224 static void *
 225 taskq_thread(void *arg)
 226 {
 227         taskq_t *tq = arg;
 228         taskq_ent_t *t;
 229         boolean_t prealloc;
 230 
 231         mutex_enter(&tq->tq_lock);
 232         while (tq->tq_flags & TASKQ_ACTIVE) {
 233                 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
 234                         if (--tq->tq_active == 0)




   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
  27  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  28  * Copyright 2017 RackTop Systems.
  29  * Copyright 2018, Joyent, Inc.
  30  */
  31 
  32 #include <sys/taskq_impl.h>
  33 
  34 #include <sys/class.h>
  35 #include <sys/debug.h>
  36 #include <sys/ksynch.h>
  37 #include <sys/kmem.h>
  38 #include <sys/time.h>
  39 #include <sys/systm.h>
  40 #include <sys/sysmacros.h>
  41 #include <sys/unistd.h>
  42 
  43 /* avoid <sys/disp.h> */
  44 #define maxclsyspri     99
  45 
  46 /* avoid <unistd.h> */
  47 extern long sysconf(int);
  48 
  49 /* avoiding <thread.h> */


 196         /*
 197          * Enqueue the task to the underlying queue.
 198          */
 199         mutex_enter(&tq->tq_lock);
 200 
 201         if (flags & TQ_FRONT) {
 202                 t->tqent_next = tq->tq_task.tqent_next;
 203                 t->tqent_prev = &tq->tq_task;
 204         } else {
 205                 t->tqent_next = &tq->tq_task;
 206                 t->tqent_prev = tq->tq_task.tqent_prev;
 207         }
 208         t->tqent_next->tqent_prev = t;
 209         t->tqent_prev->tqent_next = t;
 210         t->tqent_func = func;
 211         t->tqent_arg = arg;
 212         cv_signal(&tq->tq_dispatch_cv);
 213         mutex_exit(&tq->tq_lock);
 214 }
 215 
 216 boolean_t
 217 taskq_empty(taskq_t *tq)
 218 {
 219         boolean_t rv;
 220 
 221         mutex_enter(&tq->tq_lock);
 222         rv = (tq->tq_task.tqent_next == &tq->tq_task) && (tq->tq_active == 0);
 223         mutex_exit(&tq->tq_lock);
 224 
 225         return (rv);
 226 }
 227 
 228 void
 229 taskq_wait(taskq_t *tq)
 230 {
 231         mutex_enter(&tq->tq_lock);
 232         while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
 233                 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
 234         mutex_exit(&tq->tq_lock);
 235 }
 236 
 237 static void *
 238 taskq_thread(void *arg)
 239 {
 240         taskq_t *tq = arg;
 241         taskq_ent_t *t;
 242         boolean_t prealloc;
 243 
 244         mutex_enter(&tq->tq_lock);
 245         while (tq->tq_flags & TASKQ_ACTIVE) {
 246                 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
 247                         if (--tq->tq_active == 0)