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/queue.h" 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 struct spdk_thread; 50 struct spdk_io_channel_iter; 51 struct spdk_poller; 52 53 /** 54 * A function that is called each time a new thread is created. 55 * The implementor of this function should frequently call 56 * spdk_thread_poll() on the thread provided. 57 * 58 * \param thread The new spdk_thread. 59 */ 60 typedef void (*spdk_new_thread_fn)(struct spdk_thread *thread); 61 62 /** 63 * A function that will be called on the target thread. 64 * 65 * \param ctx Context passed as arg to spdk_thread_pass_msg(). 66 */ 67 typedef void (*spdk_msg_fn)(void *ctx); 68 69 /** 70 * Function to be called to pass a message to a thread. 71 * 72 * \param fn Callback function for a thread. 73 * \param ctx Context passed to fn. 74 * \param thread_ctx Context for the thread. 75 */ 76 typedef void (*spdk_thread_pass_msg)(spdk_msg_fn fn, void *ctx, 77 void *thread_ctx); 78 79 /** 80 * Callback function for a poller. 81 * 82 * \param ctx Context passed as arg to spdk_poller_register(). 83 * \return 0 to indicate that polling took place but no events were found; 84 * positive to indicate that polling took place and some events were processed; 85 * negative if the poller does not provide spin-wait information. 86 */ 87 typedef int (*spdk_poller_fn)(void *ctx); 88 89 /** 90 * Function to be called to start a poller for the thread. 91 * 92 * \param thread_ctx Context for the thread. 93 * \param fn Callback function for a poller. 94 * \param arg Argument passed to callback. 95 * \param period Polling period in microseconds. 96 * 97 * \return a pointer to the poller on success, or NULL on failure. 98 */ 99 typedef struct spdk_poller *(*spdk_start_poller)(void *thread_ctx, 100 spdk_poller_fn fn, 101 void *arg, 102 uint64_t period_microseconds); 103 104 /** 105 * Function to be called to stop a poller. 106 * 107 * \param poller Poller to stop. 108 * \param thread_ctx Context for the thread. 109 */ 110 typedef void (*spdk_stop_poller)(struct spdk_poller *poller, void *thread_ctx); 111 112 /** 113 * I/O channel creation callback. 114 * 115 * \param io_device I/O device associated with this channel. 116 * \param ctx_buf Context for the I/O device. 117 */ 118 typedef int (*spdk_io_channel_create_cb)(void *io_device, void *ctx_buf); 119 120 /** 121 * I/O channel destruction callback. 122 * 123 * \param io_device I/O device associated with this channel. 124 * \param ctx_buf Context for the I/O device. 125 */ 126 typedef void (*spdk_io_channel_destroy_cb)(void *io_device, void *ctx_buf); 127 128 /** 129 * I/O device unregister callback. 130 * 131 * \param io_device Unregistered I/O device. 132 */ 133 typedef void (*spdk_io_device_unregister_cb)(void *io_device); 134 135 /** 136 * Called on the appropriate thread for each channel associated with io_device. 137 * 138 * \param i I/O channel iterator. 139 */ 140 typedef void (*spdk_channel_msg)(struct spdk_io_channel_iter *i); 141 142 /** 143 * spdk_for_each_channel() callback. 144 * 145 * \param i I/O channel iterator. 146 * \param status 0 if it completed successfully, or negative errno if it failed. 147 */ 148 typedef void (*spdk_channel_for_each_cpl)(struct spdk_io_channel_iter *i, int status); 149 150 /** 151 * \brief Represents a per-thread channel for accessing an I/O device. 152 * 153 * An I/O device may be a physical entity (i.e. NVMe controller) or a software 154 * entity (i.e. a blobstore). 155 * 156 * This structure is not part of the API - all accesses should be done through 157 * spdk_io_channel function calls. 158 */ 159 struct spdk_io_channel { 160 struct spdk_thread *thread; 161 struct io_device *dev; 162 uint32_t ref; 163 uint32_t destroy_ref; 164 TAILQ_ENTRY(spdk_io_channel) tailq; 165 spdk_io_channel_destroy_cb destroy_cb; 166 167 /* 168 * Modules will allocate extra memory off the end of this structure 169 * to store references to hardware-specific references (i.e. NVMe queue 170 * pairs, or references to child device spdk_io_channels (i.e. 171 * virtual bdevs). 172 */ 173 }; 174 175 /** 176 * Initialize the threading library. Must be called once prior to allocating any threads. 177 * 178 * \param new_thread_fn Called each time a new SPDK thread is created. The implementor 179 * is expected to frequently call spdk_thread_poll() on the provided thread. 180 * 181 * \return 0 on success. Negated errno on failure. 182 */ 183 int spdk_thread_lib_init(spdk_new_thread_fn new_thread_fn); 184 185 /** 186 * Release all resources associated with this library. 187 */ 188 void spdk_thread_lib_fini(void); 189 190 /** 191 * Creates a new SPDK thread object. 192 * 193 * \param name Human-readable name for the thread; can be retrieved with spdk_thread_get_name(). 194 * The string is copied, so the pointed-to data only needs to be valid during the 195 * spdk_thread_create() call. May be NULL to specify no name. 196 * 197 * \return a pointer to the allocated thread on success or NULL on failure.. 198 */ 199 struct spdk_thread *spdk_thread_create(const char *name); 200 201 /** 202 * Release any resources related to the given thread and destroy it. Execution 203 * continues on the current system thread after returning. 204 * 205 * \param thread The thread to destroy. 206 * 207 * All I/O channel references associated with the thread must be released using 208 * spdk_put_io_channel() prior to calling this function. 209 */ 210 void spdk_thread_exit(struct spdk_thread *thread); 211 212 /** 213 * Perform one iteration worth of processing on the thread. This includes 214 * both expired and continuous pollers as well as messages. 215 * 216 * \param thread The thread to process 217 * \param max_msgs The maximum number of messages that will be processed. 218 * Use 0 to process the default number of messages (8). 219 * 220 * \return 1 if work was done. 0 if no work was done. -1 if unknown. 221 */ 222 int spdk_thread_poll(struct spdk_thread *thread, uint32_t max_msgs); 223 224 /** 225 * Return the number of ticks until the next timed poller 226 * would expire. Timed pollers are pollers for which 227 * period_microseconds is greater than 0. 228 * 229 * \param thread The thread to check poller expiration times on 230 * 231 * \return Number of ticks. If no timed pollers, return 0. 232 */ 233 uint64_t spdk_thread_next_poller_expiration(struct spdk_thread *thread); 234 235 /** 236 * Returns whether there are any active pollers (pollers for which 237 * period_microseconds equals 0) registered to be run on the thread. 238 * 239 * \param thread The thread to check. 240 * 241 * \return 1 if there is at least one active poller, 0 otherwise. 242 */ 243 int spdk_thread_has_active_pollers(struct spdk_thread *thread); 244 245 /** 246 * Get count of allocated threads. 247 */ 248 uint32_t spdk_thread_get_count(void); 249 250 /** 251 * Get a handle to the current thread. 252 * 253 * This handle may be passed to other threads and used as the target of 254 * spdk_thread_send_msg(). 255 * 256 * \sa spdk_io_channel_get_thread() 257 * 258 * \return a pointer to the current thread on success or NULL on failure. 259 */ 260 struct spdk_thread *spdk_get_thread(void); 261 262 /** 263 * Get a thread's name. 264 * 265 * \param thread Thread to query. 266 * 267 * \return the name of the thread. 268 */ 269 const char *spdk_thread_get_name(const struct spdk_thread *thread); 270 271 /** 272 * Send a message to the given thread. 273 * 274 * The message may be sent asynchronously - i.e. spdk_thread_send_msg may return 275 * prior to `fn` being called. 276 * 277 * \param thread The target thread. 278 * \param fn This function will be called on the given thread. 279 * \param ctx This context will be passed to fn when called. 280 */ 281 void spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx); 282 283 /** 284 * Send a message to each thread, serially. 285 * 286 * The message is sent asynchronously - i.e. spdk_for_each_thread will return 287 * prior to `fn` being called on each thread. 288 * 289 * \param fn This is the function that will be called on each thread. 290 * \param ctx This context will be passed to fn when called. 291 * \param cpl This will be called on the originating thread after `fn` has been 292 * called on each thread. 293 */ 294 void spdk_for_each_thread(spdk_msg_fn fn, void *ctx, spdk_msg_fn cpl); 295 296 /** 297 * Register a poller on the current thread. 298 * 299 * The poller can be unregistered by calling spdk_poller_unregister(). 300 * 301 * \param fn This function will be called every `period_microseconds`. 302 * \param arg Argument passed to fn. 303 * \param period_microseconds How often to call `fn`. If 0, call `fn` as often 304 * as possible. 305 * 306 * \return a pointer to the poller registered on the current thread on success 307 * or NULL on failure. 308 */ 309 struct spdk_poller *spdk_poller_register(spdk_poller_fn fn, 310 void *arg, 311 uint64_t period_microseconds); 312 313 /** 314 * Unregister a poller on the current thread. 315 * 316 * \param ppoller The poller to unregister. 317 */ 318 void spdk_poller_unregister(struct spdk_poller **ppoller); 319 320 /** 321 * Register the opaque io_device context as an I/O device. 322 * 323 * After an I/O device is registered, it can return I/O channels using the 324 * spdk_get_io_channel() function. 325 * 326 * \param io_device The pointer to io_device context. 327 * \param create_cb Callback function invoked to allocate any resources required 328 * for a new I/O channel. 329 * \param destroy_cb Callback function invoked to release the resources for an 330 * I/O channel. 331 * \param ctx_size The size of the context buffer allocated to store references 332 * to allocated I/O channel resources. 333 * \param name A string name for the device used only for debugging. Optional - 334 * may be NULL. 335 */ 336 void spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb, 337 spdk_io_channel_destroy_cb destroy_cb, uint32_t ctx_size, 338 const char *name); 339 340 /** 341 * Unregister the opaque io_device context as an I/O device. 342 * 343 * The actual unregistration might be deferred until all active I/O channels are 344 * destroyed. 345 * 346 * \param io_device The pointer to io_device context. 347 * \param unregister_cb An optional callback function invoked to release any 348 * references to this I/O device. 349 */ 350 void spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregister_cb); 351 352 /** 353 * Get an I/O channel for the specified io_device to be used by the calling thread. 354 * 355 * The io_device context pointer specified must have previously been registered 356 * using spdk_io_device_register(). If an existing I/O channel does not exist 357 * yet for the given io_device on the calling thread, it will allocate an I/O 358 * channel and invoke the create_cb function pointer specified in spdk_io_device_register(). 359 * If an I/O channel already exists for the given io_device on the calling thread, 360 * its reference is returned rather than creating a new I/O channel. 361 * 362 * \param io_device The pointer to io_device context. 363 * 364 * \return a pointer to the I/O channel for this device on success or NULL on failure. 365 */ 366 struct spdk_io_channel *spdk_get_io_channel(void *io_device); 367 368 /** 369 * Release a reference to an I/O channel. This happens asynchronously. 370 * 371 * Actual release will happen on the same thread that called spdk_get_io_channel() 372 * for the specified I/O channel. If this releases the last reference to the 373 * I/O channel, The destroy_cb function specified in spdk_io_device_register() 374 * will be invoked to release any associated resources. 375 * 376 * \param ch I/O channel to release a reference. 377 */ 378 void spdk_put_io_channel(struct spdk_io_channel *ch); 379 380 /** 381 * Get the context buffer associated with an I/O channel. 382 * 383 * \param ch I/O channel. 384 * 385 * \return a pointer to the context buffer. 386 */ 387 static inline void * 388 spdk_io_channel_get_ctx(struct spdk_io_channel *ch) 389 { 390 return (uint8_t *)ch + sizeof(*ch); 391 } 392 393 /** 394 * Get I/O channel from the context buffer. This is the inverse of 395 * spdk_io_channel_get_ctx(). 396 * 397 * \param ctx The pointer to the context buffer. 398 * 399 * \return a pointer to the I/O channel associated with the context buffer. 400 */ 401 struct spdk_io_channel *spdk_io_channel_from_ctx(void *ctx); 402 403 /** 404 * Get the thread associated with an I/O channel. 405 * 406 * \param ch I/O channel. 407 * 408 * \return a pointer to the thread associated with the I/O channel 409 */ 410 struct spdk_thread *spdk_io_channel_get_thread(struct spdk_io_channel *ch); 411 412 /** 413 * Call 'fn' on each channel associated with io_device. 414 * 415 * This happens asynchronously, so fn may be called after spdk_for_each_channel 416 * returns. 'fn' will be called for each channel serially, such that two calls 417 * to 'fn' will not overlap in time. After 'fn' has been called, call 418 * spdk_for_each_channel_continue() to continue iterating. 419 * 420 * \param io_device 'fn' will be called on each channel associated with this io_device. 421 * \param fn Called on the appropriate thread for each channel associated with io_device. 422 * \param ctx Context buffer registered to spdk_io_channel_iter that can be obatined 423 * form the function spdk_io_channel_iter_get_ctx(). 424 * \param cpl Called on the thread that spdk_for_each_channel was initially called 425 * from when 'fn' has been called on each channel. 426 */ 427 void spdk_for_each_channel(void *io_device, spdk_channel_msg fn, void *ctx, 428 spdk_channel_for_each_cpl cpl); 429 430 /** 431 * Get io_device from the I/O channel iterator. 432 * 433 * \param i I/O channel iterator. 434 * 435 * \return a pointer to the io_device. 436 */ 437 void *spdk_io_channel_iter_get_io_device(struct spdk_io_channel_iter *i); 438 439 /** 440 * Get I/O channel from the I/O channel iterator. 441 * 442 * \param i I/O channel iterator. 443 * 444 * \return a pointer to the I/O channel. 445 */ 446 struct spdk_io_channel *spdk_io_channel_iter_get_channel(struct spdk_io_channel_iter *i); 447 448 /** 449 * Get context buffer from the I/O channel iterator. 450 * 451 * \param i I/O channel iterator. 452 * 453 * \return a pointer to the context buffer. 454 */ 455 void *spdk_io_channel_iter_get_ctx(struct spdk_io_channel_iter *i); 456 457 /** 458 * Helper function to iterate all channels for spdk_for_each_channel(). 459 * 460 * \param i I/O channel iterator. 461 * \param status Status for the I/O channel iterator. 462 */ 463 void spdk_for_each_channel_continue(struct spdk_io_channel_iter *i, int status); 464 465 #ifdef __cplusplus 466 } 467 #endif 468 469 #endif /* SPDK_THREAD_H_ */ 470