Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between volatile and __volatile__

Tags:

c

gcc

volatile

What is the difference between volatile and __volatile__ in C code compiled with gcc?

I was looking in the Linux source code and I noticed that some places use __asm__ __volatile__ others use asm volatile and others use __asm__ volatile.

I have not seen __volatile__ used without __asm__, while I have seen volatile used in a variety of other places.

Is there any difference between what __volatile__ and volatile do? If so what is it? Or if not is there a reason that __volatile__ is used sometimes?

like image 318
Gabriel Southern Avatar asked Dec 06 '22 10:12

Gabriel Southern


2 Answers

This is just for backward compatibility with legacy code. Keywords that were added to C in later life, such as inline, volatile, asm, etc, have underscore prefix/suffix versions (__inline__, __asm__, __volatile__, etc) so that you can use them in legacy code that already uses the unadorned names as types/functions/variables/whatever. There is a command line switch which controls this, -ansi - when you compile with gcc -ansi ... only the underscore versions of these newer keywords are recognised.

like image 78
Paul R Avatar answered Dec 28 '22 06:12

Paul R


Because of -ansi

Although a real -ansi program would not use asm(), there needs to be a way to include an asm macro in a header even when the program itself is built with -ansi. (With -ansi, gcc doesn't include extensions which conflict with strict ISO C, like new keywords.)

A properly namespaced alternative __asm__ was necessary for asm, which could not be defined by the system in -ansi mode. Now, this wasn't strictly necessary with volatile, which has been a keyword for a long time, but perhaps by inertia someone also (and unnecessarily) made a __volatile__ (reserved system name format) to go with volatile, even if the program would have been legal -ansi either way.

like image 36
DigitalRoss Avatar answered Dec 28 '22 07:12

DigitalRoss