Skip to content

Commit 8d7b52d

Browse files
Matti LinnanvuoriLinus Torvalds
authored andcommitted
atomic_ops.txt has incorrect, misleading and insufficient information [Bug 9020]
atomic_ops.txt has incorrect, misleading and insufficient information about semantics of initializer, atomic_set, atomic_read and atomic_xchg. It also incorrectly implies that operations mentioned above are not actual atomic operations. Included is most of the patch Document non-semantics of atomic_read() and atomic_set() by Chris Snook, except the word "assignment". Signed-off-by: Matti Linnanvuori <mattilinnanvuori@yahoo.com> Cc: Nick Piggin <npiggin@suse.de> Cc: "Paul E. McKenney" <paulmck@us.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5f519d7 commit 8d7b52d

1 file changed

Lines changed: 50 additions & 5 deletions

File tree

‎Documentation/atomic_ops.txt‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ suffice:
1414

1515
typedef struct { volatile int counter; } atomic_t;
1616

17+
Historically, counter has been declared volatile. This is now discouraged.
18+
See Documentation/volatile-considered-harmful.txt for the complete rationale.
19+
1720
local_t is very similar to atomic_t. If the counter is per CPU and only
1821
updated by one CPU, local_t is probably more appropriate. Please see
1922
Documentation/local_ops.txt for the semantics of local_t.
2023

21-
The first operations to implement for atomic_t's are the
22-
initializers and plain reads.
24+
The first operations to implement for atomic_t's are the initializers and
25+
plain reads.
2326

2427
#define ATOMIC_INIT(i) { (i) }
2528
#define atomic_set(v, i) ((v)->counter = (i))
@@ -28,6 +31,12 @@ The first macro is used in definitions, such as:
2831

2932
static atomic_t my_counter = ATOMIC_INIT(1);
3033

34+
The initializer is atomic in that the return values of the atomic operations
35+
are guaranteed to be correct reflecting the initialized value if the
36+
initializer is used before runtime. If the initializer is used at runtime, a
37+
proper implicit or explicit read memory barrier is needed before reading the
38+
value with atomic_read from another thread.
39+
3140
The second interface can be used at runtime, as in:
3241

3342
struct foo { atomic_t counter; };
@@ -40,13 +49,43 @@ The second interface can be used at runtime, as in:
4049
return -ENOMEM;
4150
atomic_set(&k->counter, 0);
4251

52+
The setting is atomic in that the return values of the atomic operations by
53+
all threads are guaranteed to be correct reflecting either the value that has
54+
been set with this operation or set with another operation. A proper implicit
55+
or explicit memory barrier is needed before the value set with the operation
56+
is guaranteed to be readable with atomic_read from another thread.
57+
4358
Next, we have:
4459

4560
#define atomic_read(v) ((v)->counter)
4661

47-
which simply reads the current value of the counter.
48-
49-
Now, we move onto the actual atomic operation interfaces.
62+
which simply reads the counter value currently visible to the calling thread.
63+
The read is atomic in that the return value is guaranteed to be one of the
64+
values initialized or modified with the interface operations if a proper
65+
implicit or explicit memory barrier is used after possible runtime
66+
initialization by any other thread and the value is modified only with the
67+
interface operations. atomic_read does not guarantee that the runtime
68+
initialization by any other thread is visible yet, so the user of the
69+
interface must take care of that with a proper implicit or explicit memory
70+
barrier.
71+
72+
*** WARNING: atomic_read() and atomic_set() DO NOT IMPLY BARRIERS! ***
73+
74+
Some architectures may choose to use the volatile keyword, barriers, or inline
75+
assembly to guarantee some degree of immediacy for atomic_read() and
76+
atomic_set(). This is not uniformly guaranteed, and may change in the future,
77+
so all users of atomic_t should treat atomic_read() and atomic_set() as simple
78+
C statements that may be reordered or optimized away entirely by the compiler
79+
or processor, and explicitly invoke the appropriate compiler and/or memory
80+
barrier for each use case. Failure to do so will result in code that may
81+
suddenly break when used with different architectures or compiler
82+
optimizations, or even changes in unrelated code which changes how the
83+
compiler optimizes the section accessing atomic_t variables.
84+
85+
*** YOU HAVE BEEN WARNED! ***
86+
87+
Now, we move onto the atomic operation interfaces typically implemented with
88+
the help of assembly code.
5089

5190
void atomic_add(int i, atomic_t *v);
5291
void atomic_sub(int i, atomic_t *v);
@@ -121,6 +160,12 @@ operation.
121160

122161
Then:
123162

163+
int atomic_xchg(atomic_t *v, int new);
164+
165+
This performs an atomic exchange operation on the atomic variable v, setting
166+
the given new value. It returns the old value that the atomic variable v had
167+
just before the operation.
168+
124169
int atomic_cmpxchg(atomic_t *v, int old, int new);
125170

126171
This performs an atomic compare exchange operation on the atomic value v,

0 commit comments

Comments
 (0)