xref: /spdk/test/unit/lib/thread/thread.c/thread_ut.c (revision 7961de43413e7f818f7499bf8518909beb59c82f)
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_cunit.h"
37 
38 #include "spdk_internal/thread.h"
39 
40 #include "thread/thread.c"
41 #include "common/lib/ut_multithread.c"
42 
43 static int g_sched_rc = 0;
44 
45 static int
46 _thread_schedule(struct spdk_thread *thread)
47 {
48 	return g_sched_rc;
49 }
50 
51 static void
52 thread_alloc(void)
53 {
54 	struct spdk_thread *thread;
55 
56 	/* No schedule callback */
57 	spdk_thread_lib_init(NULL, 0);
58 	thread = spdk_thread_create(NULL, NULL);
59 	SPDK_CU_ASSERT_FATAL(thread != NULL);
60 	spdk_set_thread(thread);
61 	spdk_thread_exit(thread);
62 	spdk_thread_destroy(thread);
63 	spdk_thread_lib_fini();
64 
65 	/* Schedule callback exists */
66 	spdk_thread_lib_init(_thread_schedule, 0);
67 
68 	/* Scheduling succeeds */
69 	g_sched_rc = 0;
70 	thread = spdk_thread_create(NULL, NULL);
71 	SPDK_CU_ASSERT_FATAL(thread != NULL);
72 	spdk_set_thread(thread);
73 	spdk_thread_exit(thread);
74 	spdk_thread_destroy(thread);
75 
76 	/* Scheduling fails */
77 	g_sched_rc = -1;
78 	thread = spdk_thread_create(NULL, NULL);
79 	SPDK_CU_ASSERT_FATAL(thread == NULL);
80 
81 	spdk_thread_lib_fini();
82 }
83 
84 static void
85 send_msg_cb(void *ctx)
86 {
87 	bool *done = ctx;
88 
89 	*done = true;
90 }
91 
92 static void
93 thread_send_msg(void)
94 {
95 	struct spdk_thread *thread0;
96 	bool done = false;
97 
98 	allocate_threads(2);
99 	set_thread(0);
100 	thread0 = spdk_get_thread();
101 
102 	set_thread(1);
103 	/* Simulate thread 1 sending a message to thread 0. */
104 	spdk_thread_send_msg(thread0, send_msg_cb, &done);
105 
106 	/* We have not polled thread 0 yet, so done should be false. */
107 	CU_ASSERT(!done);
108 
109 	/*
110 	 * Poll thread 1.  The message was sent to thread 0, so this should be
111 	 *  a nop and done should still be false.
112 	 */
113 	poll_thread(1);
114 	CU_ASSERT(!done);
115 
116 	/*
117 	 * Poll thread 0.  This should execute the message and done should then
118 	 *  be true.
119 	 */
120 	poll_thread(0);
121 	CU_ASSERT(done);
122 
123 	free_threads();
124 }
125 
126 static int
127 poller_run_done(void *ctx)
128 {
129 	bool	*poller_run = ctx;
130 
131 	*poller_run = true;
132 
133 	return -1;
134 }
135 
136 static void
137 thread_poller(void)
138 {
139 	struct spdk_poller	*poller = NULL;
140 	bool			poller_run = false;
141 
142 	allocate_threads(1);
143 
144 	set_thread(0);
145 	MOCK_SET(spdk_get_ticks, 0);
146 	/* Register a poller with no-wait time and test execution */
147 	poller = spdk_poller_register(poller_run_done, &poller_run, 0);
148 	CU_ASSERT(poller != NULL);
149 
150 	poll_threads();
151 	CU_ASSERT(poller_run == true);
152 
153 	spdk_poller_unregister(&poller);
154 	CU_ASSERT(poller == NULL);
155 
156 	/* Register a poller with 1000us wait time and test single execution */
157 	poller_run = false;
158 	poller = spdk_poller_register(poller_run_done, &poller_run, 1000);
159 	CU_ASSERT(poller != NULL);
160 
161 	poll_threads();
162 	CU_ASSERT(poller_run == false);
163 
164 	spdk_delay_us(1000);
165 	poll_threads();
166 	CU_ASSERT(poller_run == true);
167 
168 	poller_run = false;
169 	poll_threads();
170 	CU_ASSERT(poller_run == false);
171 
172 	spdk_delay_us(1000);
173 	poll_threads();
174 	CU_ASSERT(poller_run == true);
175 
176 	spdk_poller_unregister(&poller);
177 	CU_ASSERT(poller == NULL);
178 
179 	free_threads();
180 }
181 
182 struct poller_ctx {
183 	struct spdk_poller	*poller;
184 	bool			run;
185 };
186 
187 static int
188 poller_run_pause(void *ctx)
189 {
190 	struct poller_ctx *poller_ctx = ctx;
191 
192 	poller_ctx->run = true;
193 	spdk_poller_pause(poller_ctx->poller);
194 
195 	return 0;
196 }
197 
198 static void
199 poller_msg_pause_cb(void *ctx)
200 {
201 	struct spdk_poller *poller = ctx;
202 
203 	spdk_poller_pause(poller);
204 }
205 
206 static void
207 poller_msg_resume_cb(void *ctx)
208 {
209 	struct spdk_poller *poller = ctx;
210 
211 	spdk_poller_resume(poller);
212 }
213 
214 static void
215 poller_pause(void)
216 {
217 	struct poller_ctx poller_ctx = {};
218 	unsigned int delay[] = { 0, 1000 };
219 	unsigned int i;
220 
221 	allocate_threads(1);
222 	set_thread(0);
223 
224 	/* Register a poller that pauses itself */
225 	poller_ctx.poller = spdk_poller_register(poller_run_pause, &poller_ctx, 0);
226 	CU_ASSERT_PTR_NOT_NULL(poller_ctx.poller);
227 
228 	poller_ctx.run = false;
229 	poll_threads();
230 	CU_ASSERT_EQUAL(poller_ctx.run, true);
231 
232 	poller_ctx.run = false;
233 	poll_threads();
234 	CU_ASSERT_EQUAL(poller_ctx.run, false);
235 
236 	spdk_poller_unregister(&poller_ctx.poller);
237 	CU_ASSERT_PTR_NULL(poller_ctx.poller);
238 
239 	/* Verify that resuming an unpaused poller doesn't do anything */
240 	poller_ctx.poller = spdk_poller_register(poller_run_done, &poller_ctx.run, 0);
241 	CU_ASSERT_PTR_NOT_NULL(poller_ctx.poller);
242 
243 	spdk_poller_resume(poller_ctx.poller);
244 
245 	poller_ctx.run = false;
246 	poll_threads();
247 	CU_ASSERT_EQUAL(poller_ctx.run, true);
248 
249 	/* Verify that pausing the same poller twice works too */
250 	spdk_poller_pause(poller_ctx.poller);
251 
252 	poller_ctx.run = false;
253 	poll_threads();
254 	CU_ASSERT_EQUAL(poller_ctx.run, false);
255 
256 	spdk_poller_pause(poller_ctx.poller);
257 	poll_threads();
258 	CU_ASSERT_EQUAL(poller_ctx.run, false);
259 
260 	spdk_poller_resume(poller_ctx.poller);
261 	poll_threads();
262 	CU_ASSERT_EQUAL(poller_ctx.run, true);
263 
264 	/* Verify that a poller is run when it's resumed immediately after pausing */
265 	poller_ctx.run = false;
266 	spdk_poller_pause(poller_ctx.poller);
267 	spdk_poller_resume(poller_ctx.poller);
268 	poll_threads();
269 	CU_ASSERT_EQUAL(poller_ctx.run, true);
270 
271 	spdk_poller_unregister(&poller_ctx.poller);
272 	CU_ASSERT_PTR_NULL(poller_ctx.poller);
273 
274 	/* Poll the thread to make sure the previous poller gets unregistered */
275 	poll_threads();
276 	CU_ASSERT_EQUAL(spdk_thread_has_pollers(spdk_get_thread()), false);
277 
278 	/* Verify that it's possible to unregister a paused poller */
279 	poller_ctx.poller = spdk_poller_register(poller_run_done, &poller_ctx.run, 0);
280 	CU_ASSERT_PTR_NOT_NULL(poller_ctx.poller);
281 
282 	poller_ctx.run = false;
283 	poll_threads();
284 	CU_ASSERT_EQUAL(poller_ctx.run, true);
285 
286 	spdk_poller_pause(poller_ctx.poller);
287 
288 	poller_ctx.run = false;
289 	poll_threads();
290 	CU_ASSERT_EQUAL(poller_ctx.run, false);
291 
292 	spdk_poller_unregister(&poller_ctx.poller);
293 
294 	poll_threads();
295 	CU_ASSERT_EQUAL(poller_ctx.run, false);
296 	CU_ASSERT_EQUAL(spdk_thread_has_pollers(spdk_get_thread()), false);
297 
298 	/* Register pollers with 0 and 1000us wait time and pause/resume them */
299 	for (i = 0; i < SPDK_COUNTOF(delay); ++i) {
300 		poller_ctx.poller = spdk_poller_register(poller_run_done, &poller_ctx.run, delay[i]);
301 		CU_ASSERT_PTR_NOT_NULL(poller_ctx.poller);
302 
303 		spdk_delay_us(delay[i]);
304 		poller_ctx.run = false;
305 		poll_threads();
306 		CU_ASSERT_EQUAL(poller_ctx.run, true);
307 
308 		spdk_poller_pause(poller_ctx.poller);
309 
310 		spdk_delay_us(delay[i]);
311 		poller_ctx.run = false;
312 		poll_threads();
313 		CU_ASSERT_EQUAL(poller_ctx.run, false);
314 
315 		spdk_poller_resume(poller_ctx.poller);
316 
317 		spdk_delay_us(delay[i]);
318 		poll_threads();
319 		CU_ASSERT_EQUAL(poller_ctx.run, true);
320 
321 		/* Verify that the poller can be paused/resumed from spdk_thread_send_msg */
322 		spdk_thread_send_msg(spdk_get_thread(), poller_msg_pause_cb, poller_ctx.poller);
323 
324 		spdk_delay_us(delay[i]);
325 		poller_ctx.run = false;
326 		poll_threads();
327 		CU_ASSERT_EQUAL(poller_ctx.run, false);
328 
329 		spdk_thread_send_msg(spdk_get_thread(), poller_msg_resume_cb, poller_ctx.poller);
330 
331 		poll_threads();
332 		if (delay[i] > 0) {
333 			spdk_delay_us(delay[i]);
334 			poll_threads();
335 		}
336 		CU_ASSERT_EQUAL(poller_ctx.run, true);
337 
338 		spdk_poller_unregister(&poller_ctx.poller);
339 		CU_ASSERT_PTR_NULL(poller_ctx.poller);
340 	}
341 
342 	free_threads();
343 }
344 
345 static void
346 for_each_cb(void *ctx)
347 {
348 	int *count = ctx;
349 
350 	(*count)++;
351 }
352 
353 static void
354 thread_for_each(void)
355 {
356 	int count = 0;
357 	int i;
358 
359 	allocate_threads(3);
360 	set_thread(0);
361 
362 	spdk_for_each_thread(for_each_cb, &count, for_each_cb);
363 
364 	/* We have not polled thread 0 yet, so count should be 0 */
365 	CU_ASSERT(count == 0);
366 
367 	/* Poll each thread to verify the message is passed to each */
368 	for (i = 0; i < 3; i++) {
369 		poll_thread(i);
370 		CU_ASSERT(count == (i + 1));
371 	}
372 
373 	/*
374 	 * After each thread is called, the completion calls it
375 	 * one more time.
376 	 */
377 	poll_thread(0);
378 	CU_ASSERT(count == 4);
379 
380 	free_threads();
381 }
382 
383 static int
384 channel_create(void *io_device, void *ctx_buf)
385 {
386 	return 0;
387 }
388 
389 static void
390 channel_destroy(void *io_device, void *ctx_buf)
391 {
392 }
393 
394 static void
395 channel_msg(struct spdk_io_channel_iter *i)
396 {
397 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
398 	int *count = spdk_io_channel_get_ctx(ch);
399 
400 	(*count)++;
401 
402 	spdk_for_each_channel_continue(i, 0);
403 }
404 
405 static void
406 channel_cpl(struct spdk_io_channel_iter *i, int status)
407 {
408 }
409 
410 static void
411 for_each_channel_remove(void)
412 {
413 	struct spdk_io_channel *ch0, *ch1, *ch2;
414 	int io_target;
415 	int count = 0;
416 
417 	allocate_threads(3);
418 	set_thread(0);
419 	spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL);
420 	ch0 = spdk_get_io_channel(&io_target);
421 	set_thread(1);
422 	ch1 = spdk_get_io_channel(&io_target);
423 	set_thread(2);
424 	ch2 = spdk_get_io_channel(&io_target);
425 
426 	/*
427 	 * Test that io_channel handles the case where we start to iterate through
428 	 *  the channels, and during the iteration, one of the channels is deleted.
429 	 * This is done in some different and sometimes non-intuitive orders, because
430 	 *  some operations are deferred and won't execute until their threads are
431 	 *  polled.
432 	 *
433 	 * Case #1: Put the I/O channel before spdk_for_each_channel.
434 	 */
435 	set_thread(0);
436 	spdk_put_io_channel(ch0);
437 	poll_threads();
438 	spdk_for_each_channel(&io_target, channel_msg, &count, channel_cpl);
439 	poll_threads();
440 
441 	/*
442 	 * Case #2: Put the I/O channel after spdk_for_each_channel, but before
443 	 *  thread 0 is polled.
444 	 */
445 	ch0 = spdk_get_io_channel(&io_target);
446 	spdk_for_each_channel(&io_target, channel_msg, &count, channel_cpl);
447 	spdk_put_io_channel(ch0);
448 	poll_threads();
449 
450 	set_thread(1);
451 	spdk_put_io_channel(ch1);
452 	set_thread(2);
453 	spdk_put_io_channel(ch2);
454 	spdk_io_device_unregister(&io_target, NULL);
455 	poll_threads();
456 
457 	free_threads();
458 }
459 
460 struct unreg_ctx {
461 	bool	ch_done;
462 	bool	foreach_done;
463 };
464 
465 static void
466 unreg_ch_done(struct spdk_io_channel_iter *i)
467 {
468 	struct unreg_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
469 
470 	ctx->ch_done = true;
471 
472 	SPDK_CU_ASSERT_FATAL(i->cur_thread != NULL);
473 	spdk_for_each_channel_continue(i, 0);
474 }
475 
476 static void
477 unreg_foreach_done(struct spdk_io_channel_iter *i, int status)
478 {
479 	struct unreg_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
480 
481 	ctx->foreach_done = true;
482 }
483 
484 static void
485 for_each_channel_unreg(void)
486 {
487 	struct spdk_io_channel *ch0;
488 	struct io_device *dev;
489 	struct unreg_ctx ctx = {};
490 	int io_target;
491 
492 	allocate_threads(1);
493 	set_thread(0);
494 	CU_ASSERT(TAILQ_EMPTY(&g_io_devices));
495 	spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL);
496 	CU_ASSERT(!TAILQ_EMPTY(&g_io_devices));
497 	dev = TAILQ_FIRST(&g_io_devices);
498 	SPDK_CU_ASSERT_FATAL(dev != NULL);
499 	CU_ASSERT(TAILQ_NEXT(dev, tailq) == NULL);
500 	ch0 = spdk_get_io_channel(&io_target);
501 	spdk_for_each_channel(&io_target, unreg_ch_done, &ctx, unreg_foreach_done);
502 
503 	spdk_io_device_unregister(&io_target, NULL);
504 	/*
505 	 * There is an outstanding foreach call on the io_device, so the unregister should not
506 	 *  have removed the device.
507 	 */
508 	CU_ASSERT(dev == TAILQ_FIRST(&g_io_devices));
509 	spdk_io_device_register(&io_target, channel_create, channel_destroy, sizeof(int), NULL);
510 	/*
511 	 * There is already a device registered at &io_target, so a new io_device should not
512 	 *  have been added to g_io_devices.
513 	 */
514 	CU_ASSERT(dev == TAILQ_FIRST(&g_io_devices));
515 	CU_ASSERT(TAILQ_NEXT(dev, tailq) == NULL);
516 
517 	poll_thread(0);
518 	CU_ASSERT(ctx.ch_done == true);
519 	CU_ASSERT(ctx.foreach_done == true);
520 	/*
521 	 * There are no more foreach operations outstanding, so we can unregister the device,
522 	 *  even though a channel still exists for the device.
523 	 */
524 	spdk_io_device_unregister(&io_target, NULL);
525 	CU_ASSERT(TAILQ_EMPTY(&g_io_devices));
526 
527 	set_thread(0);
528 	spdk_put_io_channel(ch0);
529 
530 	poll_threads();
531 
532 	free_threads();
533 }
534 
535 static void
536 thread_name(void)
537 {
538 	struct spdk_thread *thread;
539 	const char *name;
540 
541 	spdk_thread_lib_init(NULL, 0);
542 
543 	/* Create thread with no name, which automatically generates one */
544 	thread = spdk_thread_create(NULL, NULL);
545 	spdk_set_thread(thread);
546 	thread = spdk_get_thread();
547 	SPDK_CU_ASSERT_FATAL(thread != NULL);
548 	name = spdk_thread_get_name(thread);
549 	CU_ASSERT(name != NULL);
550 	spdk_thread_exit(thread);
551 	spdk_thread_destroy(thread);
552 
553 	/* Create thread named "test_thread" */
554 	thread = spdk_thread_create("test_thread", NULL);
555 	spdk_set_thread(thread);
556 	thread = spdk_get_thread();
557 	SPDK_CU_ASSERT_FATAL(thread != NULL);
558 	name = spdk_thread_get_name(thread);
559 	SPDK_CU_ASSERT_FATAL(name != NULL);
560 	CU_ASSERT(strcmp(name, "test_thread") == 0);
561 	spdk_thread_exit(thread);
562 	spdk_thread_destroy(thread);
563 
564 	spdk_thread_lib_fini();
565 }
566 
567 static uint64_t device1;
568 static uint64_t device2;
569 static uint64_t device3;
570 
571 static uint64_t ctx1 = 0x1111;
572 static uint64_t ctx2 = 0x2222;
573 
574 static int g_create_cb_calls = 0;
575 static int g_destroy_cb_calls = 0;
576 
577 static int
578 create_cb_1(void *io_device, void *ctx_buf)
579 {
580 	CU_ASSERT(io_device == &device1);
581 	*(uint64_t *)ctx_buf = ctx1;
582 	g_create_cb_calls++;
583 	return 0;
584 }
585 
586 static void
587 destroy_cb_1(void *io_device, void *ctx_buf)
588 {
589 	CU_ASSERT(io_device == &device1);
590 	CU_ASSERT(*(uint64_t *)ctx_buf == ctx1);
591 	g_destroy_cb_calls++;
592 }
593 
594 static int
595 create_cb_2(void *io_device, void *ctx_buf)
596 {
597 	CU_ASSERT(io_device == &device2);
598 	*(uint64_t *)ctx_buf = ctx2;
599 	g_create_cb_calls++;
600 	return 0;
601 }
602 
603 static void
604 destroy_cb_2(void *io_device, void *ctx_buf)
605 {
606 	CU_ASSERT(io_device == &device2);
607 	CU_ASSERT(*(uint64_t *)ctx_buf == ctx2);
608 	g_destroy_cb_calls++;
609 }
610 
611 static void
612 channel(void)
613 {
614 	struct spdk_io_channel *ch1, *ch2;
615 	void *ctx;
616 
617 	allocate_threads(1);
618 	set_thread(0);
619 
620 	spdk_io_device_register(&device1, create_cb_1, destroy_cb_1, sizeof(ctx1), NULL);
621 	spdk_io_device_register(&device2, create_cb_2, destroy_cb_2, sizeof(ctx2), NULL);
622 
623 	g_create_cb_calls = 0;
624 	ch1 = spdk_get_io_channel(&device1);
625 	CU_ASSERT(g_create_cb_calls == 1);
626 	SPDK_CU_ASSERT_FATAL(ch1 != NULL);
627 
628 	g_create_cb_calls = 0;
629 	ch2 = spdk_get_io_channel(&device1);
630 	CU_ASSERT(g_create_cb_calls == 0);
631 	CU_ASSERT(ch1 == ch2);
632 	SPDK_CU_ASSERT_FATAL(ch2 != NULL);
633 
634 	g_destroy_cb_calls = 0;
635 	spdk_put_io_channel(ch2);
636 	poll_threads();
637 	CU_ASSERT(g_destroy_cb_calls == 0);
638 
639 	g_create_cb_calls = 0;
640 	ch2 = spdk_get_io_channel(&device2);
641 	CU_ASSERT(g_create_cb_calls == 1);
642 	CU_ASSERT(ch1 != ch2);
643 	SPDK_CU_ASSERT_FATAL(ch2 != NULL);
644 
645 	ctx = spdk_io_channel_get_ctx(ch2);
646 	CU_ASSERT(*(uint64_t *)ctx == ctx2);
647 
648 	g_destroy_cb_calls = 0;
649 	spdk_put_io_channel(ch1);
650 	poll_threads();
651 	CU_ASSERT(g_destroy_cb_calls == 1);
652 
653 	g_destroy_cb_calls = 0;
654 	spdk_put_io_channel(ch2);
655 	poll_threads();
656 	CU_ASSERT(g_destroy_cb_calls == 1);
657 
658 	ch1 = spdk_get_io_channel(&device3);
659 	CU_ASSERT(ch1 == NULL);
660 
661 	spdk_io_device_unregister(&device1, NULL);
662 	poll_threads();
663 	spdk_io_device_unregister(&device2, NULL);
664 	poll_threads();
665 	CU_ASSERT(TAILQ_EMPTY(&g_io_devices));
666 	free_threads();
667 	CU_ASSERT(TAILQ_EMPTY(&g_threads));
668 }
669 
670 static int
671 create_cb(void *io_device, void *ctx_buf)
672 {
673 	uint64_t *refcnt = (uint64_t *)ctx_buf;
674 
675 	CU_ASSERT(*refcnt == 0);
676 	*refcnt = 1;
677 
678 	return 0;
679 }
680 
681 static void
682 destroy_cb(void *io_device, void *ctx_buf)
683 {
684 	uint64_t *refcnt = (uint64_t *)ctx_buf;
685 
686 	CU_ASSERT(*refcnt == 1);
687 	*refcnt = 0;
688 }
689 
690 /**
691  * This test is checking that a sequence of get, put, get, put without allowing
692  * the deferred put operation to complete doesn't result in releasing the memory
693  * for the channel twice.
694  */
695 static void
696 channel_destroy_races(void)
697 {
698 	uint64_t device;
699 	struct spdk_io_channel *ch;
700 
701 	allocate_threads(1);
702 	set_thread(0);
703 
704 	spdk_io_device_register(&device, create_cb, destroy_cb, sizeof(uint64_t), NULL);
705 
706 	ch = spdk_get_io_channel(&device);
707 	SPDK_CU_ASSERT_FATAL(ch != NULL);
708 
709 	spdk_put_io_channel(ch);
710 
711 	ch = spdk_get_io_channel(&device);
712 	SPDK_CU_ASSERT_FATAL(ch != NULL);
713 
714 	spdk_put_io_channel(ch);
715 	poll_threads();
716 
717 	spdk_io_device_unregister(&device, NULL);
718 	poll_threads();
719 
720 	CU_ASSERT(TAILQ_EMPTY(&g_io_devices));
721 	free_threads();
722 	CU_ASSERT(TAILQ_EMPTY(&g_threads));
723 }
724 
725 int
726 main(int argc, char **argv)
727 {
728 	CU_pSuite	suite = NULL;
729 	unsigned int	num_failures;
730 
731 	if (CU_initialize_registry() != CUE_SUCCESS) {
732 		return CU_get_error();
733 	}
734 
735 	suite = CU_add_suite("io_channel", NULL, NULL);
736 	if (suite == NULL) {
737 		CU_cleanup_registry();
738 		return CU_get_error();
739 	}
740 
741 	if (
742 		CU_add_test(suite, "thread_alloc", thread_alloc) == NULL ||
743 		CU_add_test(suite, "thread_send_msg", thread_send_msg) == NULL ||
744 		CU_add_test(suite, "thread_poller", thread_poller) == NULL ||
745 		CU_add_test(suite, "poller_pause", poller_pause) == NULL ||
746 		CU_add_test(suite, "thread_for_each", thread_for_each) == NULL ||
747 		CU_add_test(suite, "for_each_channel_remove", for_each_channel_remove) == NULL ||
748 		CU_add_test(suite, "for_each_channel_unreg", for_each_channel_unreg) == NULL ||
749 		CU_add_test(suite, "thread_name", thread_name) == NULL ||
750 		CU_add_test(suite, "channel", channel) == NULL ||
751 		CU_add_test(suite, "channel_destroy_races", channel_destroy_races) == NULL
752 	) {
753 		CU_cleanup_registry();
754 		return CU_get_error();
755 	}
756 
757 	CU_basic_set_mode(CU_BRM_VERBOSE);
758 	CU_basic_run_tests();
759 	num_failures = CU_get_number_of_failures();
760 	CU_cleanup_registry();
761 	return num_failures;
762 }
763