| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| <html> |
| <!-- Copyright (C) 1988-2015 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being "Funding Free Software", the Front-Cover |
| Texts being (a) (see below), and with the Back-Cover Texts being (b) |
| (see below). A copy of the license is included in the section entitled |
| "GNU Free Documentation License". |
| |
| (a) The FSF's Front-Cover Text is: |
| |
| A GNU Manual |
| |
| (b) The FSF's Back-Cover Text is: |
| |
| You have freedom to copy and modify this GNU Manual, like GNU |
| software. Copies published by the Free Software Foundation raise |
| funds for GNU development. --> |
| <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ --> |
| <head> |
| <title>Using the GNU Compiler Collection (GCC): Volatiles</title> |
| |
| <meta name="description" content="Using the GNU Compiler Collection (GCC): Volatiles"> |
| <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Volatiles"> |
| <meta name="resource-type" content="document"> |
| <meta name="distribution" content="global"> |
| <meta name="Generator" content="makeinfo"> |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| <link href="index.html#Top" rel="start" title="Top"> |
| <link href="Option-Index.html#Option-Index" rel="index" title="Option Index"> |
| <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> |
| <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions"> |
| <link href="Extended-Asm.html#Extended-Asm" rel="next" title="Extended Asm"> |
| <link href="Inline.html#Inline" rel="prev" title="Inline"> |
| <style type="text/css"> |
| <!-- |
| a.summary-letter {text-decoration: none} |
| blockquote.smallquotation {font-size: smaller} |
| div.display {margin-left: 3.2em} |
| div.example {margin-left: 3.2em} |
| div.indentedblock {margin-left: 3.2em} |
| div.lisp {margin-left: 3.2em} |
| div.smalldisplay {margin-left: 3.2em} |
| div.smallexample {margin-left: 3.2em} |
| div.smallindentedblock {margin-left: 3.2em; font-size: smaller} |
| div.smalllisp {margin-left: 3.2em} |
| kbd {font-style:oblique} |
| pre.display {font-family: inherit} |
| pre.format {font-family: inherit} |
| pre.menu-comment {font-family: serif} |
| pre.menu-preformatted {font-family: serif} |
| pre.smalldisplay {font-family: inherit; font-size: smaller} |
| pre.smallexample {font-size: smaller} |
| pre.smallformat {font-family: inherit; font-size: smaller} |
| pre.smalllisp {font-size: smaller} |
| span.nocodebreak {white-space:nowrap} |
| span.nolinebreak {white-space:nowrap} |
| span.roman {font-family:serif; font-weight:normal} |
| span.sansserif {font-family:sans-serif; font-weight:normal} |
| ul.no-bullet {list-style: none} |
| --> |
| </style> |
| |
| |
| </head> |
| |
| <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> |
| <a name="Volatiles"></a> |
| <div class="header"> |
| <p> |
| Next: <a href="Extended-Asm.html#Extended-Asm" accesskey="n" rel="next">Extended Asm</a>, Previous: <a href="Inline.html#Inline" accesskey="p" rel="prev">Inline</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p> |
| </div> |
| <hr> |
| <a name="When-is-a-Volatile-Object-Accessed_003f"></a> |
| <h3 class="section">6.40 When is a Volatile Object Accessed?</h3> |
| <a name="index-accessing-volatiles"></a> |
| <a name="index-volatile-read"></a> |
| <a name="index-volatile-write"></a> |
| <a name="index-volatile-access"></a> |
| |
| <p>C has the concept of volatile objects. These are normally accessed by |
| pointers and used for accessing hardware or inter-thread |
| communication. The standard encourages compilers to refrain from |
| optimizations concerning accesses to volatile objects, but leaves it |
| implementation defined as to what constitutes a volatile access. The |
| minimum requirement is that at a sequence point all previous accesses |
| to volatile objects have stabilized and no subsequent accesses have |
| occurred. Thus an implementation is free to reorder and combine |
| volatile accesses that occur between sequence points, but cannot do |
| so for accesses across a sequence point. The use of volatile does |
| not allow you to violate the restriction on updating objects multiple |
| times between two sequence points. |
| </p> |
| <p>Accesses to non-volatile objects are not ordered with respect to |
| volatile accesses. You cannot use a volatile object as a memory |
| barrier to order a sequence of writes to non-volatile memory. For |
| instance: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int *ptr = <var>something</var>; |
| volatile int vobj; |
| *ptr = <var>something</var>; |
| vobj = 1; |
| </pre></div> |
| |
| <p>Unless <var>*ptr</var> and <var>vobj</var> can be aliased, it is not guaranteed |
| that the write to <var>*ptr</var> occurs by the time the update |
| of <var>vobj</var> happens. If you need this guarantee, you must use |
| a stronger memory barrier such as: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int *ptr = <var>something</var>; |
| volatile int vobj; |
| *ptr = <var>something</var>; |
| asm volatile ("" : : : "memory"); |
| vobj = 1; |
| </pre></div> |
| |
| <p>A scalar volatile object is read when it is accessed in a void context: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">volatile int *src = <var>somevalue</var>; |
| *src; |
| </pre></div> |
| |
| <p>Such expressions are rvalues, and GCC implements this as a |
| read of the volatile object being pointed to. |
| </p> |
| <p>Assignments are also expressions and have an rvalue. However when |
| assigning to a scalar volatile, the volatile object is not reread, |
| regardless of whether the assignment expression’s rvalue is used or |
| not. If the assignment’s rvalue is used, the value is that assigned |
| to the volatile object. For instance, there is no read of <var>vobj</var> |
| in all the following cases: |
| </p> |
| <div class="smallexample"> |
| <pre class="smallexample">int obj; |
| volatile int vobj; |
| vobj = <var>something</var>; |
| obj = vobj = <var>something</var>; |
| obj ? vobj = <var>onething</var> : vobj = <var>anotherthing</var>; |
| obj = (<var>something</var>, vobj = <var>anotherthing</var>); |
| </pre></div> |
| |
| <p>If you need to read the volatile object after an assignment has |
| occurred, you must use a separate expression with an intervening |
| sequence point. |
| </p> |
| <p>As bit-fields are not individually addressable, volatile bit-fields may |
| be implicitly read when written to, or when adjacent bit-fields are |
| accessed. Bit-field operations may be optimized such that adjacent |
| bit-fields are only partially accessed, if they straddle a storage unit |
| boundary. For these reasons it is unwise to use volatile bit-fields to |
| access hardware. |
| </p> |
| <hr> |
| <div class="header"> |
| <p> |
| Next: <a href="Extended-Asm.html#Extended-Asm" accesskey="n" rel="next">Extended Asm</a>, Previous: <a href="Inline.html#Inline" accesskey="p" rel="prev">Inline</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p> |
| </div> |
| |
| |
| |
| </body> |
| </html> |