Home | History | Annotate | Line # | Download | only in libpthread
      1 /*	$NetBSD: t_mtx.c,v 1.2 2020/06/11 11:40:54 martin 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_mtx.c,v 1.2 2020/06/11 11:40:54 martin Exp $");
     36 
     37 #include <time.h>
     38 #include <threads.h>
     39 
     40 #include <atf-c.h>
     41 
     42 ATF_TC(mtx_init);
     43 ATF_TC_HEAD(mtx_init, tc)
     44 {
     45 	atf_tc_set_md_var(tc, "descr", "Test C11 mtx_init(3)");
     46 }
     47 
     48 ATF_TC_BODY(mtx_init, tc)
     49 {
     50 	mtx_t m;
     51 
     52 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
     53 	mtx_destroy(&m);
     54 
     55 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success);
     56 	mtx_destroy(&m);
     57 
     58 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
     59 	mtx_destroy(&m);
     60 
     61 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
     62 	mtx_destroy(&m);
     63 
     64 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_recursive), thrd_error);
     65 
     66 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_timed), thrd_error);
     67 
     68 	ATF_REQUIRE_EQ(mtx_init(&m, -1), thrd_error);
     69 }
     70 
     71 ATF_TC(mtx_lock);
     72 ATF_TC_HEAD(mtx_lock, tc)
     73 {
     74 	atf_tc_set_md_var(tc, "descr", "Test C11 mtx_lock(3)");
     75 }
     76 
     77 ATF_TC_BODY(mtx_lock, tc)
     78 {
     79 	mtx_t m;
     80 
     81 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
     82 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
     83 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
     84 	mtx_destroy(&m);
     85 
     86 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
     87 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
     88 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
     89 	mtx_destroy(&m);
     90 
     91 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success);
     92 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
     93 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
     94 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
     95 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
     96 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
     97 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
     98 	mtx_destroy(&m);
     99 
    100 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
    101 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    102 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    103 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    104 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    105 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    106 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    107 	mtx_destroy(&m);
    108 }
    109 
    110 ATF_TC(mtx_timedlock);
    111 ATF_TC_HEAD(mtx_timedlock, tc)
    112 {
    113 	atf_tc_set_md_var(tc, "descr", "Test C11 mtx_timedlock(3)");
    114 }
    115 
    116 ATF_TC_BODY(mtx_timedlock, tc)
    117 {
    118 	mtx_t m;
    119 	struct timespec ts;
    120 
    121 	ts.tv_sec = 0;
    122 	ts.tv_nsec = 1;
    123 
    124 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
    125 	ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
    126 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    127 	mtx_destroy(&m);
    128 
    129 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
    130 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    131 	ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_timedout);
    132 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    133 	mtx_destroy(&m);
    134 
    135 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
    136 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    137 	ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
    138 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    139 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    140 	mtx_destroy(&m);
    141 
    142 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
    143 	ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
    144 	ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
    145 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    146 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    147 	mtx_destroy(&m);
    148 
    149 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
    150 	ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
    151 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    152 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    153 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    154 	mtx_destroy(&m);
    155 }
    156 
    157 ATF_TC(mtx_trylock);
    158 ATF_TC_HEAD(mtx_trylock, tc)
    159 {
    160 	atf_tc_set_md_var(tc, "descr", "Test C11 mtx_trylock(3)");
    161 }
    162 
    163 ATF_TC_BODY(mtx_trylock, tc)
    164 {
    165 	mtx_t m;
    166 
    167 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
    168 	ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
    169 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    170 	mtx_destroy(&m);
    171 
    172 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
    173 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    174 	ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_busy);
    175 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    176 	mtx_destroy(&m);
    177 
    178 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success);
    179 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    180 	ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
    181 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    182 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    183 	mtx_destroy(&m);
    184 
    185 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
    186 	ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
    187 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    188 	mtx_destroy(&m);
    189 
    190 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
    191 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    192 	ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_busy);
    193 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    194 	mtx_destroy(&m);
    195 
    196 	ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
    197 	ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
    198 	ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
    199 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    200 	ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
    201 	mtx_destroy(&m);
    202 }
    203 
    204 ATF_TP_ADD_TCS(tp)
    205 {
    206 	ATF_TP_ADD_TC(tp, mtx_init);
    207 	ATF_TP_ADD_TC(tp, mtx_lock);
    208 	ATF_TP_ADD_TC(tp, mtx_timedlock);
    209 	ATF_TP_ADD_TC(tp, mtx_trylock);
    210 
    211 	return atf_no_error();
    212 }
    213