Error » Certification & Programming center Error !! » Programming tutorials » Use of volatile

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Use of volatile
LinkBack Thread Tools Display Modes
Old 03-Jan-2007, 10:16 PM   #1 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Use of volatile

The compilation system tries to reduce code size and execution time on all machines, by optimizing code. It is programmer responsibility to inform compilation system that certain code (or variable) should not be optimized. Many programmers do not understand when to use volatile to inform compilation system that certain code should not be optimized or what it does. Although (no doubt) their program work, they do not exploit the full power of the language.

A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread. Volatile, can appear only once in a declaration with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. For example, the following declarations are legal



/* Let T denotes some data type */typedef volatile T i; volatile T i;T volatile i ; </PRE>And the following declaration is illegal

T i, volatile vi ;</PRE>Volatile qualifiers can be used to change the behavior of a type. For example,

volatile int p = 3;</PRE>declares and initializes an object with type volatile int whose value will be always read from memory.
Syntax

Keyword volatile can be placed before or after the data type in the variable definition. For example following declaration are identical:


Volatile T a =3;T volatile a=3; </PRE>The declaration declares and initializes an objet with type volatile T whose value will be always read from memory
Pointer declaration


Volatile T * ptr; T volatile * ptr; </PRE>Ptr is a a pointer to a volatile T:

volatile pointer to a volatile variable int volatile * volatile ptr; </PRE>volatile can be applied to derived types such as an array type ,in that case, the element is qualified, not the array type. When applied to struct types entire contents of the struct become volatile. You can apply the volatile qualifier to the individual members of the struct. Type qualifiers are relevant only when accessing identifiers as l-values in expressions. volatile does not affects the range of values or arithmetic properties of the object.. To declare the item pointed to by the pointer as volatile, use a declaration of the form:

volatile T *vptr;</PRE>

To declare the value of the pointer - that is, the actual address stored in the pointer - as volatile, use a declaration of the form:

T* volatile ptrv;</PRE>Use of volatile

- An object that is a memory-mapped I/O port
- An object variable that is shared between multiple concurrent processes
- An object that is modified by an interrupt service routine
- An automatic object declared in a function that calls setjmp and whose value is-changed between the call to setjmp and a corresponding call to longjmp
# An object that is a memory-mapped I/O port

an 8 bit , memory -mapped I/O port at physical address 0X15 can be declared as

char const ptr=(char*)0X15 ;</PRE>*ptr access the port consider following code fragment to set the third bit of the output port at periodic intervals

*ptr = 0;while(*ptr){ *ptr = 4 ; *ptr = 0 ;}</PRE>the above code may be optimize as

*ptr = 0while(0) {}</PRE>*ptr is assigned 0 before value 4 is used ( and value of *ptr never changes ( same constant is always assigned to it)
volatile keyword is used to suppress these optimization the compiler assumes that the value can change any time , even if no explicit code modify it to suppress all optimization declare ptr as

volatile char * const ptr = (volatile char*)0x16</PRE>this declaration say the object at which ptr points can change without notice , but that ptr itself is a constant whose value never change
# An object that is shared between multiple concurrent processes

if two threads/tasks concurrently assign distinct values to the same shared non-volatile variable, a subsequent use of that variable may obtain a value that is not equal to either of the assigned values, but some implementation-dependent mixture of the two values. so a global variable references by multiple thread programmers must declare shared variable as volatile.

#include<THREAD.H>#include<STDIO.H>volatile int num ;void* foo(){while(1) { ++num ; sleep(1000); }}main(){ int p ; void *targ =NULL ; thread_t id ; num = 0; p = thr_create((void*)NULL , 0,foo,targ,0,&id); if(!p) printf(" can not create thread "); while(1) { printf("%d" , num ) ; }}</STDIO.H></THREAD.H></PRE>the compiler may use a register to store the num variable in the main thread , so the change to the value by the second thread are ignored . The volatile modifier is a away of telling the compiler that no optimization applied to the variable its value be not placed in register and value may change outside influence during evaluation

# An automatic object declared in a function that calls setjmp and whose value is-changed between the call to setjmp and a corresponding call to longjmp

Variables local to functions that call setjmp is most often used. When an automatic object is declared in a function that calls setjmp, the compilation system knows that it has to produce code that exactly matches what the programmer wrote. Therefore, the most recent value for such an automatic object will always be in memory (not just in a register) and as such will be guaranteed to be up-to-date when longjmp is called. Without volatile variable is undefined by the standard whether the result one see differs with or without optimization simply reflects that fact Consider following code

#include<SETJMP.H>static jmp_buf buf ;main( ){ volatile int b; b =3 ; if(setjmp(buf)!=0) { printf("%d ", b) ; exit(0); } b=5; longjmp(buf , 1) ; }</SETJMP.H></PRE>volatile variable isn't affected by the optimization. So value of b is after the longjump is the last value variable assigned. Without volatile b may or may not be restored to its last value when the longjmp occurs. For clear understanding assembly listing of above program by cc compiler has been given below.

/*Listing1: Assembly code fragment of above program Produced by cc compiler when volatile is used*/.file "vol.c"main: pushl %ebp movl %esp, %ebp subl $20, %esp movl $3, -4(%ebp) pushl $buf call _setjmp addl $16, %esp testl %eax, %eax je .L18 subl $8, %esp movl -4(%ebp), %eax pushl %eax pushl $.LC0 call printf movl $0, (%esp) call exit .p2align 2.L18: movl $5, -4(%ebp) subl $8, %esp pushl $1 pushl $buf call longjmp</PRE>


/*Listing 2:Assemply code fragment of above program produced by cc compile witout volatile keword */.file "wvol.c"main: pushl %ebp movl %esp, %ebp subl $20, %esp pushl $buf call _setjmp addl $16, %esp testl %eax, %eax je .L18 subl $8, %esp pushl $3 pushl $.LC0 call printf movl $0, (%esp) call exit .p2align 2.L18: subl $8, %esp pushl $1 pushl $buf call longjmp</PRE>


/listing 3: difference between listing 1 and listing 3 ***/< .file "vol.c"---> .file "wvol.c"< movl $3, -4(%ebp) < movl -4(%ebp), %eax< pushl %eax> pushl $3< movl $5, -4(%ebp) / * store in stack */</PRE>
From above listing 3 it is clear that when you use setjmp and longjmp, the only automatic variables guaranteed to remain valid are those declared volatile.
# An object modified by an interrupt service routine

Interrupt service routines often set variables that are tested in main line code. One problem that arises as soon as you use interrupt is that interrupt routines need to communicate with rest of the code .A interrupt may update a variable num that is used in main line of code .An incorrect implementation of this might be:

static lon int num ;void interrupt update(void){++num ;}main(){long val ;val = num ;while(val !=num)val = num ;rturn val ;}</PRE>When compilation system execute while statement, the optimizer in compiler may notice that it read the value num once already and that value is still in the register. Instead of re reading the value from memory, compiler may produce code to use the (possibly messed up) value in the register defeating purpose of original C program. Some compiler may optimize entire while loop assuming that since the value of num was just assigned to val , the two must be equal and condition in while statement will therefore always be false .To avoid this ,you have to declare num to be volatile that warns compilers that certain variables may change because of interrupt routines.

static volatile long int num ;</PRE>With volatile keyword in the declaration the compiler knows that the value of num must b read from memory every time it is referenced.
Conclusion

When using exact semantics is necessary, volatile should be used. If you are given a piece of touchy code given as above. It is a good idea in any case to look the compiler outputting listing at the assembly language to be sure that compiler produce code that makes sense.
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -8. The time now is 09:56 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228