]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcc/extensions-to-the-c-language-family/when-is-a-volatile-object-accessed.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gcc / extensions-to-the-c-language-family / when-is-a-volatile-object-accessed.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. index:: accessing volatiles, volatile read, volatile write, volatile access
7
8 .. _volatiles:
9
10 When is a Volatile Object Accessed?
11 ***********************************
12
13 C has the concept of volatile objects. These are normally accessed by
14 pointers and used for accessing hardware or inter-thread
15 communication. The standard encourages compilers to refrain from
16 optimizations concerning accesses to volatile objects, but leaves it
17 implementation defined as to what constitutes a volatile access. The
18 minimum requirement is that at a sequence point all previous accesses
19 to volatile objects have stabilized and no subsequent accesses have
20 occurred. Thus an implementation is free to reorder and combine
21 volatile accesses that occur between sequence points, but cannot do
22 so for accesses across a sequence point. The use of volatile does
23 not allow you to violate the restriction on updating objects multiple
24 times between two sequence points.
25
26 Accesses to non-volatile objects are not ordered with respect to
27 volatile accesses. You cannot use a volatile object as a memory
28 barrier to order a sequence of writes to non-volatile memory. For
29 instance:
30
31 .. code-block:: c++
32
33 int *ptr = something;
34 volatile int vobj;
35 *ptr = something;
36 vobj = 1;
37
38 Unless :samp:`{*ptr}` and :samp:`{vobj}` can be aliased, it is not guaranteed
39 that the write to :samp:`{*ptr}` occurs by the time the update
40 of :samp:`{vobj}` happens. If you need this guarantee, you must use
41 a stronger memory barrier such as:
42
43 .. code-block:: c++
44
45 int *ptr = something;
46 volatile int vobj;
47 *ptr = something;
48 asm volatile ("" : : : "memory");
49 vobj = 1;
50
51 A scalar volatile object is read when it is accessed in a void context:
52
53 .. code-block:: c++
54
55 volatile int *src = somevalue;
56 *src;
57
58 Such expressions are rvalues, and GCC implements this as a
59 read of the volatile object being pointed to.
60
61 Assignments are also expressions and have an rvalue. However when
62 assigning to a scalar volatile, the volatile object is not reread,
63 regardless of whether the assignment expression's rvalue is used or
64 not. If the assignment's rvalue is used, the value is that assigned
65 to the volatile object. For instance, there is no read of :samp:`{vobj}`
66 in all the following cases:
67
68 .. code-block:: c++
69
70 int obj;
71 volatile int vobj;
72 vobj = something;
73 obj = vobj = something;
74 obj ? vobj = onething : vobj = anotherthing;
75 obj = (something, vobj = anotherthing);
76
77 If you need to read the volatile object after an assignment has
78 occurred, you must use a separate expression with an intervening
79 sequence point.
80
81 As bit-fields are not individually addressable, volatile bit-fields may
82 be implicitly read when written to, or when adjacent bit-fields are
83 accessed. Bit-field operations may be optimized such that adjacent
84 bit-fields are only partially accessed, if they straddle a storage unit
85 boundary. For these reasons it is unwise to use volatile bit-fields to
86 access hardware.