xref: /freebsd/sys/contrib/openzfs/module/zfs/zthr.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
7eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
8eda14cbcSMatt Macy  * 1.0 of the CDDL.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
11eda14cbcSMatt Macy  * source. A copy of the CDDL is also available via the Internet at
12eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * CDDL HEADER END
15eda14cbcSMatt Macy  */
16eda14cbcSMatt Macy 
17eda14cbcSMatt Macy /*
18eda14cbcSMatt Macy  * Copyright (c) 2017, 2020 by Delphix. All rights reserved.
19eda14cbcSMatt Macy  */
20eda14cbcSMatt Macy 
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * ZTHR Infrastructure
23eda14cbcSMatt Macy  * ===================
24eda14cbcSMatt Macy  *
25eda14cbcSMatt Macy  * ZTHR threads are used for isolated operations that span multiple txgs
26eda14cbcSMatt Macy  * within a SPA. They generally exist from SPA creation/loading and until
27eda14cbcSMatt Macy  * the SPA is exported/destroyed. The ideal requirements for an operation
28eda14cbcSMatt Macy  * to be modeled with a zthr are the following:
29eda14cbcSMatt Macy  *
30eda14cbcSMatt Macy  * 1] The operation needs to run over multiple txgs.
31eda14cbcSMatt Macy  * 2] There is be a single point of reference in memory or on disk that
32eda14cbcSMatt Macy  *    indicates whether the operation should run/is running or has
33eda14cbcSMatt Macy  *    stopped.
34eda14cbcSMatt Macy  *
35eda14cbcSMatt Macy  * If the operation satisfies the above then the following rules guarantee
36eda14cbcSMatt Macy  * a certain level of correctness:
37eda14cbcSMatt Macy  *
38eda14cbcSMatt Macy  * 1] Any thread EXCEPT the zthr changes the work indicator from stopped
39eda14cbcSMatt Macy  *    to running but not the opposite.
40eda14cbcSMatt Macy  * 2] Only the zthr can change the work indicator from running to stopped
41eda14cbcSMatt Macy  *    (e.g. when it is done) but not the opposite.
42eda14cbcSMatt Macy  *
43eda14cbcSMatt Macy  * This way a normal zthr cycle should go like this:
44eda14cbcSMatt Macy  *
45eda14cbcSMatt Macy  * 1] An external thread changes the work indicator from stopped to
46eda14cbcSMatt Macy  *    running and wakes up the zthr.
47eda14cbcSMatt Macy  * 2] The zthr wakes up, checks the indicator and starts working.
48eda14cbcSMatt Macy  * 3] When the zthr is done, it changes the indicator to stopped, allowing
49eda14cbcSMatt Macy  *    a new cycle to start.
50eda14cbcSMatt Macy  *
51eda14cbcSMatt Macy  * Besides being awakened by other threads, a zthr can be configured
52eda14cbcSMatt Macy  * during creation to wakeup on its own after a specified interval
53eda14cbcSMatt Macy  * [see zthr_create_timer()].
54eda14cbcSMatt Macy  *
55eda14cbcSMatt Macy  * Note: ZTHR threads are NOT a replacement for generic threads! Please
56eda14cbcSMatt Macy  * ensure that they fit your use-case well before using them.
57eda14cbcSMatt Macy  *
58eda14cbcSMatt Macy  * == ZTHR creation
59eda14cbcSMatt Macy  *
602c48331dSMatt Macy  * Every zthr needs four inputs to start running:
61eda14cbcSMatt Macy  *
62eda14cbcSMatt Macy  * 1] A user-defined checker function (checkfunc) that decides whether
63eda14cbcSMatt Macy  *    the zthr should start working or go to sleep. The function should
64eda14cbcSMatt Macy  *    return TRUE when the zthr needs to work or FALSE to let it sleep,
65eda14cbcSMatt Macy  *    and should adhere to the following signature:
66eda14cbcSMatt Macy  *    boolean_t checkfunc_name(void *args, zthr_t *t);
67eda14cbcSMatt Macy  *
68eda14cbcSMatt Macy  * 2] A user-defined ZTHR function (func) which the zthr executes when
69eda14cbcSMatt Macy  *    it is not sleeping. The function should adhere to the following
70eda14cbcSMatt Macy  *    signature type:
71eda14cbcSMatt Macy  *    void func_name(void *args, zthr_t *t);
72eda14cbcSMatt Macy  *
73eda14cbcSMatt Macy  * 3] A void args pointer that will be passed to checkfunc and func
74eda14cbcSMatt Macy  *    implicitly by the infrastructure.
75eda14cbcSMatt Macy  *
762c48331dSMatt Macy  * 4] A name for the thread. This string must be valid for the lifetime
772c48331dSMatt Macy  *    of the zthr.
782c48331dSMatt Macy  *
79eda14cbcSMatt Macy  * The reason why the above API needs two different functions,
80eda14cbcSMatt Macy  * instead of one that both checks and does the work, has to do with
81eda14cbcSMatt Macy  * the zthr's internal state lock (zthr_state_lock) and the allowed
82eda14cbcSMatt Macy  * cancellation windows. We want to hold the zthr_state_lock while
83eda14cbcSMatt Macy  * running checkfunc but not while running func. This way the zthr
84eda14cbcSMatt Macy  * can be cancelled while doing work and not while checking for work.
85eda14cbcSMatt Macy  *
86eda14cbcSMatt Macy  * To start a zthr:
872faf504dSMartin Matuska  *     zthr_t *zthr_pointer = zthr_create(checkfunc, func, args,
882faf504dSMartin Matuska  *         pri);
89eda14cbcSMatt Macy  * or
90eda14cbcSMatt Macy  *     zthr_t *zthr_pointer = zthr_create_timer(checkfunc, func,
912faf504dSMartin Matuska  *         args, max_sleep, pri);
92eda14cbcSMatt Macy  *
93eda14cbcSMatt Macy  * After that you should be able to wakeup, cancel, and resume the
94eda14cbcSMatt Macy  * zthr from another thread using the zthr_pointer.
95eda14cbcSMatt Macy  *
96eda14cbcSMatt Macy  * NOTE: ZTHR threads could potentially wake up spuriously and the
97eda14cbcSMatt Macy  * user should take this into account when writing a checkfunc.
98eda14cbcSMatt Macy  * [see ZTHR state transitions]
99eda14cbcSMatt Macy  *
100eda14cbcSMatt Macy  * == ZTHR wakeup
101eda14cbcSMatt Macy  *
102eda14cbcSMatt Macy  * ZTHR wakeup should be used when new work is added for the zthr. The
103eda14cbcSMatt Macy  * sleeping zthr will wakeup, see that it has more work to complete
104eda14cbcSMatt Macy  * and proceed. This can be invoked from open or syncing context.
105eda14cbcSMatt Macy  *
106eda14cbcSMatt Macy  * To wakeup a zthr:
107eda14cbcSMatt Macy  *     zthr_wakeup(zthr_t *t)
108eda14cbcSMatt Macy  *
109eda14cbcSMatt Macy  * == ZTHR cancellation and resumption
110eda14cbcSMatt Macy  *
111eda14cbcSMatt Macy  * ZTHR threads must be cancelled when their SPA is being exported
112eda14cbcSMatt Macy  * or when they need to be paused so they don't interfere with other
113eda14cbcSMatt Macy  * operations.
114eda14cbcSMatt Macy  *
115eda14cbcSMatt Macy  * To cancel a zthr:
116eda14cbcSMatt Macy  *     zthr_cancel(zthr_pointer);
117eda14cbcSMatt Macy  *
118eda14cbcSMatt Macy  * To resume it:
119eda14cbcSMatt Macy  *     zthr_resume(zthr_pointer);
120eda14cbcSMatt Macy  *
121eda14cbcSMatt Macy  * ZTHR cancel and resume should be invoked in open context during the
122eda14cbcSMatt Macy  * lifecycle of the pool as it is imported, exported or destroyed.
123eda14cbcSMatt Macy  *
124eda14cbcSMatt Macy  * A zthr will implicitly check if it has received a cancellation
125eda14cbcSMatt Macy  * signal every time func returns and every time it wakes up [see
126eda14cbcSMatt Macy  * ZTHR state transitions below].
127eda14cbcSMatt Macy  *
128eda14cbcSMatt Macy  * At times, waiting for the zthr's func to finish its job may take
129eda14cbcSMatt Macy  * time. This may be very time-consuming for some operations that
130eda14cbcSMatt Macy  * need to cancel the SPA's zthrs (e.g spa_export). For this scenario
131eda14cbcSMatt Macy  * the user can explicitly make their ZTHR function aware of incoming
132eda14cbcSMatt Macy  * cancellation signals using zthr_iscancelled(). A common pattern for
133eda14cbcSMatt Macy  * that looks like this:
134eda14cbcSMatt Macy  *
135eda14cbcSMatt Macy  * int
136eda14cbcSMatt Macy  * func_name(void *args, zthr_t *t)
137eda14cbcSMatt Macy  * {
138eda14cbcSMatt Macy  *     ... <unpack args> ...
139eda14cbcSMatt Macy  *     while (!work_done && !zthr_iscancelled(t)) {
140eda14cbcSMatt Macy  *         ... <do more work> ...
141eda14cbcSMatt Macy  *     }
142eda14cbcSMatt Macy  * }
143eda14cbcSMatt Macy  *
144eda14cbcSMatt Macy  * == ZTHR cleanup
145eda14cbcSMatt Macy  *
146eda14cbcSMatt Macy  * Cancelling a zthr doesn't clean up its metadata (internal locks,
147eda14cbcSMatt Macy  * function pointers to func and checkfunc, etc..). This is because
148eda14cbcSMatt Macy  * we want to keep them around in case we want to resume the execution
149eda14cbcSMatt Macy  * of the zthr later. Similarly for zthrs that exit themselves.
150eda14cbcSMatt Macy  *
151eda14cbcSMatt Macy  * To completely cleanup a zthr, cancel it first to ensure that it
152eda14cbcSMatt Macy  * is not running and then use zthr_destroy().
153eda14cbcSMatt Macy  *
154eda14cbcSMatt Macy  * == ZTHR state transitions
155eda14cbcSMatt Macy  *
156eda14cbcSMatt Macy  *    zthr creation
157eda14cbcSMatt Macy  *      +
158eda14cbcSMatt Macy  *      |
159eda14cbcSMatt Macy  *      |      woke up
160eda14cbcSMatt Macy  *      |   +--------------+ sleep
161eda14cbcSMatt Macy  *      |   |                  ^
162eda14cbcSMatt Macy  *      |   |                  |
163eda14cbcSMatt Macy  *      |   |                  | FALSE
164eda14cbcSMatt Macy  *      |   |                  |
165eda14cbcSMatt Macy  *      v   v     FALSE        +
166eda14cbcSMatt Macy  *   cancelled? +---------> checkfunc?
167eda14cbcSMatt Macy  *      +   ^                  +
168eda14cbcSMatt Macy  *      |   |                  |
169eda14cbcSMatt Macy  *      |   |                  | TRUE
170eda14cbcSMatt Macy  *      |   |                  |
171eda14cbcSMatt Macy  *      |   |  func returned   v
172eda14cbcSMatt Macy  *      |   +---------------+ func
173eda14cbcSMatt Macy  *      |
174eda14cbcSMatt Macy  *      | TRUE
175eda14cbcSMatt Macy  *      |
176eda14cbcSMatt Macy  *      v
177eda14cbcSMatt Macy  *   zthr stopped running
178eda14cbcSMatt Macy  *
179eda14cbcSMatt Macy  * == Implementation of ZTHR requests
180eda14cbcSMatt Macy  *
181eda14cbcSMatt Macy  * ZTHR cancel and resume are requests on a zthr to change its
182eda14cbcSMatt Macy  * internal state. These requests are serialized using the
183eda14cbcSMatt Macy  * zthr_request_lock, while changes in its internal state are
184eda14cbcSMatt Macy  * protected by the zthr_state_lock. A request will first acquire
185eda14cbcSMatt Macy  * the zthr_request_lock and then immediately acquire the
186eda14cbcSMatt Macy  * zthr_state_lock. We do this so that incoming requests are
187eda14cbcSMatt Macy  * serialized using the request lock, while still allowing us
188eda14cbcSMatt Macy  * to use the state lock for thread communication via zthr_cv.
189eda14cbcSMatt Macy  *
190eda14cbcSMatt Macy  * ZTHR wakeup broadcasts to zthr_cv, causing sleeping threads
191eda14cbcSMatt Macy  * to wakeup. It acquires the zthr_state_lock but not the
192eda14cbcSMatt Macy  * zthr_request_lock, so that a wakeup on a zthr in the middle
193eda14cbcSMatt Macy  * of being cancelled will not block.
194eda14cbcSMatt Macy  */
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy #include <sys/zfs_context.h>
197eda14cbcSMatt Macy #include <sys/zthr.h>
198eda14cbcSMatt Macy 
199eda14cbcSMatt Macy struct zthr {
200eda14cbcSMatt Macy 	/* running thread doing the work */
201eda14cbcSMatt Macy 	kthread_t	*zthr_thread;
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 	/* lock protecting internal data & invariants */
204eda14cbcSMatt Macy 	kmutex_t	zthr_state_lock;
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy 	/* mutex that serializes external requests */
207eda14cbcSMatt Macy 	kmutex_t	zthr_request_lock;
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy 	/* notification mechanism for requests */
210eda14cbcSMatt Macy 	kcondvar_t	zthr_cv;
211eda14cbcSMatt Macy 
212eda14cbcSMatt Macy 	/* flag set to true if we are canceling the zthr */
213eda14cbcSMatt Macy 	boolean_t	zthr_cancel;
214eda14cbcSMatt Macy 
215eda14cbcSMatt Macy 	/* flag set to true if we are waiting for the zthr to finish */
216eda14cbcSMatt Macy 	boolean_t	zthr_haswaiters;
217eda14cbcSMatt Macy 	kcondvar_t	zthr_wait_cv;
218eda14cbcSMatt Macy 	/*
219eda14cbcSMatt Macy 	 * maximum amount of time that the zthr is spent sleeping;
220eda14cbcSMatt Macy 	 * if this is 0, the thread doesn't wake up until it gets
221eda14cbcSMatt Macy 	 * signaled.
222eda14cbcSMatt Macy 	 */
223eda14cbcSMatt Macy 	hrtime_t	zthr_sleep_timeout;
224eda14cbcSMatt Macy 
2252faf504dSMartin Matuska 	/* Thread priority */
2262faf504dSMartin Matuska 	pri_t		zthr_pri;
2272faf504dSMartin Matuska 
228eda14cbcSMatt Macy 	/* consumer-provided callbacks & data */
229eda14cbcSMatt Macy 	zthr_checkfunc_t	*zthr_checkfunc;
230eda14cbcSMatt Macy 	zthr_func_t	*zthr_func;
231eda14cbcSMatt Macy 	void		*zthr_arg;
2322c48331dSMatt Macy 	const char	*zthr_name;
233eda14cbcSMatt Macy };
234eda14cbcSMatt Macy 
235da5137abSMartin Matuska static __attribute__((noreturn)) void
zthr_procedure(void * arg)236eda14cbcSMatt Macy zthr_procedure(void *arg)
237eda14cbcSMatt Macy {
238eda14cbcSMatt Macy 	zthr_t *t = arg;
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
241eda14cbcSMatt Macy 	ASSERT3P(t->zthr_thread, ==, curthread);
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy 	while (!t->zthr_cancel) {
244eda14cbcSMatt Macy 		if (t->zthr_checkfunc(t->zthr_arg, t)) {
245eda14cbcSMatt Macy 			mutex_exit(&t->zthr_state_lock);
246eda14cbcSMatt Macy 			t->zthr_func(t->zthr_arg, t);
247eda14cbcSMatt Macy 			mutex_enter(&t->zthr_state_lock);
248eda14cbcSMatt Macy 		} else {
249eda14cbcSMatt Macy 			if (t->zthr_sleep_timeout == 0) {
2502c48331dSMatt Macy 				cv_wait_idle(&t->zthr_cv, &t->zthr_state_lock);
251eda14cbcSMatt Macy 			} else {
2522c48331dSMatt Macy 				(void) cv_timedwait_idle_hires(&t->zthr_cv,
253eda14cbcSMatt Macy 				    &t->zthr_state_lock, t->zthr_sleep_timeout,
254eda14cbcSMatt Macy 				    MSEC2NSEC(1), 0);
255eda14cbcSMatt Macy 			}
256eda14cbcSMatt Macy 		}
257eda14cbcSMatt Macy 		if (t->zthr_haswaiters) {
258eda14cbcSMatt Macy 			t->zthr_haswaiters = B_FALSE;
259eda14cbcSMatt Macy 			cv_broadcast(&t->zthr_wait_cv);
260eda14cbcSMatt Macy 		}
261eda14cbcSMatt Macy 	}
262eda14cbcSMatt Macy 
263eda14cbcSMatt Macy 	/*
264eda14cbcSMatt Macy 	 * Clear out the kernel thread metadata and notify the
265eda14cbcSMatt Macy 	 * zthr_cancel() thread that we've stopped running.
266eda14cbcSMatt Macy 	 */
267eda14cbcSMatt Macy 	t->zthr_thread = NULL;
268eda14cbcSMatt Macy 	t->zthr_cancel = B_FALSE;
269eda14cbcSMatt Macy 	cv_broadcast(&t->zthr_cv);
270eda14cbcSMatt Macy 
271eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
272eda14cbcSMatt Macy 	thread_exit();
273eda14cbcSMatt Macy }
274eda14cbcSMatt Macy 
275eda14cbcSMatt Macy zthr_t *
zthr_create(const char * zthr_name,zthr_checkfunc_t * checkfunc,zthr_func_t * func,void * arg,pri_t pri)276eda14cbcSMatt Macy zthr_create(const char *zthr_name, zthr_checkfunc_t *checkfunc,
2772faf504dSMartin Matuska     zthr_func_t *func, void *arg, pri_t pri)
278eda14cbcSMatt Macy {
279eda14cbcSMatt Macy 	return (zthr_create_timer(zthr_name, checkfunc,
2802faf504dSMartin Matuska 	    func, arg, (hrtime_t)0, pri));
281eda14cbcSMatt Macy }
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy /*
284eda14cbcSMatt Macy  * Create a zthr with specified maximum sleep time.  If the time
285eda14cbcSMatt Macy  * in sleeping state exceeds max_sleep, a wakeup(do the check and
286eda14cbcSMatt Macy  * start working if required) will be triggered.
287eda14cbcSMatt Macy  */
288eda14cbcSMatt Macy zthr_t *
zthr_create_timer(const char * zthr_name,zthr_checkfunc_t * checkfunc,zthr_func_t * func,void * arg,hrtime_t max_sleep,pri_t pri)289eda14cbcSMatt Macy zthr_create_timer(const char *zthr_name, zthr_checkfunc_t *checkfunc,
2902faf504dSMartin Matuska     zthr_func_t *func, void *arg, hrtime_t max_sleep, pri_t pri)
291eda14cbcSMatt Macy {
292eda14cbcSMatt Macy 	zthr_t *t = kmem_zalloc(sizeof (*t), KM_SLEEP);
293eda14cbcSMatt Macy 	mutex_init(&t->zthr_state_lock, NULL, MUTEX_DEFAULT, NULL);
294eda14cbcSMatt Macy 	mutex_init(&t->zthr_request_lock, NULL, MUTEX_DEFAULT, NULL);
295eda14cbcSMatt Macy 	cv_init(&t->zthr_cv, NULL, CV_DEFAULT, NULL);
296eda14cbcSMatt Macy 	cv_init(&t->zthr_wait_cv, NULL, CV_DEFAULT, NULL);
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
299eda14cbcSMatt Macy 	t->zthr_checkfunc = checkfunc;
300eda14cbcSMatt Macy 	t->zthr_func = func;
301eda14cbcSMatt Macy 	t->zthr_arg = arg;
302eda14cbcSMatt Macy 	t->zthr_sleep_timeout = max_sleep;
3032c48331dSMatt Macy 	t->zthr_name = zthr_name;
3042faf504dSMartin Matuska 	t->zthr_pri = pri;
305eda14cbcSMatt Macy 
306eda14cbcSMatt Macy 	t->zthr_thread = thread_create_named(zthr_name, NULL, 0,
3072faf504dSMartin Matuska 	    zthr_procedure, t, 0, &p0, TS_RUN, pri);
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy 	return (t);
312eda14cbcSMatt Macy }
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy void
zthr_destroy(zthr_t * t)315eda14cbcSMatt Macy zthr_destroy(zthr_t *t)
316eda14cbcSMatt Macy {
317eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&t->zthr_state_lock));
318eda14cbcSMatt Macy 	ASSERT(!MUTEX_HELD(&t->zthr_request_lock));
319eda14cbcSMatt Macy 	VERIFY3P(t->zthr_thread, ==, NULL);
320eda14cbcSMatt Macy 	mutex_destroy(&t->zthr_request_lock);
321eda14cbcSMatt Macy 	mutex_destroy(&t->zthr_state_lock);
322eda14cbcSMatt Macy 	cv_destroy(&t->zthr_cv);
323eda14cbcSMatt Macy 	cv_destroy(&t->zthr_wait_cv);
324eda14cbcSMatt Macy 	kmem_free(t, sizeof (*t));
325eda14cbcSMatt Macy }
326eda14cbcSMatt Macy 
327eda14cbcSMatt Macy /*
328eda14cbcSMatt Macy  * Wake up the zthr if it is sleeping. If the thread has been cancelled
329eda14cbcSMatt Macy  * or is in the process of being cancelled, this is a no-op.
330eda14cbcSMatt Macy  */
331eda14cbcSMatt Macy void
zthr_wakeup(zthr_t * t)332eda14cbcSMatt Macy zthr_wakeup(zthr_t *t)
333eda14cbcSMatt Macy {
334eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
335eda14cbcSMatt Macy 
336eda14cbcSMatt Macy 	/*
337eda14cbcSMatt Macy 	 * There are 5 states that we can find the zthr when issuing
338eda14cbcSMatt Macy 	 * this broadcast:
339eda14cbcSMatt Macy 	 *
340eda14cbcSMatt Macy 	 * [1] The common case of the thread being asleep, at which
341eda14cbcSMatt Macy 	 *     point the broadcast will wake it up.
342eda14cbcSMatt Macy 	 * [2] The thread has been cancelled. Waking up a cancelled
343eda14cbcSMatt Macy 	 *     thread is a no-op. Any work that is still left to be
344eda14cbcSMatt Macy 	 *     done should be handled the next time the thread is
345eda14cbcSMatt Macy 	 *     resumed.
346eda14cbcSMatt Macy 	 * [3] The thread is doing work and is already up, so this
347eda14cbcSMatt Macy 	 *     is basically a no-op.
348eda14cbcSMatt Macy 	 * [4] The thread was just created/resumed, in which case the
349eda14cbcSMatt Macy 	 *     behavior is similar to [3].
350eda14cbcSMatt Macy 	 * [5] The thread is in the middle of being cancelled, which
351eda14cbcSMatt Macy 	 *     will be a no-op.
352eda14cbcSMatt Macy 	 */
353eda14cbcSMatt Macy 	cv_broadcast(&t->zthr_cv);
354eda14cbcSMatt Macy 
355eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
356eda14cbcSMatt Macy }
357eda14cbcSMatt Macy 
358eda14cbcSMatt Macy /*
359eda14cbcSMatt Macy  * Sends a cancel request to the zthr and blocks until the zthr is
360eda14cbcSMatt Macy  * cancelled. If the zthr is not running (e.g. has been cancelled
361eda14cbcSMatt Macy  * already), this is a no-op. Note that this function should not be
362eda14cbcSMatt Macy  * called from syncing context as it could deadlock with the zthr_func.
363eda14cbcSMatt Macy  */
364eda14cbcSMatt Macy void
zthr_cancel(zthr_t * t)365eda14cbcSMatt Macy zthr_cancel(zthr_t *t)
366eda14cbcSMatt Macy {
367eda14cbcSMatt Macy 	mutex_enter(&t->zthr_request_lock);
368eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy 	/*
371eda14cbcSMatt Macy 	 * Since we are holding the zthr_state_lock at this point
372eda14cbcSMatt Macy 	 * we can find the state in one of the following 4 states:
373eda14cbcSMatt Macy 	 *
374eda14cbcSMatt Macy 	 * [1] The thread has already been cancelled, therefore
375eda14cbcSMatt Macy 	 *     there is nothing for us to do.
376eda14cbcSMatt Macy 	 * [2] The thread is sleeping so we set the flag, broadcast
377eda14cbcSMatt Macy 	 *     the CV and wait for it to exit.
378eda14cbcSMatt Macy 	 * [3] The thread is doing work, in which case we just set
379eda14cbcSMatt Macy 	 *     the flag and wait for it to finish.
380eda14cbcSMatt Macy 	 * [4] The thread was just created/resumed, in which case
381eda14cbcSMatt Macy 	 *     the behavior is similar to [3].
382eda14cbcSMatt Macy 	 *
383eda14cbcSMatt Macy 	 * Since requests are serialized, by the time that we get
384eda14cbcSMatt Macy 	 * control back we expect that the zthr is cancelled and
385eda14cbcSMatt Macy 	 * not running anymore.
386eda14cbcSMatt Macy 	 */
387eda14cbcSMatt Macy 	if (t->zthr_thread != NULL) {
388eda14cbcSMatt Macy 		t->zthr_cancel = B_TRUE;
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy 		/* broadcast in case the zthr is sleeping */
391eda14cbcSMatt Macy 		cv_broadcast(&t->zthr_cv);
392eda14cbcSMatt Macy 
393eda14cbcSMatt Macy 		while (t->zthr_thread != NULL)
394eda14cbcSMatt Macy 			cv_wait(&t->zthr_cv, &t->zthr_state_lock);
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 		ASSERT(!t->zthr_cancel);
397eda14cbcSMatt Macy 	}
398eda14cbcSMatt Macy 
399eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
400eda14cbcSMatt Macy 	mutex_exit(&t->zthr_request_lock);
401eda14cbcSMatt Macy }
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy /*
404eda14cbcSMatt Macy  * Sends a resume request to the supplied zthr. If the zthr is already
405eda14cbcSMatt Macy  * running this is a no-op. Note that this function should not be
406eda14cbcSMatt Macy  * called from syncing context as it could deadlock with the zthr_func.
407eda14cbcSMatt Macy  */
408eda14cbcSMatt Macy void
zthr_resume(zthr_t * t)409eda14cbcSMatt Macy zthr_resume(zthr_t *t)
410eda14cbcSMatt Macy {
411eda14cbcSMatt Macy 	mutex_enter(&t->zthr_request_lock);
412eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy 	ASSERT3P(&t->zthr_checkfunc, !=, NULL);
415eda14cbcSMatt Macy 	ASSERT3P(&t->zthr_func, !=, NULL);
416eda14cbcSMatt Macy 	ASSERT(!t->zthr_cancel);
417eda14cbcSMatt Macy 	ASSERT(!t->zthr_haswaiters);
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy 	/*
420eda14cbcSMatt Macy 	 * There are 4 states that we find the zthr in at this point
421eda14cbcSMatt Macy 	 * given the locks that we hold:
422eda14cbcSMatt Macy 	 *
423eda14cbcSMatt Macy 	 * [1] The zthr was cancelled, so we spawn a new thread for
424eda14cbcSMatt Macy 	 *     the zthr (common case).
425eda14cbcSMatt Macy 	 * [2] The zthr is running at which point this is a no-op.
426eda14cbcSMatt Macy 	 * [3] The zthr is sleeping at which point this is a no-op.
427eda14cbcSMatt Macy 	 * [4] The zthr was just spawned at which point this is a
428eda14cbcSMatt Macy 	 *     no-op.
429eda14cbcSMatt Macy 	 */
430eda14cbcSMatt Macy 	if (t->zthr_thread == NULL) {
4312c48331dSMatt Macy 		t->zthr_thread = thread_create_named(t->zthr_name, NULL, 0,
4322faf504dSMartin Matuska 		    zthr_procedure, t, 0, &p0, TS_RUN, t->zthr_pri);
433eda14cbcSMatt Macy 	}
434eda14cbcSMatt Macy 
435eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
436eda14cbcSMatt Macy 	mutex_exit(&t->zthr_request_lock);
437eda14cbcSMatt Macy }
438eda14cbcSMatt Macy 
439eda14cbcSMatt Macy /*
440eda14cbcSMatt Macy  * This function is intended to be used by the zthr itself
441eda14cbcSMatt Macy  * (specifically the zthr_func callback provided) to check
442eda14cbcSMatt Macy  * if another thread has signaled it to stop running before
443eda14cbcSMatt Macy  * doing some expensive operation.
444eda14cbcSMatt Macy  *
445eda14cbcSMatt Macy  * returns TRUE if we are in the middle of trying to cancel
446eda14cbcSMatt Macy  *     this thread.
447eda14cbcSMatt Macy  *
448eda14cbcSMatt Macy  * returns FALSE otherwise.
449eda14cbcSMatt Macy  */
450eda14cbcSMatt Macy boolean_t
zthr_iscancelled(zthr_t * t)451eda14cbcSMatt Macy zthr_iscancelled(zthr_t *t)
452eda14cbcSMatt Macy {
453eda14cbcSMatt Macy 	ASSERT3P(t->zthr_thread, ==, curthread);
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy 	/*
456eda14cbcSMatt Macy 	 * The majority of the functions here grab zthr_request_lock
457eda14cbcSMatt Macy 	 * first and then zthr_state_lock. This function only grabs
458eda14cbcSMatt Macy 	 * the zthr_state_lock. That is because this function should
459eda14cbcSMatt Macy 	 * only be called from the zthr_func to check if someone has
460eda14cbcSMatt Macy 	 * issued a zthr_cancel() on the thread. If there is a zthr_cancel()
461eda14cbcSMatt Macy 	 * happening concurrently, attempting to grab the request lock
462eda14cbcSMatt Macy 	 * here would result in a deadlock.
463eda14cbcSMatt Macy 	 *
464eda14cbcSMatt Macy 	 * By grabbing only the zthr_state_lock this function is allowed
465eda14cbcSMatt Macy 	 * to run concurrently with a zthr_cancel() request.
466eda14cbcSMatt Macy 	 */
467eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
468eda14cbcSMatt Macy 	boolean_t cancelled = t->zthr_cancel;
469eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
470eda14cbcSMatt Macy 	return (cancelled);
471eda14cbcSMatt Macy }
472eda14cbcSMatt Macy 
473e92ffd9bSMartin Matuska boolean_t
zthr_iscurthread(zthr_t * t)474e92ffd9bSMartin Matuska zthr_iscurthread(zthr_t *t)
475e92ffd9bSMartin Matuska {
476e92ffd9bSMartin Matuska 	return (t->zthr_thread == curthread);
477e92ffd9bSMartin Matuska }
478e92ffd9bSMartin Matuska 
479eda14cbcSMatt Macy /*
480eda14cbcSMatt Macy  * Wait for the zthr to finish its current function. Similar to
481eda14cbcSMatt Macy  * zthr_iscancelled, you can use zthr_has_waiters to have the zthr_func end
482eda14cbcSMatt Macy  * early. Unlike zthr_cancel, the thread is not destroyed. If the zthr was
483eda14cbcSMatt Macy  * sleeping or cancelled, return immediately.
484eda14cbcSMatt Macy  */
485eda14cbcSMatt Macy void
zthr_wait_cycle_done(zthr_t * t)486eda14cbcSMatt Macy zthr_wait_cycle_done(zthr_t *t)
487eda14cbcSMatt Macy {
488eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
489eda14cbcSMatt Macy 
490eda14cbcSMatt Macy 	/*
491eda14cbcSMatt Macy 	 * Since we are holding the zthr_state_lock at this point
492eda14cbcSMatt Macy 	 * we can find the state in one of the following 5 states:
493eda14cbcSMatt Macy 	 *
494eda14cbcSMatt Macy 	 * [1] The thread has already cancelled, therefore
495eda14cbcSMatt Macy 	 *     there is nothing for us to do.
496eda14cbcSMatt Macy 	 * [2] The thread is sleeping so we set the flag, broadcast
497eda14cbcSMatt Macy 	 *     the CV and wait for it to exit.
498eda14cbcSMatt Macy 	 * [3] The thread is doing work, in which case we just set
499eda14cbcSMatt Macy 	 *     the flag and wait for it to finish.
500eda14cbcSMatt Macy 	 * [4] The thread was just created/resumed, in which case
501eda14cbcSMatt Macy 	 *     the behavior is similar to [3].
502eda14cbcSMatt Macy 	 * [5] The thread is the middle of being cancelled, which is
503eda14cbcSMatt Macy 	 *     similar to [3]. We'll wait for the cancel, which is
504eda14cbcSMatt Macy 	 *     waiting for the zthr func.
505eda14cbcSMatt Macy 	 *
506eda14cbcSMatt Macy 	 * Since requests are serialized, by the time that we get
507eda14cbcSMatt Macy 	 * control back we expect that the zthr has completed it's
508eda14cbcSMatt Macy 	 * zthr_func.
509eda14cbcSMatt Macy 	 */
510eda14cbcSMatt Macy 	if (t->zthr_thread != NULL) {
511eda14cbcSMatt Macy 		t->zthr_haswaiters = B_TRUE;
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 		/* broadcast in case the zthr is sleeping */
514eda14cbcSMatt Macy 		cv_broadcast(&t->zthr_cv);
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 		while ((t->zthr_haswaiters) && (t->zthr_thread != NULL))
517eda14cbcSMatt Macy 			cv_wait(&t->zthr_wait_cv, &t->zthr_state_lock);
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 		ASSERT(!t->zthr_haswaiters);
520eda14cbcSMatt Macy 	}
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
523eda14cbcSMatt Macy }
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy /*
526eda14cbcSMatt Macy  * This function is intended to be used by the zthr itself
527eda14cbcSMatt Macy  * to check if another thread is waiting on it to finish
528eda14cbcSMatt Macy  *
529eda14cbcSMatt Macy  * returns TRUE if we have been asked to finish.
530eda14cbcSMatt Macy  *
531eda14cbcSMatt Macy  * returns FALSE otherwise.
532eda14cbcSMatt Macy  */
533eda14cbcSMatt Macy boolean_t
zthr_has_waiters(zthr_t * t)534eda14cbcSMatt Macy zthr_has_waiters(zthr_t *t)
535eda14cbcSMatt Macy {
536eda14cbcSMatt Macy 	ASSERT3P(t->zthr_thread, ==, curthread);
537eda14cbcSMatt Macy 
538eda14cbcSMatt Macy 	mutex_enter(&t->zthr_state_lock);
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 	/*
541eda14cbcSMatt Macy 	 * Similarly to zthr_iscancelled(), we only grab the
542eda14cbcSMatt Macy 	 * zthr_state_lock so that the zthr itself can use this
543eda14cbcSMatt Macy 	 * to check for the request.
544eda14cbcSMatt Macy 	 */
545eda14cbcSMatt Macy 	boolean_t has_waiters = t->zthr_haswaiters;
546eda14cbcSMatt Macy 	mutex_exit(&t->zthr_state_lock);
547eda14cbcSMatt Macy 	return (has_waiters);
548eda14cbcSMatt Macy }
549