Volatile (keyword): Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
(Mainly syntax highlighting)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(4 intermediate revisions by 4 users not shown)
Line 5:
The <code>volatile</code> keyword can be used in type definitions as well as function arguments, variable definitions and typecasts. Similar to the <code>const</code> keyword, it either makes the variable itself or the data it points to <code>volatile</code>, or both.
 
<sourcesyntaxhighlight lang="c">
/* Ensures the changes to/reads from x are always performed */
volatile int x;
Line 22:
volatile int * volatile ptr;
int volatile * volatile ptr;
</syntaxhighlight>
</source>
 
== Examples ==
Line 30:
This is an example of a function that is supposed to create a very short delay, but might be optimized away by the compiler entirely because to the optimizer this code seems redundant:
 
<sourcesyntaxhighlight lang="c">
static void some_delay(void)
{
Line 45:
}
}
</syntaxhighlight>
</source>
 
This example is supposed to poll a byte until it is <code>0</code> (e.g. waiting for a [[Spinlock]] to be released). Obviously another thread is supposed to change that memory, but since the compiler has no clue about this, we need to ensure that the code isn't optimized away in any case:
 
<sourcesyntaxhighlight lang="c">
typedef unsigned char * spin_lock;
/* Should be defined as '''typedef volatile unsigned char * spin_lock;'''
Line 72:
}
}
</syntaxhighlight>
</source>
 
=== Dereferencing memory ===
Line 78:
This is an example of code that is supposed to touch a piece of memory (e.g. to make it become resident or to check if it's valid and doesn't raise a page fault):
 
<sourcesyntaxhighlight lang="c">
static void touch_mem(void *ptr)
{
Line 88:
*data = *data;
}
</syntaxhighlight>
</source>
 
Also see the [[APIC#IO_APIC_Configuration|APIC example code]] where the <code>volatile</code> keyword is crucial (reading/writing to hardware registers).
Line 96:
Suppose you have one thread "A" that loops until another thread "B" wants thread "A" to terminate gracefully. They share a structure with a field "terminate" that thread "A" polls to see if another thread wants it to terminate. Unless that field or the entire structure is declared <code>volatile</code>, there's no guarantee that the code works as expected because the compiler/optimizer has no clue about that field being touched by someone else while looping. So it could generate code that reads the value once and caches it, resulting in an infinite loop. Use <code>volatile</code> to prevent wrong code being generated:
 
<sourcesyntaxhighlight lang="c">
typedef struct {
int terminate; /* Should be volatile int terminate; */
Line 117:
}
}
</syntaxhighlight>
</source>
 
== External Links ==
[http://www.mjmwired.net/kernel/Documentation/volatile-considered-harmful.txt Volatile considered harmful]
 
[[Category:C]]
[[Category:C++]]