blobfs: Simplify sync unit test

Rely on the available threading abstractions to make this
a bit simpler.

Change-Id: If8f028092c057637ff167d2ec7faa3dce009af61
Signed-off-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/449463
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Ben Walker 2019-03-26 14:50:55 -07:00 committed by Jim Harris
parent 1679104edb
commit 088c5e04ab

View File

@ -49,6 +49,7 @@
struct spdk_filesystem *g_fs; struct spdk_filesystem *g_fs;
struct spdk_file *g_file; struct spdk_file *g_file;
int g_fserrno; int g_fserrno;
struct spdk_thread *g_dispatch_thread = NULL;
/* Return NULL to test hardcoded defaults. */ /* Return NULL to test hardcoded defaults. */
struct spdk_conf_section * struct spdk_conf_section *
@ -68,27 +69,21 @@ struct ut_request {
fs_request_fn fn; fs_request_fn fn;
void *arg; void *arg;
volatile int done; volatile int done;
int from_ut;
}; };
static struct ut_request *g_req = NULL;
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
static void static void
send_request(fs_request_fn fn, void *arg) send_request(fs_request_fn fn, void *arg)
{ {
struct ut_request *req; spdk_thread_send_msg(g_dispatch_thread, (spdk_msg_fn)fn, arg);
}
req = calloc(1, sizeof(*req)); static void
assert(req != NULL); ut_call_fn(void *arg)
req->fn = fn; {
req->arg = arg; struct ut_request *req = arg;
req->done = 0;
req->from_ut = 0;
pthread_mutex_lock(&g_mutex); req->fn(req->arg);
g_req = req; req->done = 1;
pthread_mutex_unlock(&g_mutex);
} }
static void static void
@ -96,30 +91,14 @@ ut_send_request(fs_request_fn fn, void *arg)
{ {
struct ut_request req; struct ut_request req;
req.fn = fn; req.fn = fn;
req.arg = arg; req.arg = arg;
req.done = 0; req.done = 0;
req.from_ut = 1;
pthread_mutex_lock(&g_mutex); spdk_thread_send_msg(g_dispatch_thread, ut_call_fn, &req);
g_req = &req;
pthread_mutex_unlock(&g_mutex);
while (1) { /* Wait for this to finish */
pthread_mutex_lock(&g_mutex); while (req.done == 0) { }
if (req.done == 1) {
pthread_mutex_unlock(&g_mutex);
break;
}
pthread_mutex_unlock(&g_mutex);
}
/*
* Make sure the address of the local req variable is not in g_req when we exit this
* function to make static analysis tools happy.
*/
g_req = NULL;
} }
static void static void
@ -349,35 +328,22 @@ fs_delete_file_without_close(void)
} }
static bool g_thread_exit = false;
static void static void
terminate_spdk_thread(void *arg) terminate_spdk_thread(void *arg)
{ {
spdk_thread_exit(spdk_get_thread()); g_thread_exit = true;
pthread_exit(NULL);
} }
static void * static void *
spdk_thread(void *arg) spdk_thread(void *arg)
{ {
struct spdk_thread *thread; struct spdk_thread *thread = arg;
struct ut_request *req;
thread = spdk_thread_create("thread1");
spdk_set_thread(thread); spdk_set_thread(thread);
while (1) { while (!g_thread_exit) {
pthread_mutex_lock(&g_mutex);
if (g_req != NULL) {
req = g_req;
req->fn(req->arg);
req->done = 1;
if (!req->from_ut) {
free(req);
}
g_req = NULL;
}
pthread_mutex_unlock(&g_mutex);
spdk_thread_poll(thread, 0, 0); spdk_thread_poll(thread, 0, 0);
} }
@ -412,18 +378,33 @@ int main(int argc, char **argv)
return CU_get_error(); return CU_get_error();
} }
thread = spdk_thread_create("thread0"); spdk_thread_lib_init(NULL, 0);
thread = spdk_thread_create("test_thread");
spdk_set_thread(thread); spdk_set_thread(thread);
pthread_create(&spdk_tid, NULL, spdk_thread, NULL); g_dispatch_thread = spdk_thread_create("dispatch_thread");
pthread_create(&spdk_tid, NULL, spdk_thread, g_dispatch_thread);
g_dev_buffer = calloc(1, DEV_BUFFER_SIZE); g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests(); CU_basic_run_tests();
num_failures = CU_get_number_of_failures(); num_failures = CU_get_number_of_failures();
CU_cleanup_registry(); CU_cleanup_registry();
free(g_dev_buffer); free(g_dev_buffer);
send_request(terminate_spdk_thread, NULL);
ut_send_request(terminate_spdk_thread, NULL);
pthread_join(spdk_tid, NULL); pthread_join(spdk_tid, NULL);
while (spdk_thread_poll(g_dispatch_thread, 0, 0) > 0) {}
while (spdk_thread_poll(thread, 0, 0) > 0) {}
spdk_thread_exit(thread); spdk_thread_exit(thread);
spdk_thread_exit(g_dispatch_thread);
spdk_thread_lib_fini();
return num_failures; return num_failures;
} }