xref: /freebsd/sys/contrib/openzfs/include/os/freebsd/spl/sys/taskq.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_TASKQ_H
28 #define	_SYS_TASKQ_H
29 
30 #ifdef _KERNEL
31 
32 #include <sys/types.h>
33 #include <sys/proc.h>
34 #include <sys/queue.h>
35 #include <sys/taskqueue.h>
36 #include <sys/thread.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 #define	TASKQ_NAMELEN	31
43 
44 typedef struct taskq {
45 	struct taskqueue	*tq_queue;
46 	int			tq_nthreads;
47 } taskq_t;
48 
49 typedef uintptr_t taskqid_t;
50 typedef void (task_func_t)(void *);
51 
52 typedef struct taskq_ent {
53 	union {
54 		struct task	 tqent_task;
55 		struct timeout_task tqent_timeout_task;
56 	};
57 	task_func_t	*tqent_func;
58 	void		*tqent_arg;
59 	taskqid_t	 tqent_id;
60 	LIST_ENTRY(taskq_ent) tqent_hash;
61 	uint_t		 tqent_type;
62 	volatile uint_t	 tqent_rc;
63 } taskq_ent_t;
64 
65 /*
66  * Public flags for taskq_create(): bit range 0-15
67  */
68 #define	TASKQ_PREPOPULATE	0x0001	/* Prepopulate with threads and data */
69 #define	TASKQ_CPR_SAFE		0x0002	/* Use CPR safe protocol */
70 #define	TASKQ_DYNAMIC		0x0004	/* Use dynamic thread scheduling */
71 #define	TASKQ_THREADS_CPU_PCT	0x0008	/* number of threads as % of ncpu */
72 #define	TASKQ_DC_BATCH		0x0010	/* Taskq uses SDC in batch mode */
73 
74 /*
75  * Flags for taskq_dispatch. TQ_SLEEP/TQ_NOSLEEP should be same as
76  * KM_SLEEP/KM_NOSLEEP.
77  */
78 #define	TQ_SLEEP	0x00	/* Can block for memory */
79 #define	TQ_NOSLEEP	0x01	/* cannot block for memory; may fail */
80 #define	TQ_NOQUEUE	0x02	/* Do not enqueue if can't dispatch */
81 #define	TQ_NOALLOC	0x04	/* cannot allocate memory; may fail */
82 #define	TQ_FRONT	0x08	/* Put task at the front of the queue */
83 
84 #define	TASKQID_INVALID		((taskqid_t)0)
85 
86 extern taskq_t *system_taskq;
87 /* Global dynamic task queue for long delay */
88 extern taskq_t *system_delay_taskq;
89 
90 extern taskqid_t taskq_dispatch(taskq_t *, task_func_t, void *, uint_t);
91 extern taskqid_t taskq_dispatch_delay(taskq_t *, task_func_t, void *,
92     uint_t, clock_t);
93 extern void taskq_dispatch_ent(taskq_t *, task_func_t, void *, uint_t,
94     taskq_ent_t *);
95 extern int taskq_empty_ent(taskq_ent_t *);
96 extern void taskq_init_ent(taskq_ent_t *);
97 taskq_t	*taskq_create(const char *, int, pri_t, int, int, uint_t);
98 taskq_t	*taskq_create_synced(const char *, int, pri_t, int, int, uint_t,
99     kthread_t ***);
100 taskq_t	*taskq_create_instance(const char *, int, int, pri_t, int, int, uint_t);
101 taskq_t	*taskq_create_proc(const char *, int, pri_t, int, int,
102     struct proc *, uint_t);
103 taskq_t	*taskq_create_sysdc(const char *, int, int, int,
104     struct proc *, uint_t, uint_t);
105 void	nulltask(void *);
106 extern void taskq_destroy(taskq_t *);
107 extern void taskq_wait_id(taskq_t *, taskqid_t);
108 extern void taskq_wait_outstanding(taskq_t *, taskqid_t);
109 extern void taskq_wait(taskq_t *);
110 extern int taskq_cancel_id(taskq_t *, taskqid_t);
111 extern int taskq_member(taskq_t *, kthread_t *);
112 extern taskq_t *taskq_of_curthread(void);
113 void	taskq_suspend(taskq_t *);
114 int	taskq_suspended(taskq_t *);
115 void	taskq_resume(taskq_t *);
116 
117 #ifdef	__cplusplus
118 }
119 #endif
120 
121 #endif /* _KERNEL */
122 
123 #ifdef _STANDALONE
124 typedef void taskq_t;
125 typedef int taskq_ent_t;
126 #define	taskq_init_ent(x)
127 #endif /* _STANDALONE */
128 
129 #endif	/* _SYS_TASKQ_H */
130