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