Home | History | Annotate | Line # | Download | only in sys
t_pslist.c revision 1.1.16.1
      1       1.1  riastrad /*	$NetBSD: t_pslist.c,v 1.1.16.1 2020/04/08 14:09:08 martin Exp $	*/
      2       1.1  riastrad 
      3       1.1  riastrad /*-
      4       1.1  riastrad  * Copyright (c) 2016 The NetBSD Foundation, Inc.
      5       1.1  riastrad  * All rights reserved.
      6       1.1  riastrad  *
      7       1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  riastrad  * by Taylor R. Campbell.
      9       1.1  riastrad  *
     10       1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11       1.1  riastrad  * modification, are permitted provided that the following conditions
     12       1.1  riastrad  * are met:
     13       1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14       1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15       1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18       1.1  riastrad  *
     19       1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  riastrad  */
     31       1.1  riastrad 
     32       1.1  riastrad /*
     33       1.1  riastrad  * XXX This is a limited test to make sure the operations behave as
     34       1.1  riastrad  * described on a sequential machine.  It does nothing to test the
     35  1.1.16.1    martin  * pserialize-safety of any operations.  The following definitions must
     36  1.1.16.1    martin  * be replaced in any parallel tests.
     37       1.1  riastrad  */
     38       1.1  riastrad 
     39  1.1.16.1    martin #define	atomic_load_relaxed(p)		(*(p))
     40  1.1.16.1    martin #define	atomic_load_acquire(p)		(*(p))
     41  1.1.16.1    martin #define	atomic_load_consume(p)		(*(p))
     42  1.1.16.1    martin #define	atomic_store_relaxed(p,v)	(*(p) = (v))
     43  1.1.16.1    martin #define	atomic_store_release(p,v)	(*(p) = (v))
     44  1.1.16.1    martin 
     45  1.1.16.1    martin #include <sys/pslist.h>
     46  1.1.16.1    martin 
     47  1.1.16.1    martin #include <atf-c.h>
     48  1.1.16.1    martin 
     49       1.1  riastrad ATF_TC(misc);
     50       1.1  riastrad ATF_TC_HEAD(misc, tc)
     51       1.1  riastrad {
     52       1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "pserialize-safe list tests");
     53       1.1  riastrad }
     54       1.1  riastrad ATF_TC_BODY(misc, tc)
     55       1.1  riastrad {
     56       1.1  riastrad 	struct pslist_head h = PSLIST_INITIALIZER;
     57       1.1  riastrad 	struct element {
     58       1.1  riastrad 		unsigned i;
     59       1.1  riastrad 		struct pslist_entry entry;
     60       1.1  riastrad 	} elements[] = {
     61       1.1  riastrad 		{ .i = 0, .entry = PSLIST_ENTRY_INITIALIZER },
     62       1.1  riastrad 		{ .i = 1 },
     63       1.1  riastrad 		{ .i = 2 },
     64       1.1  riastrad 		{ .i = 3 },
     65       1.1  riastrad 		{ .i = 4 },
     66       1.1  riastrad 		{ .i = 5 },
     67       1.1  riastrad 		{ .i = 6 },
     68       1.1  riastrad 		{ .i = 7 },
     69       1.1  riastrad 	};
     70       1.1  riastrad 	struct element *element;
     71       1.1  riastrad 	unsigned i;
     72       1.1  riastrad 
     73       1.1  riastrad 	/* Check PSLIST_INITIALIZER is destroyable.  */
     74       1.1  riastrad 	PSLIST_DESTROY(&h);
     75       1.1  riastrad 	PSLIST_INIT(&h);
     76       1.1  riastrad 
     77       1.1  riastrad 	/* Check PSLIST_ENTRY_INITIALIZER is destroyable.  */
     78       1.1  riastrad 	PSLIST_ENTRY_DESTROY(&elements[0], entry);
     79       1.1  riastrad 
     80       1.1  riastrad 	for (i = 0; i < __arraycount(elements); i++)
     81       1.1  riastrad 		PSLIST_ENTRY_INIT(&elements[i], entry);
     82       1.1  riastrad 
     83       1.1  riastrad 	PSLIST_WRITER_INSERT_HEAD(&h, &elements[4], entry);
     84       1.1  riastrad 	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[2], entry);
     85       1.1  riastrad 	PSLIST_WRITER_INSERT_BEFORE(&elements[4], &elements[3], entry);
     86       1.1  riastrad 	PSLIST_WRITER_INSERT_BEFORE(&elements[2], &elements[1], entry);
     87       1.1  riastrad 	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
     88       1.1  riastrad 	PSLIST_WRITER_INSERT_AFTER(&elements[4], &elements[5], entry);
     89       1.1  riastrad 	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[7], entry);
     90       1.1  riastrad 	PSLIST_WRITER_INSERT_AFTER(&elements[5], &elements[6], entry);
     91       1.1  riastrad 
     92       1.1  riastrad 	PSLIST_WRITER_REMOVE(&elements[0], entry);
     93       1.1  riastrad 	ATF_CHECK(elements[0].entry.ple_next != NULL);
     94       1.1  riastrad 	PSLIST_ENTRY_DESTROY(&elements[0], entry);
     95       1.1  riastrad 
     96       1.1  riastrad 	PSLIST_WRITER_REMOVE(&elements[4], entry);
     97       1.1  riastrad 	ATF_CHECK(elements[4].entry.ple_next != NULL);
     98       1.1  riastrad 	PSLIST_ENTRY_DESTROY(&elements[4], entry);
     99       1.1  riastrad 
    100       1.1  riastrad 	PSLIST_ENTRY_INIT(&elements[0], entry);
    101       1.1  riastrad 	PSLIST_WRITER_INSERT_HEAD(&h, &elements[0], entry);
    102       1.1  riastrad 
    103       1.1  riastrad 	PSLIST_ENTRY_INIT(&elements[4], entry);
    104       1.1  riastrad 	PSLIST_WRITER_INSERT_AFTER(&elements[3], &elements[4], entry);
    105       1.1  riastrad 
    106       1.1  riastrad 	i = 0;
    107       1.1  riastrad 	PSLIST_WRITER_FOREACH(element, &h, struct element, entry) {
    108       1.1  riastrad 		ATF_CHECK_EQ(i, element->i);
    109       1.1  riastrad 		i++;
    110       1.1  riastrad 	}
    111       1.1  riastrad 	i = 0;
    112       1.1  riastrad 	PSLIST_READER_FOREACH(element, &h, struct element, entry) {
    113       1.1  riastrad 		ATF_CHECK_EQ(i, element->i);
    114       1.1  riastrad 		i++;
    115       1.1  riastrad 	}
    116       1.1  riastrad 
    117       1.1  riastrad 	while ((element = PSLIST_WRITER_FIRST(&h, struct element, entry))
    118       1.1  riastrad 	    != NULL) {
    119       1.1  riastrad 		PSLIST_WRITER_REMOVE(element, entry);
    120       1.1  riastrad 		PSLIST_ENTRY_DESTROY(element, entry);
    121       1.1  riastrad 	}
    122       1.1  riastrad 
    123       1.1  riastrad 	PSLIST_DESTROY(&h);
    124       1.1  riastrad }
    125       1.1  riastrad 
    126       1.1  riastrad ATF_TP_ADD_TCS(tp)
    127       1.1  riastrad {
    128       1.1  riastrad 
    129       1.1  riastrad 	ATF_TP_ADD_TC(tp, misc);
    130       1.1  riastrad 
    131       1.1  riastrad 	return atf_no_error();
    132       1.1  riastrad }
    133