[BACK]Return to t_pslist.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / tests / include / sys

File: [cvs.NetBSD.org] / src / tests / include / sys / t_pslist.c (download)

Revision 1.1, Sat Apr 9 04:39:47 2016 UTC (5 years ago) by riastradh
Branch: MAIN
CVS Tags: prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20191119, phil-wifi-20190609, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan
Branch point for: phil-wifi

Introduce pserialize-safe linked lists.

These are like LIST_* from queue(3), but issue the appropriate memory
barriers for pserialize readers and writers.

Discussed on tech-kern:

https://mail-index.netbsd.org/tech-kern/2016/04/03/msg020365.html

/*	$NetBSD: t_pslist.c,v 1.1 2016/04/09 04:39:47 riastradh Exp $	*/

/*-
 * Copyright (c) 2016 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Taylor R. Campbell.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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 <sys/pslist.h>

#include <atf-c.h>

/*
 * XXX This is a limited test to make sure the operations behave as
 * described on a sequential machine.  It does nothing to test the
 * pserialize-safety of any operations.
 */

ATF_TC(misc);
ATF_TC_HEAD(misc, tc)
{
	atf_tc_set_md_var(tc, "descr", "pserialize-safe list tests");
}
ATF_TC_BODY(misc, tc)
{
	struct pslist_head h = PSLIST_INITIALIZER;
	struct element {
		unsigned i;
		struct pslist_entry entry;
	} elements[] = {
		{ .i = 0, .entry = PSLIST_ENTRY_INITIALIZER },
		{ .i = 1 },
		{ .i = 2 },
		{ .i = 3 },
		{ .i = 4 },
		{ .i = 5 },
		{ .i = 6 },
		{ .i = 7 },
	};
	struct element *element;
	unsigned i;

	/* Check PSLIST_INITIALIZER is destroyable.  */
	PSLIST_DESTROY(&h);
	PSLIST_INIT(&h);

	/* Check PSLIST_ENTRY_INITIALIZER is destroyable.  */
	PSLIST_ENTRY_DESTROY(&elements[0], entry);

	for (i = 0; i < __arraycount(elements); i++)
		PSLIST_ENTRY_INIT(&elements[i], entry);

	PSLIST_WRITER_INSERT_HEAD(&h, &elements[4], entry);
	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[2], entry);
	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[3], entry);
	PSLIST_WRITER_INSERT_BEFORE(&elements[2], &elements[1], entry);
	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
	PSLIST_WRITER_INSERT_AFTER(&elements[4], &elements[5], entry);
	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[7], entry);
	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[6], entry);

	PSLIST_WRITER_REMOVE(&elements[0], entry);
	ATF_CHECK(elements[0].entry.ple_next != NULL);
	PSLIST_ENTRY_DESTROY(&elements[0], entry);

	PSLIST_WRITER_REMOVE(&elements[4], entry);
	ATF_CHECK(elements[4].entry.ple_next != NULL);
	PSLIST_ENTRY_DESTROY(&elements[4], entry);

	PSLIST_ENTRY_INIT(&elements[0], entry);
	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);

	PSLIST_ENTRY_INIT(&elements[4], entry);
	PSLIST_WRITER_INSERT_AFTER(&elements[3], &elements[4], entry);

	i = 0;
	PSLIST_WRITER_FOREACH(element, &h, struct element, entry) {
		ATF_CHECK_EQ(i, element->i);
		i++;
	}
	i = 0;
	PSLIST_READER_FOREACH(element, &h, struct element, entry) {
		ATF_CHECK_EQ(i, element->i);
		i++;
	}

	while ((element = PSLIST_WRITER_FIRST(&h, struct element, entry))
	    != NULL) {
		PSLIST_WRITER_REMOVE(element, entry);
		PSLIST_ENTRY_DESTROY(element, entry);
	}

	PSLIST_DESTROY(&h);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, misc);

	return atf_no_error();
}