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_cunit.h" 35 #include "spdk/thread.h" 36 #include "spdk_internal/mock.h" 37 #include "spdk_internal/thread.h" 38 39 #include "common/lib/test_env.c" 40 41 static uint32_t g_ut_num_threads; 42 43 int allocate_threads(int num_threads); 44 void free_threads(void); 45 void poll_threads(void); 46 bool poll_thread(uintptr_t thread_id); 47 48 struct ut_msg { 49 spdk_msg_fn fn; 50 void *ctx; 51 TAILQ_ENTRY(ut_msg) link; 52 }; 53 54 struct ut_thread { 55 struct spdk_thread *thread; 56 struct spdk_io_channel *ch; 57 }; 58 59 struct ut_thread *g_ut_threads; 60 61 #define INVALID_THREAD 0x1000 62 63 static uint64_t g_thread_id = INVALID_THREAD; 64 65 static void 66 set_thread(uintptr_t thread_id) 67 { 68 g_thread_id = thread_id; 69 if (thread_id == INVALID_THREAD) { 70 spdk_set_thread(NULL); 71 } else { 72 spdk_set_thread(g_ut_threads[thread_id].thread); 73 } 74 75 } 76 77 int 78 allocate_threads(int num_threads) 79 { 80 struct spdk_thread *thread; 81 uint32_t i; 82 83 g_ut_num_threads = num_threads; 84 85 g_ut_threads = calloc(num_threads, sizeof(*g_ut_threads)); 86 assert(g_ut_threads != NULL); 87 88 for (i = 0; i < g_ut_num_threads; i++) { 89 set_thread(i); 90 thread = spdk_allocate_thread(NULL, NULL, NULL, NULL, NULL); 91 assert(thread != NULL); 92 g_ut_threads[i].thread = thread; 93 } 94 95 set_thread(INVALID_THREAD); 96 return 0; 97 } 98 99 void 100 free_threads(void) 101 { 102 uint32_t i; 103 104 for (i = 0; i < g_ut_num_threads; i++) { 105 set_thread(i); 106 spdk_free_thread(); 107 } 108 109 g_ut_num_threads = 0; 110 free(g_ut_threads); 111 g_ut_threads = NULL; 112 } 113 114 bool 115 poll_thread(uintptr_t thread_id) 116 { 117 bool busy = false; 118 struct ut_thread *thread = &g_ut_threads[thread_id]; 119 uintptr_t original_thread_id; 120 121 assert(thread_id != (uintptr_t)INVALID_THREAD); 122 assert(thread_id < g_ut_num_threads); 123 124 original_thread_id = g_thread_id; 125 set_thread(INVALID_THREAD); 126 127 while (spdk_thread_poll(thread->thread, 0) > 0) { 128 busy = true; 129 } 130 131 set_thread(original_thread_id); 132 133 return busy; 134 } 135 136 void 137 poll_threads(void) 138 { 139 while (true) { 140 bool busy = false; 141 142 for (uint32_t i = 0; i < g_ut_num_threads; i++) { 143 busy = busy || poll_thread(i); 144 } 145 146 if (!busy) { 147 break; 148 } 149 } 150 } 151