xref: /netbsd-src/tests/lib/libc/atomic/t___sync_lock.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: t___sync_lock.c,v 1.1 2019/02/26 10:01:41 isaki Exp $	*/
2 
3 /*
4  * Copyright (C) 2019 Tetsuya Isaki. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: t___sync_lock.c,v 1.1 2019/02/26 10:01:41 isaki Exp $");
30 
31 #include <atf-c.h>
32 #include <inttypes.h>
33 #include <machine/types.h>	// for __HAVE_ATOMIC64_OPS
34 
35 /*
36  * These tests don't examine the atomicity.
37  */
38 
39 /* XXX
40  * Depending on a combination of arch and compiler, __sync_* is
41  * implemented as compiler's builtin function.  In that case, even
42  * if libc exports the function symbol, it is not used.  As a result
43  * this tests will examine compiler's builtin functions.
44  * It's better to run only when target is actually in libc.
45  */
46 
47 #define OLDVAL (0x1122334455667788UL)
48 #define NEWVAL (0x8090a0b0c0d0e0f0UL)
49 
50 #define atf_sync_tas(NAME, TYPE, FMT) \
51 ATF_TC(NAME); \
52 ATF_TC_HEAD(NAME, tc) \
53 { \
54 	atf_tc_set_md_var(tc, "descr", #NAME); \
55 } \
56 ATF_TC_BODY(NAME, tc) \
57 { \
58 	volatile TYPE val; \
59 	TYPE newval; \
60 	TYPE expval; \
61 	TYPE expres; \
62 	TYPE res; \
63 	val = (TYPE)OLDVAL; \
64 	newval = (TYPE)NEWVAL; \
65 	expval = (TYPE)NEWVAL; \
66 	expres = (TYPE)OLDVAL; \
67 	res = NAME(&val, newval); \
68 	ATF_REQUIRE_MSG(val == expval, \
69 	    "val expects 0x%" FMT " but 0x%" FMT, expval, val); \
70 	ATF_REQUIRE_MSG(res == expres, \
71 	    "res expects 0x%" FMT " but 0x%" FMT, expres, res); \
72 }
73 
74 atf_sync_tas(__sync_lock_test_and_set_1, uint8_t,  PRIx8);
75 atf_sync_tas(__sync_lock_test_and_set_2, uint16_t, PRIx16);
76 atf_sync_tas(__sync_lock_test_and_set_4, uint32_t, PRIx32);
77 #ifdef __HAVE_ATOMIC64_OPS
78 atf_sync_tas(__sync_lock_test_and_set_8, uint64_t, PRIx64);
79 #endif
80 
81 #define atf_sync_rel(NAME, TYPE, FMT) \
82 ATF_TC(NAME); \
83 ATF_TC_HEAD(NAME, tc) \
84 { \
85 	atf_tc_set_md_var(tc, "descr", #NAME); \
86 } \
87 ATF_TC_BODY(NAME, tc) \
88 { \
89 	volatile TYPE val; \
90 	TYPE expval; \
91 	val = (TYPE)OLDVAL; \
92 	expval = (TYPE)0; \
93 	NAME(&val); \
94 	ATF_REQUIRE_MSG(val == expval, \
95 	    "val expects 0x%" FMT " but 0x%" FMT, expval, val); \
96 }
97 
98 atf_sync_rel(__sync_lock_release_1, uint8_t,  PRIx8);
99 atf_sync_rel(__sync_lock_release_2, uint16_t, PRIx16);
100 atf_sync_rel(__sync_lock_release_4, uint32_t, PRIx32);
101 #ifdef __HAVE_ATOMIC64_OPS
102 atf_sync_rel(__sync_lock_release_8, uint64_t, PRIx64);
103 #endif
104 
105 /*
106  * __sync_synchronize(): This is just a link-time test.
107  */
108 ATF_TC(__sync_synchronize);
109 ATF_TC_HEAD(__sync_synchronize, tc)
110 {
111 	atf_tc_set_md_var(tc, "descr", "__sync_synchronize");
112 }
113 ATF_TC_BODY(__sync_synchronize, tc)
114 {
115 	__sync_synchronize();
116 }
117 
118 ATF_TP_ADD_TCS(tp)
119 {
120 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_1);
121 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_2);
122 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_4);
123 #ifdef __HAVE_ATOMIC64_OPS
124 	ATF_TP_ADD_TC(tp, __sync_lock_test_and_set_8);
125 #endif
126 
127 	ATF_TP_ADD_TC(tp, __sync_lock_release_1);
128 	ATF_TP_ADD_TC(tp, __sync_lock_release_2);
129 	ATF_TP_ADD_TC(tp, __sync_lock_release_4);
130 #ifdef __HAVE_ATOMIC64_OPS
131 	ATF_TP_ADD_TC(tp, __sync_lock_release_8);
132 #endif
133 
134 	ATF_TP_ADD_TC(tp, __sync_synchronize);
135 
136 	return atf_no_error();
137 }
138