Home | History | Annotate | Line # | Download | only in libpthread
      1 /*	$NetBSD: t_tss.c,v 1.1 2019/04/24 11:43:19 kamil Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Kamil Rytarowski.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __COPYRIGHT("@(#) Copyright (c) 2019\
     34  The NetBSD Foundation, inc. All rights reserved.");
     35 __RCSID("$NetBSD: t_tss.c,v 1.1 2019/04/24 11:43:19 kamil Exp $");
     36 
     37 #include <stdlib.h>
     38 #include <threads.h>
     39 
     40 #include <atf-c.h>
     41 
     42 ATF_TC(tss_create);
     43 ATF_TC_HEAD(tss_create, tc)
     44 {
     45 	atf_tc_set_md_var(tc, "descr", "Test C11 tss_create(3)");
     46 }
     47 
     48 ATF_TC_BODY(tss_create, tc)
     49 {
     50 	tss_t s;
     51 
     52 	ATF_REQUIRE_EQ(tss_create(&s, NULL), thrd_success);
     53 	tss_delete(s);
     54 }
     55 
     56 ATF_TC(tss_set);
     57 ATF_TC_HEAD(tss_set, tc)
     58 {
     59 	atf_tc_set_md_var(tc, "descr", "Test C11 tss_set(3)");
     60 }
     61 
     62 ATF_TC_BODY(tss_set, tc)
     63 {
     64 	tss_t s;
     65 	int b = 5;
     66 	void *v;
     67 
     68 	v = (void *)(intptr_t)b;
     69 
     70 	ATF_REQUIRE_EQ(tss_create(&s, NULL), thrd_success);
     71 	ATF_REQUIRE_EQ(tss_get(s), NULL);
     72 	ATF_REQUIRE_EQ(tss_set(s, v), thrd_success);
     73 	ATF_REQUIRE_EQ(tss_get(s), v);
     74 
     75 	tss_delete(s);
     76 }
     77 
     78 ATF_TC(tss_destructor_main_thread);
     79 ATF_TC_HEAD(tss_destructor_main_thread, tc)
     80 {
     81 	atf_tc_set_md_var(tc, "descr",
     82 	    "Test C11 tss(3) destructor in main thread");
     83 }
     84 
     85 static void
     86 c_destructor_main(void *arg)
     87 {
     88 
     89 	abort();
     90 }
     91 
     92 ATF_TC_BODY(tss_destructor_main_thread, tc)
     93 {
     94 	tss_t s;
     95 	int b = 5;
     96 	void *v;
     97 
     98 	v = (void *)(intptr_t)b;
     99 
    100 	ATF_REQUIRE_EQ(tss_create(&s, c_destructor_main), thrd_success);
    101 	ATF_REQUIRE_EQ(tss_set(s, v), thrd_success);
    102 
    103 	tss_delete(s);
    104 
    105 	/* Destructor must NOT be called for tss_delete(3) */
    106 }
    107 
    108 ATF_TC(tss_destructor_thread_exit);
    109 ATF_TC_HEAD(tss_destructor_thread_exit, tc)
    110 {
    111 	atf_tc_set_md_var(tc, "descr",
    112 	    "Test C11 tss(3) destructor on thread exit");
    113 }
    114 
    115 static tss_t s_empty, s_nonempty;
    116 static int c_iter_empty, c_iter_nonempty;
    117 
    118 static void
    119 c_destructor_thread_empty(void *arg)
    120 {
    121 
    122 	tss_set(s_empty, arg);
    123 	++c_iter_empty;
    124 }
    125 
    126 static void
    127 c_destructor_thread_nonempty(void *arg)
    128 {
    129 
    130 	tss_set(s_nonempty, arg);
    131 	++c_iter_nonempty;
    132 }
    133 
    134 static int
    135 t_func(void *arg __unused)
    136 {
    137 	int b = 5;
    138 	void *v;
    139 
    140 	v = (void *)(intptr_t)b;
    141 
    142 	ATF_REQUIRE_EQ(tss_set(s_nonempty, v), thrd_success);
    143 
    144 	return 0;
    145 }
    146 
    147 ATF_TC_BODY(tss_destructor_thread_exit, tc)
    148 {
    149 	thrd_t t;
    150 
    151 	ATF_REQUIRE_EQ(tss_create(&s_empty, c_destructor_thread_empty),
    152 	    thrd_success);
    153 	ATF_REQUIRE_EQ(tss_create(&s_nonempty, c_destructor_thread_nonempty),
    154 	    thrd_success);
    155 
    156 	ATF_REQUIRE_EQ(thrd_create(&t, t_func, NULL), thrd_success);
    157 	ATF_REQUIRE_EQ(thrd_join(t, NULL), thrd_success);
    158 
    159 	ATF_REQUIRE_EQ(c_iter_empty, 0);
    160 	ATF_REQUIRE_EQ(c_iter_nonempty, TSS_DTOR_ITERATIONS);
    161 
    162 	tss_delete(s_empty);
    163 	tss_delete(s_nonempty);
    164 }
    165 
    166 ATF_TP_ADD_TCS(tp)
    167 {
    168 	ATF_TP_ADD_TC(tp, tss_create);
    169 	ATF_TP_ADD_TC(tp, tss_set);
    170 	ATF_TP_ADD_TC(tp, tss_destructor_main_thread);
    171 	ATF_TP_ADD_TC(tp, tss_destructor_thread_exit);
    172 
    173 	return atf_no_error();
    174 }
    175