xref: /spdk/include/spdk/thread.h (revision b30d57cdad6d2bc75cc1e4e2ebbcebcb0d98dcfa)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /** \file
35  * Thread
36  */
37 
38 #ifndef SPDK_THREAD_H_
39 #define SPDK_THREAD_H_
40 
41 #include "spdk/stdinc.h"
42 
43 #include "spdk/cpuset.h"
44 #include "spdk/queue.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 enum spdk_thread_poller_rc {
51 	SPDK_POLLER_IDLE,
52 	SPDK_POLLER_BUSY,
53 };
54 
55 /**
56  * A stackless, lightweight thread.
57  */
58 struct spdk_thread;
59 
60 /**
61  * A function repeatedly called on the same spdk_thread.
62  */
63 struct spdk_poller;
64 
65 struct spdk_io_channel_iter;
66 
67 /**
68  * A function that is called each time a new thread is created.
69  * The implementor of this function should frequently call
70  * spdk_thread_poll() on the thread provided.
71  *
72  * \param thread The new spdk_thread.
73  */
74 typedef int (*spdk_new_thread_fn)(struct spdk_thread *thread);
75 
76 /**
77  * SPDK thread operation type.
78  */
79 enum spdk_thread_op {
80 	/* Called each time a new thread is created. The implementor of this operation
81 	 * should frequently call spdk_thread_poll() on the thread provided.
82 	 */
83 	SPDK_THREAD_OP_NEW,
84 
85 	/* Called when SPDK thread needs to be rescheduled. (e.g., when cpumask of the
86 	 * SPDK thread is updated.
87 	 */
88 	SPDK_THREAD_OP_RESCHED,
89 };
90 
91 /**
92  * Function to be called for SPDK thread operation.
93  */
94 typedef int (*spdk_thread_op_fn)(struct spdk_thread *thread, enum spdk_thread_op op);
95 
96 /**
97  * Function to check whether the SPDK thread operation is supported.
98  */
99 typedef bool (*spdk_thread_op_supported_fn)(enum spdk_thread_op op);
100 
101 /**
102  * A function that will be called on the target thread.
103  *
104  * \param ctx Context passed as arg to spdk_thread_pass_msg().
105  */
106 typedef void (*spdk_msg_fn)(void *ctx);
107 
108 /**
109  * Function to be called to pass a message to a thread.
110  *
111  * \param fn Callback function for a thread.
112  * \param ctx Context passed to fn.
113  * \param thread_ctx Context for the thread.
114  */
115 typedef void (*spdk_thread_pass_msg)(spdk_msg_fn fn, void *ctx,
116 				     void *thread_ctx);
117 
118 /**
119  * Callback function for a poller.
120  *
121  * \param ctx Context passed as arg to spdk_poller_register().
122  * \return 0 to indicate that polling took place but no events were found;
123  * positive to indicate that polling took place and some events were processed;
124  * negative if the poller does not provide spin-wait information.
125  */
126 typedef int (*spdk_poller_fn)(void *ctx);
127 
128 /**
129  * Function to be called to start a poller for the thread.
130  *
131  * \param thread_ctx Context for the thread.
132  * \param fn Callback function for a poller.
133  * \param arg Argument passed to callback.
134  * \param period Polling period in microseconds.
135  *
136  * \return a pointer to the poller on success, or NULL on failure.
137  */
138 typedef struct spdk_poller *(*spdk_start_poller)(void *thread_ctx,
139 		spdk_poller_fn fn,
140 		void *arg,
141 		uint64_t period_microseconds);
142 
143 /**
144  * Function to be called to stop a poller.
145  *
146  * \param poller Poller to stop.
147  * \param thread_ctx Context for the thread.
148  */
149 typedef void (*spdk_stop_poller)(struct spdk_poller *poller, void *thread_ctx);
150 
151 /**
152  * I/O channel creation callback.
153  *
154  * \param io_device I/O device associated with this channel.
155  * \param ctx_buf Context for the I/O device.
156  */
157 typedef int (*spdk_io_channel_create_cb)(void *io_device, void *ctx_buf);
158 
159 /**
160  * I/O channel destruction callback.
161  *
162  * \param io_device I/O device associated with this channel.
163  * \param ctx_buf Context for the I/O device.
164  */
165 typedef void (*spdk_io_channel_destroy_cb)(void *io_device, void *ctx_buf);
166 
167 /**
168  * I/O device unregister callback.
169  *
170  * \param io_device Unregistered I/O device.
171  */
172 typedef void (*spdk_io_device_unregister_cb)(void *io_device);
173 
174 /**
175  * Called on the appropriate thread for each channel associated with io_device.
176  *
177  * \param i I/O channel iterator.
178  */
179 typedef void (*spdk_channel_msg)(struct spdk_io_channel_iter *i);
180 
181 /**
182  * spdk_for_each_channel() callback.
183  *
184  * \param i I/O channel iterator.
185  * \param status 0 if it completed successfully, or negative errno if it failed.
186  */
187 typedef void (*spdk_channel_for_each_cpl)(struct spdk_io_channel_iter *i, int status);
188 
189 /**
190  * \brief Represents a per-thread channel for accessing an I/O device.
191  *
192  * An I/O device may be a physical entity (i.e. NVMe controller) or a software
193  *  entity (i.e. a blobstore).
194  *
195  * This structure is not part of the API - all accesses should be done through
196  *  spdk_io_channel function calls.
197  */
198 struct spdk_io_channel {
199 	struct spdk_thread		*thread;
200 	struct io_device		*dev;
201 	uint32_t			ref;
202 	uint32_t			destroy_ref;
203 	TAILQ_ENTRY(spdk_io_channel)	tailq;
204 	spdk_io_channel_destroy_cb	destroy_cb;
205 
206 	/*
207 	 * Modules will allocate extra memory off the end of this structure
208 	 *  to store references to hardware-specific references (i.e. NVMe queue
209 	 *  pairs, or references to child device spdk_io_channels (i.e.
210 	 *  virtual bdevs).
211 	 */
212 };
213 
214 /**
215  * Initialize the threading library. Must be called once prior to allocating any threads.
216  *
217  * \param new_thread_fn Called each time a new SPDK thread is created. The implementor
218  * is expected to frequently call spdk_thread_poll() on the provided thread.
219  * \param ctx_sz For each thread allocated, an additional region of memory of
220  * size ctx_size will also be allocated, for use by the thread scheduler. A pointer
221  * to this region may be obtained by calling spdk_thread_get_ctx().
222  *
223  * \return 0 on success. Negated errno on failure.
224  */
225 int spdk_thread_lib_init(spdk_new_thread_fn new_thread_fn, size_t ctx_sz);
226 
227 /**
228  * Initialize the threading library. Must be called once prior to allocating any threads
229  *
230  * Both thread_op_fn and thread_op_type_supported_fn have to be specified or not
231  * specified together.
232  *
233  * \param thread_op_fn Called for SPDK thread operation.
234  * \param thread_op_supported_fn Called to check whether the SPDK thread operation is supported.
235  * \param ctx_sz For each thread allocated, for use by the thread scheduler. A pointer
236  * to this region may be obtained by calling spdk_thread_get_ctx().
237  *
238  * \return 0 on success. Negated errno on failure.
239  */
240 int spdk_thread_lib_init_ext(spdk_thread_op_fn thread_op_fn,
241 			     spdk_thread_op_supported_fn thread_op_supported_fn,
242 			     size_t ctx_sz);
243 
244 /**
245  * Release all resources associated with this library.
246  */
247 void spdk_thread_lib_fini(void);
248 
249 /**
250  * Creates a new SPDK thread object.
251  *
252  * \param name Human-readable name for the thread; can be retrieved with spdk_thread_get_name().
253  * The string is copied, so the pointed-to data only needs to be valid during the
254  * spdk_thread_create() call. May be NULL to specify no name.
255  * \param cpumask Optional mask of CPU cores on which to schedule this thread. This is only
256  * a suggestion to the scheduler. The value is copied, so cpumask may be released when
257  * this function returns. May be NULL if no mask is required.
258  *
259  * \return a pointer to the allocated thread on success or NULL on failure..
260  */
261 struct spdk_thread *spdk_thread_create(const char *name, struct spdk_cpuset *cpumask);
262 
263 /**
264  * Force the current system thread to act as if executing the given SPDK thread.
265  *
266  * \param thread The thread to set.
267  */
268 void spdk_set_thread(struct spdk_thread *thread);
269 
270 /**
271  * Mark the thread as exited, failing all future spdk_thread_send_msg(),
272  * spdk_poller_register(), and spdk_get_io_channel() calls. May only be called
273  * within an spdk poller or message.
274  *
275  * All I/O channel references associated with the thread must be released
276  * using spdk_put_io_channel(), and all active pollers associated with the thread
277  * should be unregistered using spdk_poller_unregister(), prior to calling
278  * this function. This function will complete these processing. The completion can
279  * be queried by spdk_thread_is_exited().
280  *
281  * \param thread The thread to destroy.
282  *
283  * \return always 0. (return value was deprecated but keep it for ABI compatibility.)
284  */
285 int spdk_thread_exit(struct spdk_thread *thread);
286 
287 /**
288  * Returns whether the thread is marked as exited.
289  *
290  * \param thread The thread to query.
291  *
292  * \return true if marked as exited, false otherwise.
293  */
294 bool spdk_thread_is_exited(struct spdk_thread *thread);
295 
296 /**
297  * Destroy a thread, releasing all of its resources. May only be called
298  * on a thread previously marked as exited.
299  *
300  * \param thread The thread to destroy.
301  *
302  */
303 void spdk_thread_destroy(struct spdk_thread *thread);
304 
305 /**
306  * Return a pointer to this thread's context.
307  *
308  * \param thread The thread on which to get the context.
309  *
310  * \return a pointer to the per-thread context, or NULL if there is
311  * no per-thread context.
312  */
313 void *spdk_thread_get_ctx(struct spdk_thread *thread);
314 
315 /**
316  * Get the thread's cpumask.
317  *
318  * \param thread The thread to get the cpumask for.
319  *
320  * \return cpuset pointer
321  */
322 struct spdk_cpuset *spdk_thread_get_cpumask(struct spdk_thread *thread);
323 
324 /**
325  * Set the current thread's cpumask to the specified value. The thread may be
326  * rescheduled to one of the CPUs specified in the cpumask.
327  *
328  * This API requires SPDK thread operation supports SPDK_THREAD_OP_RESCHED.
329  *
330  * \param cpumask The new cpumask for the thread.
331  *
332  * \return 0 on success, negated errno otherwise.
333  */
334 int spdk_thread_set_cpumask(struct spdk_cpuset *cpumask);
335 
336 /**
337  * Return the thread object associated with the context handle previously
338  * obtained by calling spdk_thread_get_ctx().
339  *
340  * \param ctx A context previously obtained by calling spdk_thread_get_ctx()
341  *
342  * \return The associated thread.
343  */
344 struct spdk_thread *spdk_thread_get_from_ctx(void *ctx);
345 
346 /**
347  * Perform one iteration worth of processing on the thread. This includes
348  * both expired and continuous pollers as well as messages. If the thread
349  * has exited, return immediately.
350  *
351  * \param thread The thread to process
352  * \param max_msgs The maximum number of messages that will be processed.
353  *                 Use 0 to process the default number of messages (8).
354  * \param now The current time, in ticks. Optional. If 0 is passed, this
355  *            function will call spdk_get_ticks() to get the current time.
356  *            The current time is used as start time and this function
357  *            will call spdk_get_ticks() at its end to know end time to
358  *            measure run time of this function.
359  *
360  * \return 1 if work was done. 0 if no work was done.
361  */
362 int spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs, uint64_t now);
363 
364 /**
365  * Return the number of ticks until the next timed poller
366  * would expire. Timed pollers are pollers for which
367  * period_microseconds is greater than 0.
368  *
369  * \param thread The thread to check poller expiration times on
370  *
371  * \return Number of ticks. If no timed pollers, return 0.
372  */
373 uint64_t spdk_thread_next_poller_expiration(struct spdk_thread *thread);
374 
375 /**
376  * Returns whether there are any active pollers (pollers for which
377  * period_microseconds equals 0) registered to be run on the thread.
378  *
379  * \param thread The thread to check.
380  *
381  * \return 1 if there is at least one active poller, 0 otherwise.
382  */
383 int spdk_thread_has_active_pollers(struct spdk_thread *thread);
384 
385 /**
386  * Returns whether there are any pollers registered to be run
387  * on the thread.
388  *
389  * \param thread The thread to check.
390  *
391  * \return true if there is any active poller, false otherwise.
392  */
393 bool spdk_thread_has_pollers(struct spdk_thread *thread);
394 
395 /**
396  * Returns whether there are scheduled operations to be run on the thread.
397  *
398  * \param thread The thread to check.
399  *
400  * \return true if there are no scheduled operations, false otherwise.
401  */
402 bool spdk_thread_is_idle(struct spdk_thread *thread);
403 
404 /**
405  * Get count of allocated threads.
406  */
407 uint32_t spdk_thread_get_count(void);
408 
409 /**
410  * Get a handle to the current thread.
411  *
412  * This handle may be passed to other threads and used as the target of
413  * spdk_thread_send_msg().
414  *
415  * \sa spdk_io_channel_get_thread()
416  *
417  * \return a pointer to the current thread on success or NULL on failure.
418  */
419 struct spdk_thread *spdk_get_thread(void);
420 
421 /**
422  * Get a thread's name.
423  *
424  * \param thread Thread to query.
425  *
426  * \return the name of the thread.
427  */
428 const char *spdk_thread_get_name(const struct spdk_thread *thread);
429 
430 /**
431  * Get a thread's ID.
432  *
433  * \param thread Thread to query.
434  *
435  * \return the ID of the thread..
436  */
437 uint64_t spdk_thread_get_id(const struct spdk_thread *thread);
438 
439 /**
440  * Get the thread by the ID.
441  *
442  * \param id ID of the thread.
443  * \return Thread whose ID matches or NULL otherwise.
444  */
445 struct spdk_thread *spdk_thread_get_by_id(uint64_t id);
446 
447 struct spdk_thread_stats {
448 	uint64_t busy_tsc;
449 	uint64_t idle_tsc;
450 };
451 
452 /**
453  * Get statistics about the current thread.
454  *
455  * Copy cumulative thread stats values to the provided thread stats structure.
456  *
457  * \param stats User's thread_stats structure.
458  */
459 int spdk_thread_get_stats(struct spdk_thread_stats *stats);
460 
461 /**
462  * Return the TSC value from the end of the last time this thread was polled.
463  *
464  * \param thread Thread to query.
465  *
466  * \return TSC value from the end of the last time this thread was polled.
467  */
468 uint64_t spdk_thread_get_last_tsc(struct spdk_thread *thread);
469 
470 /**
471  * Send a message to the given thread.
472  *
473  * The message will be sent asynchronously - i.e. spdk_thread_send_msg will always return
474  * prior to `fn` being called.
475  *
476  * \param thread The target thread.
477  * \param fn This function will be called on the given thread.
478  * \param ctx This context will be passed to fn when called.
479  *
480  * \return 0 on success
481  * \return -ENOMEM if the message could not be allocated
482  * \return -EIO if the message could not be sent to the destination thread
483  */
484 int spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx);
485 
486 /**
487  * Send a message to the given thread. Only one critical message can be outstanding at the same
488  * time. It's intended to use this function in any cases that might interrupt the execution of the
489  * application, such as signal handlers.
490  *
491  * The message will be sent asynchronously - i.e. spdk_thread_send_critical_msg will always return
492  * prior to `fn` being called.
493  *
494  * \param thread The target thread.
495  * \param fn This function will be called on the given thread.
496  *
497  * \return 0 on success
498  * \return -EIO if the message could not be sent to the destination thread, due to an already
499  * outstanding critical message
500  */
501 int spdk_thread_send_critical_msg(struct spdk_thread *thread, spdk_msg_fn fn);
502 
503 /**
504  * Send a message to each thread, serially.
505  *
506  * The message is sent asynchronously - i.e. spdk_for_each_thread will return
507  * prior to `fn` being called on each thread.
508  *
509  * \param fn This is the function that will be called on each thread.
510  * \param ctx This context will be passed to fn when called.
511  * \param cpl This will be called on the originating thread after `fn` has been
512  * called on each thread.
513  */
514 void spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl);
515 
516 /**
517  * Register a poller on the current thread.
518  *
519  * The poller can be unregistered by calling spdk_poller_unregister().
520  *
521  * \param fn This function will be called every `period_microseconds`.
522  * \param arg Argument passed to fn.
523  * \param period_microseconds How often to call `fn`. If 0, call `fn` as often
524  *  as possible.
525  *
526  * \return a pointer to the poller registered on the current thread on success
527  * or NULL on failure.
528  */
529 struct spdk_poller *spdk_poller_register(spdk_poller_fn fn,
530 		void *arg,
531 		uint64_t period_microseconds);
532 
533 /**
534  * Register a poller on the current thread with arbitrary name.
535  *
536  * The poller can be unregistered by calling spdk_poller_unregister().
537  *
538  * \param fn This function will be called every `period_microseconds`.
539  * \param arg Argument passed to fn.
540  * \param period_microseconds How often to call `fn`. If 0, call `fn` as often
541  *  as possible.
542  * \param name Human readable name for the poller. Pointer of the poller function
543  * name is set if NULL.
544  *
545  * \return a pointer to the poller registered on the current thread on success
546  * or NULL on failure.
547  */
548 struct spdk_poller *spdk_poller_register_named(spdk_poller_fn fn,
549 		void *arg,
550 		uint64_t period_microseconds,
551 		const char *name);
552 
553 /*
554  * \brief Register a poller on the current thread with setting its name
555  * to the string of the poller function name.
556  */
557 #define SPDK_POLLER_REGISTER(fn, arg, period_microseconds)	\
558 	spdk_poller_register_named(fn, arg, period_microseconds, #fn)
559 
560 /**
561  * Unregister a poller on the current thread.
562  *
563  * \param ppoller The poller to unregister.
564  */
565 void spdk_poller_unregister(struct spdk_poller **ppoller);
566 
567 /**
568  * Pause a poller on the current thread.
569  *
570  * The poller is not run until it is resumed with spdk_poller_resume().  It is
571  * perfectly fine to pause an already paused poller.
572  *
573  * \param poller The poller to pause.
574  */
575 void spdk_poller_pause(struct spdk_poller *poller);
576 
577 /**
578  * Resume a poller on the current thread.
579  *
580  * Resumes a poller paused with spdk_poller_pause().  It is perfectly fine to
581  * resume an unpaused poller.
582  *
583  * \param poller The poller to resume.
584  */
585 void spdk_poller_resume(struct spdk_poller *poller);
586 
587 /**
588  * Register the opaque io_device context as an I/O device.
589  *
590  * After an I/O device is registered, it can return I/O channels using the
591  * spdk_get_io_channel() function.
592  *
593  * \param io_device The pointer to io_device context.
594  * \param create_cb Callback function invoked to allocate any resources required
595  * for a new I/O channel.
596  * \param destroy_cb Callback function invoked to release the resources for an
597  * I/O channel.
598  * \param ctx_size The size of the context buffer allocated to store references
599  * to allocated I/O channel resources.
600  * \param name A string name for the device used only for debugging. Optional -
601  * may be NULL.
602  */
603 void spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb,
604 			     spdk_io_channel_destroy_cb destroy_cb, uint32_t ctx_size,
605 			     const char *name);
606 
607 /**
608  * Unregister the opaque io_device context as an I/O device.
609  *
610  * The actual unregistration might be deferred until all active I/O channels are
611  * destroyed.
612  *
613  * \param io_device The pointer to io_device context.
614  * \param unregister_cb An optional callback function invoked to release any
615  * references to this I/O device.
616  */
617 void spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregister_cb);
618 
619 /**
620  * Get an I/O channel for the specified io_device to be used by the calling thread.
621  *
622  * The io_device context pointer specified must have previously been registered
623  * using spdk_io_device_register(). If an existing I/O channel does not exist
624  * yet for the given io_device on the calling thread, it will allocate an I/O
625  * channel and invoke the create_cb function pointer specified in spdk_io_device_register().
626  * If an I/O channel already exists for the given io_device on the calling thread,
627  * its reference is returned rather than creating a new I/O channel.
628  *
629  * \param io_device The pointer to io_device context.
630  *
631  * \return a pointer to the I/O channel for this device on success or NULL on failure.
632  */
633 struct spdk_io_channel *spdk_get_io_channel(void *io_device);
634 
635 /**
636  * Release a reference to an I/O channel. This happens asynchronously.
637  *
638  * This must be called on the same thread that called spdk_get_io_channel()
639  * for the specified I/O channel. If this releases the last reference to the
640  * I/O channel, The destroy_cb function specified in spdk_io_device_register()
641  * will be invoked to release any associated resources.
642  *
643  * \param ch I/O channel to release a reference.
644  */
645 void spdk_put_io_channel(struct spdk_io_channel *ch);
646 
647 /**
648  * Get the context buffer associated with an I/O channel.
649  *
650  * \param ch I/O channel.
651  *
652  * \return a pointer to the context buffer.
653  */
654 static inline void *
655 spdk_io_channel_get_ctx(struct spdk_io_channel *ch)
656 {
657 	return (uint8_t *)ch + sizeof(*ch);
658 }
659 
660 /**
661  * Get I/O channel from the context buffer. This is the inverse of
662  * spdk_io_channel_get_ctx().
663  *
664  * \param ctx The pointer to the context buffer.
665  *
666  * \return a pointer to the I/O channel associated with the context buffer.
667  */
668 struct spdk_io_channel *spdk_io_channel_from_ctx(void *ctx);
669 
670 /**
671  * Get the thread associated with an I/O channel.
672  *
673  * \param ch I/O channel.
674  *
675  * \return a pointer to the thread associated with the I/O channel
676  */
677 struct spdk_thread *spdk_io_channel_get_thread(struct spdk_io_channel *ch);
678 
679 /**
680  * Call 'fn' on each channel associated with io_device.
681  *
682  * This happens asynchronously, so fn may be called after spdk_for_each_channel
683  * returns. 'fn' will be called for each channel serially, such that two calls
684  * to 'fn' will not overlap in time. After 'fn' has been called, call
685  * spdk_for_each_channel_continue() to continue iterating.
686  *
687  * \param io_device 'fn' will be called on each channel associated with this io_device.
688  * \param fn Called on the appropriate thread for each channel associated with io_device.
689  * \param ctx Context buffer registered to spdk_io_channel_iter that can be obatined
690  * form the function spdk_io_channel_iter_get_ctx().
691  * \param cpl Called on the thread that spdk_for_each_channel was initially called
692  * from when 'fn' has been called on each channel.
693  */
694 void spdk_for_each_channel(void *io_device, spdk_channel_msg fn, void *ctx,
695 			   spdk_channel_for_each_cpl cpl);
696 
697 /**
698  * Get io_device from the I/O channel iterator.
699  *
700  * \param i I/O channel iterator.
701  *
702  * \return a pointer to the io_device.
703  */
704 void *spdk_io_channel_iter_get_io_device(struct spdk_io_channel_iter *i);
705 
706 /**
707  * Get I/O channel from the I/O channel iterator.
708  *
709  * \param i I/O channel iterator.
710  *
711  * \return a pointer to the I/O channel.
712  */
713 struct spdk_io_channel *spdk_io_channel_iter_get_channel(struct spdk_io_channel_iter *i);
714 
715 /**
716  * Get context buffer from the I/O channel iterator.
717  *
718  * \param i I/O channel iterator.
719  *
720  * \return a pointer to the context buffer.
721  */
722 void *spdk_io_channel_iter_get_ctx(struct spdk_io_channel_iter *i);
723 
724 /**
725  * Helper function to iterate all channels for spdk_for_each_channel().
726  *
727  * \param i I/O channel iterator.
728  * \param status Status for the I/O channel iterator.
729  */
730 void spdk_for_each_channel_continue(struct spdk_io_channel_iter *i, int status);
731 
732 /**
733  * A representative for registered interrupt file descriptor.
734  */
735 struct spdk_interrupt;
736 
737 /**
738  * Callback function registered for interrupt file descriptor.
739  *
740  * \param ctx Context passed as arg to spdk_interrupt_register().
741  *
742  * \return 0 to indicate that interrupt took place but no events were found;
743  * positive to indicate that interrupt took place and some events were processed;
744  * negative if no event information is provided.
745  */
746 typedef int (*spdk_interrupt_fn)(void *ctx);
747 
748 /**
749  * Register an spdk_interrupt on the current thread. The provided function
750  * will be called any time the associated file descriptor is written to.
751  *
752  * \param efd File descriptor of the spdk_interrupt.
753  * \param fn Called each time there are events in spdk_interrupt.
754  * \param arg Function argument for fn.
755  * \param name Human readable name for the spdk_interrupt. Pointer of the spdk_interrupt
756  * name is set if NULL.
757  *
758  * \return a pointer to the spdk_interrupt registered on the current thread on success
759  * or NULL on failure.
760  */
761 struct spdk_interrupt *spdk_interrupt_register(int efd, spdk_interrupt_fn fn,
762 		void *arg, const char *name);
763 
764 /*
765  * \brief Register an spdk_interrupt on the current thread with setting its name
766  * to the string of the spdk_interrupt function name.
767  */
768 #define SPDK_INTERRUPT_REGISTER(efd, fn, arg)	\
769 	spdk_interrupt_register(efd, fn, arg, #fn)
770 
771 /**
772  * Unregister an spdk_interrupt on the current thread.
773  *
774  * \param pintr The spdk_interrupt to unregister.
775  */
776 void spdk_interrupt_unregister(struct spdk_interrupt **pintr);
777 
778 enum spdk_interrupt_event_types {
779 	SPDK_INTERRUPT_EVENT_IN = 0x001,
780 	SPDK_INTERRUPT_EVENT_OUT = 0x004,
781 	SPDK_INTERRUPT_EVENT_ET = 1u << 31
782 };
783 
784 /**
785  * Change the event_types associated with the spdk_interrupt on the current thread.
786  *
787  * \param intr The pointer to the spdk_interrupt registered on the current thread.
788  * \param event_types New event_types for the spdk_interrupt.
789  *
790  * \return 0 if success or -errno if failed.
791  */
792 int spdk_interrupt_set_event_types(struct spdk_interrupt *intr,
793 				   enum spdk_interrupt_event_types event_types);
794 
795 /**
796  * Return a file descriptor that becomes ready whenever any of the registered
797  * interrupt file descriptors are ready
798  *
799  * \param thread The thread to get.
800  *
801  * \return The spdk_interrupt fd of thread itself.
802  */
803 int spdk_thread_get_interrupt_fd(struct spdk_thread *thread);
804 
805 /**
806  * Set SPDK run as event driven mode
807  *
808  * \return 0 on success or -errno on failure
809  */
810 int spdk_interrupt_mode_enable(void);
811 
812 /**
813  * Reports whether interrupt mode is set.
814  *
815  * \return True if interrupt mode is set, false otherwise.
816  */
817 bool spdk_interrupt_mode_is_enabled(void);
818 
819 #ifdef __cplusplus
820 }
821 #endif
822 
823 #endif /* SPDK_THREAD_H_ */
824