2017-09-11 20:12:09 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk_cunit.h"
|
2018-06-11 20:32:15 +00:00
|
|
|
#include "spdk/thread.h"
|
2017-09-11 20:12:09 +00:00
|
|
|
#include "spdk_internal/mock.h"
|
2018-09-17 23:18:53 +00:00
|
|
|
#include "spdk_internal/thread.h"
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
#include "common/lib/test_env.c"
|
|
|
|
|
2017-09-11 20:12:09 +00:00
|
|
|
static uint32_t g_ut_num_threads;
|
|
|
|
|
|
|
|
int allocate_threads(int num_threads);
|
|
|
|
void free_threads(void);
|
|
|
|
void poll_threads(void);
|
2018-08-31 17:58:36 +00:00
|
|
|
bool poll_thread(uintptr_t thread_id);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
struct ut_msg {
|
2018-10-10 21:05:04 +00:00
|
|
|
spdk_msg_fn fn;
|
2017-09-11 20:12:09 +00:00
|
|
|
void *ctx;
|
|
|
|
TAILQ_ENTRY(ut_msg) link;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ut_thread {
|
|
|
|
struct spdk_thread *thread;
|
|
|
|
struct spdk_io_channel *ch;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ut_thread *g_ut_threads;
|
|
|
|
|
2018-07-10 17:47:14 +00:00
|
|
|
#define INVALID_THREAD 0x1000
|
|
|
|
|
2018-12-20 17:58:01 +00:00
|
|
|
static uint64_t g_thread_id = INVALID_THREAD;
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_thread(uintptr_t thread_id)
|
|
|
|
{
|
|
|
|
g_thread_id = thread_id;
|
2018-07-10 17:47:14 +00:00
|
|
|
if (thread_id == INVALID_THREAD) {
|
2018-09-17 23:18:53 +00:00
|
|
|
spdk_set_thread(NULL);
|
2018-07-10 17:47:14 +00:00
|
|
|
} else {
|
2018-09-17 23:18:53 +00:00
|
|
|
spdk_set_thread(g_ut_threads[thread_id].thread);
|
2018-07-10 17:47:14 +00:00
|
|
|
}
|
2018-09-17 23:18:53 +00:00
|
|
|
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
allocate_threads(int num_threads)
|
|
|
|
{
|
|
|
|
struct spdk_thread *thread;
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
g_ut_num_threads = num_threads;
|
|
|
|
|
|
|
|
g_ut_threads = calloc(num_threads, sizeof(*g_ut_threads));
|
2018-12-17 18:30:13 +00:00
|
|
|
assert(g_ut_threads != NULL);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
for (i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
set_thread(i);
|
2019-04-22 20:29:06 +00:00
|
|
|
thread = spdk_thread_create(NULL, NULL);
|
2018-12-17 18:30:13 +00:00
|
|
|
assert(thread != NULL);
|
2017-09-11 20:12:09 +00:00
|
|
|
g_ut_threads[i].thread = thread;
|
|
|
|
}
|
|
|
|
|
2018-07-10 17:47:14 +00:00
|
|
|
set_thread(INVALID_THREAD);
|
2017-09-11 20:12:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_threads(void)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
set_thread(i);
|
2019-01-15 20:54:51 +00:00
|
|
|
spdk_thread_exit(g_ut_threads[i].thread);
|
2019-01-15 20:45:59 +00:00
|
|
|
g_ut_threads[i].thread = NULL;
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_ut_num_threads = 0;
|
|
|
|
free(g_ut_threads);
|
|
|
|
g_ut_threads = NULL;
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
bool
|
2017-09-11 20:12:09 +00:00
|
|
|
poll_thread(uintptr_t thread_id)
|
|
|
|
{
|
2018-08-31 17:58:36 +00:00
|
|
|
bool busy = false;
|
2017-09-11 20:12:09 +00:00
|
|
|
struct ut_thread *thread = &g_ut_threads[thread_id];
|
|
|
|
uintptr_t original_thread_id;
|
|
|
|
|
2018-12-17 21:40:51 +00:00
|
|
|
assert(thread_id != (uintptr_t)INVALID_THREAD);
|
|
|
|
assert(thread_id < g_ut_num_threads);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
|
|
|
original_thread_id = g_thread_id;
|
2018-09-17 23:18:53 +00:00
|
|
|
set_thread(INVALID_THREAD);
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2019-02-06 20:19:39 +00:00
|
|
|
while (spdk_thread_poll(thread->thread, 0, 0) > 0) {
|
2018-08-31 17:58:36 +00:00
|
|
|
busy = true;
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_thread(original_thread_id);
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
return busy;
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
poll_threads(void)
|
|
|
|
{
|
|
|
|
while (true) {
|
2018-08-31 17:58:36 +00:00
|
|
|
bool busy = false;
|
2017-09-11 20:12:09 +00:00
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
for (uint32_t i = 0; i < g_ut_num_threads; i++) {
|
|
|
|
busy = busy || poll_thread(i);
|
2017-09-11 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 17:58:36 +00:00
|
|
|
if (!busy) {
|
2017-09-11 20:12:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|