xref: /spdk/lib/thread/thread.c (revision c94020001adcc0d8fae89ff7902e2e0f2243e25d)
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 #include "spdk/stdinc.h"
35 
36 #include "spdk/string.h"
37 #include "spdk/thread.h"
38 
39 #include "spdk_internal/log.h"
40 
41 #ifdef __linux__
42 #include <sys/prctl.h>
43 #endif
44 
45 #ifdef __FreeBSD__
46 #include <pthread_np.h>
47 #endif
48 
49 static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER;
50 
51 struct io_device {
52 	void				*io_device;
53 	char				*name;
54 	spdk_io_channel_create_cb	create_cb;
55 	spdk_io_channel_destroy_cb	destroy_cb;
56 	spdk_io_device_unregister_cb	unregister_cb;
57 	struct spdk_thread		*unregister_thread;
58 	uint32_t			ctx_size;
59 	uint32_t			for_each_count;
60 	TAILQ_ENTRY(io_device)		tailq;
61 
62 	uint32_t			refcnt;
63 
64 	bool				unregistered;
65 };
66 
67 static TAILQ_HEAD(, io_device) g_io_devices = TAILQ_HEAD_INITIALIZER(g_io_devices);
68 
69 struct spdk_thread {
70 	pthread_t			thread_id;
71 	spdk_thread_pass_msg		msg_fn;
72 	spdk_start_poller		start_poller_fn;
73 	spdk_stop_poller		stop_poller_fn;
74 	void				*thread_ctx;
75 	TAILQ_HEAD(, spdk_io_channel)	io_channels;
76 	TAILQ_ENTRY(spdk_thread)	tailq;
77 	char				*name;
78 };
79 
80 static TAILQ_HEAD(, spdk_thread) g_threads = TAILQ_HEAD_INITIALIZER(g_threads);
81 static uint32_t g_thread_count = 0;
82 
83 static struct spdk_thread *
84 _get_thread(void)
85 {
86 	pthread_t thread_id;
87 	struct spdk_thread *thread;
88 
89 	thread_id = pthread_self();
90 
91 	thread = NULL;
92 	TAILQ_FOREACH(thread, &g_threads, tailq) {
93 		if (thread->thread_id == thread_id) {
94 			return thread;
95 		}
96 	}
97 
98 	return NULL;
99 }
100 
101 static void
102 _set_thread_name(const char *thread_name)
103 {
104 #if defined(__linux__)
105 	prctl(PR_SET_NAME, thread_name, 0, 0, 0);
106 #elif defined(__FreeBSD__)
107 	pthread_set_name_np(pthread_self(), thread_name);
108 #else
109 #error missing platform support for thread name
110 #endif
111 }
112 
113 int
114 spdk_thread_lib_init(void)
115 {
116 	return 0;
117 }
118 
119 void
120 spdk_thread_lib_fini(void)
121 {
122 }
123 
124 struct spdk_thread *
125 spdk_allocate_thread(spdk_thread_pass_msg msg_fn,
126 		     spdk_start_poller start_poller_fn,
127 		     spdk_stop_poller stop_poller_fn,
128 		     void *thread_ctx, const char *name)
129 {
130 	struct spdk_thread *thread;
131 
132 	pthread_mutex_lock(&g_devlist_mutex);
133 
134 	thread = _get_thread();
135 	if (thread) {
136 		SPDK_ERRLOG("Double allocated SPDK thread\n");
137 		pthread_mutex_unlock(&g_devlist_mutex);
138 		return NULL;
139 	}
140 
141 	thread = calloc(1, sizeof(*thread));
142 	if (!thread) {
143 		SPDK_ERRLOG("Unable to allocate memory for thread\n");
144 		pthread_mutex_unlock(&g_devlist_mutex);
145 		return NULL;
146 	}
147 
148 	thread->thread_id = pthread_self();
149 	thread->msg_fn = msg_fn;
150 	thread->start_poller_fn = start_poller_fn;
151 	thread->stop_poller_fn = stop_poller_fn;
152 	thread->thread_ctx = thread_ctx;
153 	TAILQ_INIT(&thread->io_channels);
154 	TAILQ_INSERT_TAIL(&g_threads, thread, tailq);
155 	g_thread_count++;
156 	if (name) {
157 		_set_thread_name(name);
158 		thread->name = strdup(name);
159 	} else {
160 		thread->name = spdk_sprintf_alloc("%p", thread);
161 	}
162 
163 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Allocating new thread %s\n", thread->name);
164 
165 	pthread_mutex_unlock(&g_devlist_mutex);
166 
167 	return thread;
168 }
169 
170 void
171 spdk_free_thread(void)
172 {
173 	struct spdk_thread *thread;
174 
175 	pthread_mutex_lock(&g_devlist_mutex);
176 
177 	thread = _get_thread();
178 	if (!thread) {
179 		SPDK_ERRLOG("No thread allocated\n");
180 		pthread_mutex_unlock(&g_devlist_mutex);
181 		return;
182 	}
183 
184 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Freeing thread %s\n", thread->name);
185 
186 	assert(g_thread_count > 0);
187 	g_thread_count--;
188 	TAILQ_REMOVE(&g_threads, thread, tailq);
189 	free(thread->name);
190 	free(thread);
191 
192 	pthread_mutex_unlock(&g_devlist_mutex);
193 }
194 
195 uint32_t
196 spdk_thread_get_count(void)
197 {
198 	/*
199 	 * Return cached value of the current thread count.  We could acquire the
200 	 *  lock and iterate through the TAILQ of threads to count them, but that
201 	 *  count could still be invalidated after we release the lock.
202 	 */
203 	return g_thread_count;
204 }
205 
206 struct spdk_thread *
207 spdk_get_thread(void)
208 {
209 	struct spdk_thread *thread;
210 
211 	pthread_mutex_lock(&g_devlist_mutex);
212 
213 	thread = _get_thread();
214 	if (!thread) {
215 		SPDK_ERRLOG("No thread allocated\n");
216 	}
217 
218 	pthread_mutex_unlock(&g_devlist_mutex);
219 
220 	return thread;
221 }
222 
223 const char *
224 spdk_thread_get_name(const struct spdk_thread *thread)
225 {
226 	return thread->name;
227 }
228 
229 void
230 spdk_thread_send_msg(const struct spdk_thread *thread, spdk_thread_fn fn, void *ctx)
231 {
232 	thread->msg_fn(fn, ctx, thread->thread_ctx);
233 }
234 
235 
236 struct spdk_poller *
237 spdk_poller_register(spdk_poller_fn fn,
238 		     void *arg,
239 		     uint64_t period_microseconds)
240 {
241 	struct spdk_thread *thread;
242 	struct spdk_poller *poller;
243 
244 	thread = spdk_get_thread();
245 	if (!thread) {
246 		assert(false);
247 		return NULL;
248 	}
249 
250 	if (!thread->start_poller_fn || !thread->stop_poller_fn) {
251 		SPDK_ERRLOG("No related functions to start requested poller\n");
252 		assert(false);
253 		return NULL;
254 	}
255 
256 	poller = thread->start_poller_fn(thread->thread_ctx, fn, arg, period_microseconds);
257 	if (!poller) {
258 		SPDK_ERRLOG("Unable to start requested poller\n");
259 		assert(false);
260 		return NULL;
261 	}
262 
263 	return poller;
264 }
265 
266 void
267 spdk_poller_unregister(struct spdk_poller **ppoller)
268 {
269 	struct spdk_thread *thread;
270 	struct spdk_poller *poller;
271 
272 	poller = *ppoller;
273 	if (poller == NULL) {
274 		return;
275 	}
276 
277 	*ppoller = NULL;
278 
279 	thread = spdk_get_thread();
280 
281 	if (thread) {
282 		thread->stop_poller_fn(poller, thread->thread_ctx);
283 	}
284 }
285 
286 struct call_thread {
287 	struct spdk_thread *cur_thread;
288 	spdk_thread_fn fn;
289 	void *ctx;
290 
291 	struct spdk_thread *orig_thread;
292 	spdk_thread_fn cpl;
293 };
294 
295 static void
296 spdk_on_thread(void *ctx)
297 {
298 	struct call_thread *ct = ctx;
299 
300 	ct->fn(ct->ctx);
301 
302 	pthread_mutex_lock(&g_devlist_mutex);
303 	ct->cur_thread = TAILQ_NEXT(ct->cur_thread, tailq);
304 	pthread_mutex_unlock(&g_devlist_mutex);
305 
306 	if (!ct->cur_thread) {
307 		SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Completed thread iteration");
308 
309 		spdk_thread_send_msg(ct->orig_thread, ct->cpl, ct->ctx);
310 		free(ctx);
311 	} else {
312 		SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Continuing thread iteration to %s\n",
313 			      ct->cur_thread->name);
314 
315 		spdk_thread_send_msg(ct->cur_thread, spdk_on_thread, ctx);
316 	}
317 }
318 
319 void
320 spdk_for_each_thread(spdk_thread_fn fn, void *ctx, spdk_thread_fn cpl)
321 {
322 	struct call_thread *ct;
323 
324 	ct = calloc(1, sizeof(*ct));
325 	if (!ct) {
326 		SPDK_ERRLOG("Unable to perform thread iteration\n");
327 		cpl(ctx);
328 		return;
329 	}
330 
331 	ct->fn = fn;
332 	ct->ctx = ctx;
333 	ct->cpl = cpl;
334 
335 	pthread_mutex_lock(&g_devlist_mutex);
336 	ct->orig_thread = _get_thread();
337 	ct->cur_thread = TAILQ_FIRST(&g_threads);
338 	pthread_mutex_unlock(&g_devlist_mutex);
339 
340 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Starting thread iteration from %s\n",
341 		      ct->orig_thread->name);
342 
343 	spdk_thread_send_msg(ct->cur_thread, spdk_on_thread, ct);
344 }
345 
346 void
347 spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb,
348 			spdk_io_channel_destroy_cb destroy_cb, uint32_t ctx_size,
349 			const char *name)
350 {
351 	struct io_device *dev, *tmp;
352 
353 	assert(io_device != NULL);
354 	assert(create_cb != NULL);
355 	assert(destroy_cb != NULL);
356 
357 	dev = calloc(1, sizeof(struct io_device));
358 	if (dev == NULL) {
359 		SPDK_ERRLOG("could not allocate io_device\n");
360 		return;
361 	}
362 
363 	dev->io_device = io_device;
364 	if (name) {
365 		dev->name = strdup(name);
366 	} else {
367 		dev->name = spdk_sprintf_alloc("%p", dev);
368 	}
369 	dev->create_cb = create_cb;
370 	dev->destroy_cb = destroy_cb;
371 	dev->unregister_cb = NULL;
372 	dev->ctx_size = ctx_size;
373 	dev->for_each_count = 0;
374 	dev->unregistered = false;
375 	dev->refcnt = 0;
376 
377 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Registering io_device %s (%p) on thread %s\n",
378 		      dev->name, dev->io_device, spdk_get_thread()->name);
379 
380 	pthread_mutex_lock(&g_devlist_mutex);
381 	TAILQ_FOREACH(tmp, &g_io_devices, tailq) {
382 		if (tmp->io_device == io_device) {
383 			SPDK_ERRLOG("io_device %p already registered\n", io_device);
384 			free(dev->name);
385 			free(dev);
386 			pthread_mutex_unlock(&g_devlist_mutex);
387 			return;
388 		}
389 	}
390 	TAILQ_INSERT_TAIL(&g_io_devices, dev, tailq);
391 	pthread_mutex_unlock(&g_devlist_mutex);
392 }
393 
394 static void
395 _finish_unregister(void *arg)
396 {
397 	struct io_device *dev = arg;
398 
399 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Finishing unregistration of io_device %s (%p) on thread %s\n",
400 		      dev->name, dev->io_device, dev->unregister_thread->name);
401 
402 	dev->unregister_cb(dev->io_device);
403 	free(dev->name);
404 	free(dev);
405 }
406 
407 static void
408 _spdk_io_device_free(struct io_device *dev)
409 {
410 	if (dev->unregister_cb == NULL) {
411 		free(dev->name);
412 		free(dev);
413 	} else {
414 		assert(dev->unregister_thread != NULL);
415 		SPDK_DEBUGLOG(SPDK_LOG_THREAD, "io_device %s (%p) needs to unregister from thread %s\n",
416 			      dev->name, dev->io_device, dev->unregister_thread->name);
417 		spdk_thread_send_msg(dev->unregister_thread, _finish_unregister, dev);
418 	}
419 }
420 
421 void
422 spdk_io_device_unregister(void *io_device, spdk_io_device_unregister_cb unregister_cb)
423 {
424 	struct io_device *dev;
425 	uint32_t refcnt;
426 	struct spdk_thread *thread;
427 
428 	thread = spdk_get_thread();
429 
430 	pthread_mutex_lock(&g_devlist_mutex);
431 	TAILQ_FOREACH(dev, &g_io_devices, tailq) {
432 		if (dev->io_device == io_device) {
433 			break;
434 		}
435 	}
436 
437 	if (!dev) {
438 		SPDK_ERRLOG("io_device %p not found\n", io_device);
439 		assert(false);
440 		pthread_mutex_unlock(&g_devlist_mutex);
441 		return;
442 	}
443 
444 	if (dev->for_each_count > 0) {
445 		SPDK_ERRLOG("io_device %p has %u for_each calls outstanding\n", io_device, dev->for_each_count);
446 		pthread_mutex_unlock(&g_devlist_mutex);
447 		return;
448 	}
449 
450 	dev->unregister_cb = unregister_cb;
451 	dev->unregistered = true;
452 	TAILQ_REMOVE(&g_io_devices, dev, tailq);
453 	refcnt = dev->refcnt;
454 	dev->unregister_thread = thread;
455 	pthread_mutex_unlock(&g_devlist_mutex);
456 
457 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Unregistering io_device %s (%p) from thread %s\n",
458 		      dev->name, dev->io_device, thread->name);
459 
460 	if (refcnt > 0) {
461 		/* defer deletion */
462 		return;
463 	}
464 
465 	_spdk_io_device_free(dev);
466 }
467 
468 struct spdk_io_channel *
469 spdk_get_io_channel(void *io_device)
470 {
471 	struct spdk_io_channel *ch;
472 	struct spdk_thread *thread;
473 	struct io_device *dev;
474 	int rc;
475 
476 	pthread_mutex_lock(&g_devlist_mutex);
477 	TAILQ_FOREACH(dev, &g_io_devices, tailq) {
478 		if (dev->io_device == io_device) {
479 			break;
480 		}
481 	}
482 	if (dev == NULL) {
483 		SPDK_ERRLOG("could not find io_device %p\n", io_device);
484 		pthread_mutex_unlock(&g_devlist_mutex);
485 		return NULL;
486 	}
487 
488 	thread = _get_thread();
489 	if (!thread) {
490 		SPDK_ERRLOG("No thread allocated\n");
491 		pthread_mutex_unlock(&g_devlist_mutex);
492 		return NULL;
493 	}
494 
495 	TAILQ_FOREACH(ch, &thread->io_channels, tailq) {
496 		if (ch->dev == dev) {
497 			ch->ref++;
498 
499 			SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Get io_channel %p for io_device %s (%p) on thread %s refcnt %u\n",
500 				      ch, dev->name, dev->io_device, thread->name, ch->ref);
501 
502 			/*
503 			 * An I/O channel already exists for this device on this
504 			 *  thread, so return it.
505 			 */
506 			pthread_mutex_unlock(&g_devlist_mutex);
507 			return ch;
508 		}
509 	}
510 
511 	ch = calloc(1, sizeof(*ch) + dev->ctx_size);
512 	if (ch == NULL) {
513 		SPDK_ERRLOG("could not calloc spdk_io_channel\n");
514 		pthread_mutex_unlock(&g_devlist_mutex);
515 		return NULL;
516 	}
517 
518 	ch->dev = dev;
519 	ch->destroy_cb = dev->destroy_cb;
520 	ch->thread = thread;
521 	ch->ref = 1;
522 	TAILQ_INSERT_TAIL(&thread->io_channels, ch, tailq);
523 
524 	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Get io_channel %p for io_device %s (%p) on thread %s refcnt %u\n",
525 		      ch, dev->name, dev->io_device, thread->name, ch->ref);
526 
527 	dev->refcnt++;
528 
529 	pthread_mutex_unlock(&g_devlist_mutex);
530 
531 	rc = dev->create_cb(io_device, (uint8_t *)ch + sizeof(*ch));
532 	if (rc == -1) {
533 		pthread_mutex_lock(&g_devlist_mutex);
534 		TAILQ_REMOVE(&ch->thread->io_channels, ch, tailq);
535 		dev->refcnt--;
536 		free(ch);
537 		pthread_mutex_unlock(&g_devlist_mutex);
538 		return NULL;
539 	}
540 
541 	return ch;
542 }
543 
544 static void
545 _spdk_put_io_channel(void *arg)
546 {
547 	struct spdk_io_channel *ch = arg;
548 	bool do_remove_dev = true;
549 
550 	SPDK_DEBUGLOG(SPDK_LOG_THREAD,
551 		      "Releasing io_channel %p for io_device %s (%p). Channel thread %p. Current thread %s\n",
552 		      ch, ch->dev->name, ch->dev->io_device, ch->thread, spdk_get_thread()->name);
553 
554 	assert(ch->thread == spdk_get_thread());
555 
556 	if (ch->ref > 0) {
557 		/*
558 		 * Another reference to the associated io_device was requested
559 		 *  after this message was sent but before it had a chance to
560 		 *  execute.
561 		 */
562 		return;
563 	}
564 
565 	pthread_mutex_lock(&g_devlist_mutex);
566 	TAILQ_REMOVE(&ch->thread->io_channels, ch, tailq);
567 	pthread_mutex_unlock(&g_devlist_mutex);
568 
569 	/* Don't hold the devlist mutex while the destroy_cb is called. */
570 	ch->destroy_cb(ch->dev->io_device, spdk_io_channel_get_ctx(ch));
571 
572 	pthread_mutex_lock(&g_devlist_mutex);
573 	ch->dev->refcnt--;
574 
575 	if (!ch->dev->unregistered) {
576 		do_remove_dev = false;
577 	}
578 
579 	if (ch->dev->refcnt > 0) {
580 		do_remove_dev = false;
581 	}
582 
583 	pthread_mutex_unlock(&g_devlist_mutex);
584 
585 	if (do_remove_dev) {
586 		_spdk_io_device_free(ch->dev);
587 	}
588 	free(ch);
589 }
590 
591 void
592 spdk_put_io_channel(struct spdk_io_channel *ch)
593 {
594 	SPDK_DEBUGLOG(SPDK_LOG_THREAD,
595 		      "Putting io_channel %p for io_device %s (%p) on thread %s refcnt %u\n",
596 		      ch, ch->dev->name, ch->dev->io_device, ch->thread->name, ch->ref);
597 
598 	ch->ref--;
599 
600 	if (ch->ref == 0) {
601 		spdk_thread_send_msg(ch->thread, _spdk_put_io_channel, ch);
602 	}
603 }
604 
605 struct spdk_io_channel *
606 spdk_io_channel_from_ctx(void *ctx)
607 {
608 	return (struct spdk_io_channel *)((uint8_t *)ctx - sizeof(struct spdk_io_channel));
609 }
610 
611 struct spdk_thread *
612 spdk_io_channel_get_thread(struct spdk_io_channel *ch)
613 {
614 	return ch->thread;
615 }
616 
617 struct spdk_io_channel_iter {
618 	void *io_device;
619 	struct io_device *dev;
620 	spdk_channel_msg fn;
621 	int status;
622 	void *ctx;
623 	struct spdk_io_channel *ch;
624 
625 	struct spdk_thread *cur_thread;
626 
627 	struct spdk_thread *orig_thread;
628 	spdk_channel_for_each_cpl cpl;
629 };
630 
631 void *
632 spdk_io_channel_iter_get_io_device(struct spdk_io_channel_iter *i)
633 {
634 	return i->io_device;
635 }
636 
637 struct spdk_io_channel *
638 spdk_io_channel_iter_get_channel(struct spdk_io_channel_iter *i)
639 {
640 	return i->ch;
641 }
642 
643 void *
644 spdk_io_channel_iter_get_ctx(struct spdk_io_channel_iter *i)
645 {
646 	return i->ctx;
647 }
648 
649 static void
650 _call_completion(void *ctx)
651 {
652 	struct spdk_io_channel_iter *i = ctx;
653 
654 	if (i->cpl != NULL) {
655 		i->cpl(i, i->status);
656 	}
657 	free(i);
658 }
659 
660 static void
661 _call_channel(void *ctx)
662 {
663 	struct spdk_io_channel_iter *i = ctx;
664 	struct spdk_io_channel *ch;
665 
666 	/*
667 	 * It is possible that the channel was deleted before this
668 	 *  message had a chance to execute.  If so, skip calling
669 	 *  the fn() on this thread.
670 	 */
671 	pthread_mutex_lock(&g_devlist_mutex);
672 	TAILQ_FOREACH(ch, &i->cur_thread->io_channels, tailq) {
673 		if (ch->dev->io_device == i->io_device) {
674 			break;
675 		}
676 	}
677 	pthread_mutex_unlock(&g_devlist_mutex);
678 
679 	if (ch) {
680 		i->fn(i);
681 	} else {
682 		spdk_for_each_channel_continue(i, 0);
683 	}
684 }
685 
686 void
687 spdk_for_each_channel(void *io_device, spdk_channel_msg fn, void *ctx,
688 		      spdk_channel_for_each_cpl cpl)
689 {
690 	struct spdk_thread *thread;
691 	struct spdk_io_channel *ch;
692 	struct spdk_io_channel_iter *i;
693 
694 	i = calloc(1, sizeof(*i));
695 	if (!i) {
696 		SPDK_ERRLOG("Unable to allocate iterator\n");
697 		return;
698 	}
699 
700 	i->io_device = io_device;
701 	i->fn = fn;
702 	i->ctx = ctx;
703 	i->cpl = cpl;
704 
705 	pthread_mutex_lock(&g_devlist_mutex);
706 	i->orig_thread = _get_thread();
707 
708 	TAILQ_FOREACH(thread, &g_threads, tailq) {
709 		TAILQ_FOREACH(ch, &thread->io_channels, tailq) {
710 			if (ch->dev->io_device == io_device) {
711 				ch->dev->for_each_count++;
712 				i->dev = ch->dev;
713 				i->cur_thread = thread;
714 				i->ch = ch;
715 				pthread_mutex_unlock(&g_devlist_mutex);
716 				spdk_thread_send_msg(thread, _call_channel, i);
717 				return;
718 			}
719 		}
720 	}
721 
722 	pthread_mutex_unlock(&g_devlist_mutex);
723 
724 	spdk_thread_send_msg(i->orig_thread, _call_completion, i);
725 }
726 
727 void
728 spdk_for_each_channel_continue(struct spdk_io_channel_iter *i, int status)
729 {
730 	struct spdk_thread *thread;
731 	struct spdk_io_channel *ch;
732 
733 	assert(i->cur_thread == spdk_get_thread());
734 
735 	i->status = status;
736 
737 	pthread_mutex_lock(&g_devlist_mutex);
738 	if (status) {
739 		goto end;
740 	}
741 	thread = TAILQ_NEXT(i->cur_thread, tailq);
742 	while (thread) {
743 		TAILQ_FOREACH(ch, &thread->io_channels, tailq) {
744 			if (ch->dev->io_device == i->io_device) {
745 				i->cur_thread = thread;
746 				i->ch = ch;
747 				pthread_mutex_unlock(&g_devlist_mutex);
748 				spdk_thread_send_msg(thread, _call_channel, i);
749 				return;
750 			}
751 		}
752 		thread = TAILQ_NEXT(thread, tailq);
753 	}
754 
755 end:
756 	i->dev->for_each_count--;
757 	i->ch = NULL;
758 	pthread_mutex_unlock(&g_devlist_mutex);
759 
760 	spdk_thread_send_msg(i->orig_thread, _call_completion, i);
761 }
762 
763 
764 SPDK_LOG_REGISTER_COMPONENT("thread", SPDK_LOG_THREAD)
765