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