Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: threadpool.h,v 1.7 2020/04/25 07:23:21 mlelstv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef	_SYS_THREADPOOL_H_
     33 #define	_SYS_THREADPOOL_H_
     34 
     35 #if !defined(_KERNEL)
     36 #error "not supposed to be exposed to userland"
     37 #endif
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/mutex.h>
     42 #include <sys/condvar.h>
     43 #include <sys/queue.h>
     44 
     45 struct threadpool;
     46 struct threadpool_job;
     47 struct threadpool_percpu;
     48 struct threadpool_thread;
     49 
     50 typedef void threadpool_job_fn_t(struct threadpool_job *);
     51 
     52 struct threadpool_job {
     53 	kmutex_t			*job_lock;
     54 	struct threadpool_thread	*job_thread;
     55 	TAILQ_ENTRY(threadpool_job)	job_entry;
     56 	volatile unsigned int		job_refcnt;
     57 	kcondvar_t			job_cv;
     58 	threadpool_job_fn_t		*job_fn;
     59 	char				job_name[MAXCOMLEN];
     60 };
     61 
     62 void	threadpools_init(void);
     63 
     64 int	threadpool_get(struct threadpool **, pri_t);
     65 void	threadpool_put(struct threadpool *, pri_t);
     66 
     67 int	threadpool_percpu_get(struct threadpool_percpu **, pri_t);
     68 void	threadpool_percpu_put(struct threadpool_percpu *, pri_t);
     69 struct threadpool *
     70 	threadpool_percpu_ref(struct threadpool_percpu *);
     71 struct threadpool *
     72 	threadpool_percpu_ref_remote(struct threadpool_percpu *,
     73 	    struct cpu_info *);
     74 
     75 void	threadpool_job_init(struct threadpool_job *, threadpool_job_fn_t,
     76 	    kmutex_t *, const char *, ...) __printflike(4,5);
     77 void	threadpool_job_destroy(struct threadpool_job *);
     78 void	threadpool_job_done(struct threadpool_job *);
     79 
     80 void	threadpool_schedule_job(struct threadpool *, struct threadpool_job *);
     81 void	threadpool_cancel_job(struct threadpool *, struct threadpool_job *);
     82 bool	threadpool_cancel_job_async(struct threadpool *,
     83 	    struct threadpool_job *);
     84 
     85 #endif	/* _SYS_THREADPOOL_H_ */
     86