Commit 088c5e04 authored by Ben Walker's avatar Ben Walker Committed by Jim Harris
Browse files

blobfs: Simplify sync unit test



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

Change-Id: If8f028092c057637ff167d2ec7faa3dce009af61
Signed-off-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/449463


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent 1679104e
Loading
Loading
Loading
Loading
+38 −57
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@
struct spdk_filesystem *g_fs;
struct spdk_file *g_file;
int g_fserrno;
struct spdk_thread *g_dispatch_thread = NULL;

/* Return NULL to test hardcoded defaults. */
struct spdk_conf_section *
@@ -68,27 +69,21 @@ struct ut_request {
	fs_request_fn fn;
	void *arg;
	volatile int done;
	int from_ut;
};

static struct ut_request *g_req = NULL;
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

static void
send_request(fs_request_fn fn, void *arg)
{
	struct ut_request *req;

	req = calloc(1, sizeof(*req));
	assert(req != NULL);
	req->fn = fn;
	req->arg = arg;
	req->done = 0;
	req->from_ut = 0;

	pthread_mutex_lock(&g_mutex);
	g_req = req;
	pthread_mutex_unlock(&g_mutex);
	spdk_thread_send_msg(g_dispatch_thread, (spdk_msg_fn)fn, arg);
}

static void
ut_call_fn(void *arg)
{
	struct ut_request *req = arg;

	req->fn(req->arg);
	req->done = 1;
}

static void
@@ -96,30 +91,14 @@ ut_send_request(fs_request_fn fn, void *arg)
{
	struct ut_request req;


	req.fn = fn;
	req.arg = arg;
	req.done = 0;
	req.from_ut = 1;

	pthread_mutex_lock(&g_mutex);
	g_req = &req;
	pthread_mutex_unlock(&g_mutex);
	spdk_thread_send_msg(g_dispatch_thread, ut_call_fn, &req);

	while (1) {
		pthread_mutex_lock(&g_mutex);
		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;
	/* Wait for this to finish */
	while (req.done == 0) {	}
}

static void
@@ -349,35 +328,22 @@ fs_delete_file_without_close(void)

}

static bool g_thread_exit = false;

static void
terminate_spdk_thread(void *arg)
{
	spdk_thread_exit(spdk_get_thread());
	pthread_exit(NULL);
	g_thread_exit = true;
}

static void *
spdk_thread(void *arg)
{
	struct spdk_thread *thread;
	struct ut_request *req;
	struct spdk_thread *thread = arg;

	thread = spdk_thread_create("thread1");
	spdk_set_thread(thread);

	while (1) {
		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);

	while (!g_thread_exit) {
		spdk_thread_poll(thread, 0, 0);
	}

@@ -412,18 +378,33 @@ int main(int argc, char **argv)
		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);

	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);

	CU_basic_set_mode(CU_BRM_VERBOSE);
	CU_basic_run_tests();
	num_failures = CU_get_number_of_failures();
	CU_cleanup_registry();

	free(g_dev_buffer);
	send_request(terminate_spdk_thread, NULL);

	ut_send_request(terminate_spdk_thread, 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(g_dispatch_thread);

	spdk_thread_lib_fini();

	return num_failures;
}