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