1<html lang="en"> 2<head> 3<title>Volatiles - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Inline.html#Inline" title="Inline"> 10<link rel="next" href="Extended-Asm.html#Extended-Asm" title="Extended Asm"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Volatiles"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Extended-Asm.html#Extended-Asm">Extended Asm</a>, 53Previous: <a rel="previous" accesskey="p" href="Inline.html#Inline">Inline</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.40 When is a Volatile Object Accessed?</h3> 59 60<p><a name="index-accessing-volatiles-2616"></a><a name="index-volatile-read-2617"></a><a name="index-volatile-write-2618"></a><a name="index-volatile-access-2619"></a> 61C has the concept of volatile objects. These are normally accessed by 62pointers and used for accessing hardware or inter-thread 63communication. The standard encourages compilers to refrain from 64optimizations concerning accesses to volatile objects, but leaves it 65implementation defined as to what constitutes a volatile access. The 66minimum requirement is that at a sequence point all previous accesses 67to volatile objects have stabilized and no subsequent accesses have 68occurred. Thus an implementation is free to reorder and combine 69volatile accesses which occur between sequence points, but cannot do 70so for accesses across a sequence point. The use of volatile does 71not allow you to violate the restriction on updating objects multiple 72times between two sequence points. 73 74 <p>Accesses to non-volatile objects are not ordered with respect to 75volatile accesses. You cannot use a volatile object as a memory 76barrier to order a sequence of writes to non-volatile memory. For 77instance: 78 79<pre class="smallexample"> int *ptr = <var>something</var>; 80 volatile int vobj; 81 *ptr = <var>something</var>; 82 vobj = 1; 83</pre> 84 <p>Unless <var>*ptr</var> and <var>vobj</var> can be aliased, it is not guaranteed 85that the write to <var>*ptr</var> will have occurred by the time the update 86of <var>vobj</var> has happened. If you need this guarantee, you must use 87a stronger memory barrier such as: 88 89<pre class="smallexample"> int *ptr = <var>something</var>; 90 volatile int vobj; 91 *ptr = <var>something</var>; 92 asm volatile ("" : : : "memory"); 93 vobj = 1; 94</pre> 95 <p>A scalar volatile object is read when it is accessed in a void context: 96 97<pre class="smallexample"> volatile int *src = <var>somevalue</var>; 98 *src; 99</pre> 100 <p>Such expressions are rvalues, and GCC implements this as a 101read of the volatile object being pointed to. 102 103 <p>Assignments are also expressions and have an rvalue. However when 104assigning to a scalar volatile, the volatile object is not reread, 105regardless of whether the assignment expression's rvalue is used or 106not. If the assignment's rvalue is used, the value is that assigned 107to the volatile object. For instance, there is no read of <var>vobj</var> 108in all the following cases: 109 110<pre class="smallexample"> int obj; 111 volatile int vobj; 112 vobj = <var>something</var>; 113 obj = vobj = <var>something</var>; 114 obj ? vobj = <var>onething</var> : vobj = <var>anotherthing</var>; 115 obj = (<var>something</var>, vobj = <var>anotherthing</var>); 116</pre> 117 <p>If you need to read the volatile object after an assignment has 118occurred, you must use a separate expression with an intervening 119sequence point. 120 121 <p>As bitfields are not individually addressable, volatile bitfields may 122be implicitly read when written to, or when adjacent bitfields are 123accessed. Bitfield operations may be optimized such that adjacent 124bitfields are only partially accessed, if they straddle a storage unit 125boundary. For these reasons it is unwise to use volatile bitfields to 126access hardware. 127 128 </body></html> 129 130