Skip to content

Commit 4dcd4d3

Browse files
fbqpaulmckrcu
authored andcommitted
Documentation/litmus-tests/atomic: Add a test for atomic_set()
We already use a litmus test in atomic_t.txt to describe the behavior of an atomic_set() with the an atomic RMW, so add it into atomic-tests directory to make it easily accessible for anyone who cares about the semantics of our atomic APIs. Besides currently the litmus test "atomic-set" in atomic_t.txt has a few things to be improved: 1) The CPU/Processor numbers "P1,P2" are not only inconsistent with the rest of the document, which uses "CPU0" and "CPU1", but also unacceptable by the herd tool, which requires processors start at "P0". 2) The initialization block uses a "atomic_set()", which is OK, but it's better to use ATOMIC_INIT() to make clear this is an initialization. 3) The return value of atomic_add_unless() is discarded inexplicitly, which is OK for C language, but it will be helpful to the herd tool if we use a void cast to make the discard explicit. 4) The name and the paragraph describing the test need to be more accurate and aligned with our wording in LKMM. Therefore fix these in both atomic_t.txt and the new added litmus test. Acked-by: Andrea Parri <[email protected]> Acked-by: Alan Stern <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Reviewed-by: Joel Fernandes (Google) <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent efff615 commit 4dcd4d3

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

Documentation/atomic_t.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,21 @@ smp_store_release() respectively. Therefore, if you find yourself only using
8585
the Non-RMW operations of atomic_t, you do not in fact need atomic_t at all
8686
and are doing it wrong.
8787

88-
A subtle detail of atomic_set{}() is that it should be observable to the RMW
89-
ops. That is:
88+
A note for the implementation of atomic_set{}() is that it must not break the
89+
atomicity of the RMW ops. That is:
9090

91-
C atomic-set
91+
C Atomic-RMW-ops-are-atomic-WRT-atomic_set
9292

9393
{
94-
atomic_set(v, 1);
94+
atomic_t v = ATOMIC_INIT(1);
9595
}
9696

97-
P1(atomic_t *v)
97+
P0(atomic_t *v)
9898
{
99-
atomic_add_unless(v, 1, 0);
99+
(void)atomic_add_unless(v, 1, 0);
100100
}
101101

102-
P2(atomic_t *v)
102+
P1(atomic_t *v)
103103
{
104104
atomic_set(v, 0);
105105
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
C Atomic-RMW-ops-are-atomic-WRT-atomic_set
2+
3+
(*
4+
* Result: Never
5+
*
6+
* Test that atomic_set() cannot break the atomicity of atomic RMWs.
7+
*)
8+
9+
{
10+
atomic_t v = ATOMIC_INIT(1);
11+
}
12+
13+
P0(atomic_t *v)
14+
{
15+
(void)atomic_add_unless(v, 1, 0);
16+
}
17+
18+
P1(atomic_t *v)
19+
{
20+
atomic_set(v, 0);
21+
}
22+
23+
exists
24+
(v=2)

Documentation/litmus-tests/atomic/README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ This directory contains litmus tests that are typical to describe the semantics
22
of our atomic APIs. For more information about how to "run" a litmus test or
33
how to generate a kernel test module based on a litmus test, please see
44
tools/memory-model/README.
5+
6+
============
7+
LITMUS TESTS
8+
============
9+
10+
Atomic-RMW-ops-are-atomic-WRT-atomic_set.litmus
11+
Test that atomic_set() cannot break the atomicity of atomic RMWs.

0 commit comments

Comments
 (0)