473,383 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

register caching of static variables

As far as I understood the standard, non-automatic variables
(static/global data) can't be cached in registers because in a
multiprocessing or multi-threading environment, another
thread/processor could read a wrong value from main
memory, if the compiler would do so

Even if the variable has local scope (static variable enclosed
in a function scope) this would still hold since another
processor/thread could run the same code at the same time
and read a wrong value from memory when the variable
lives in a register.

At each sequence point the compiler is supposed to present a
coherent view of memory, with all assignments stored into
main memory.

The standard says:

" At certain specified points in the execution sequence called
sequence points, all side effects of previous evaluations shall
be complete and no side effects of subsequent evaluations
shall have taken place."

This can be guaranteed in a multi-processing/multi-threaded
environment only for automatic variables, not globals
or static variables.

Is this reasoning correct?

jacob
Nov 14 '05 #1
6 4884
"jacob navia" <ja***@jacob.remcomp.fr> wrote in news:c045ub$l09$1@news-
reader5.wanadoo.fr:
As far as I understood the standard, non-automatic variables
(static/global data) can't be cached in registers because in a
multiprocessing or multi-threading environment, another
thread/processor could read a wrong value from main
memory, if the compiler would do so Even if the variable has local scope (static variable enclosed
in a function scope) this would still hold since another
processor/thread could run the same code at the same time
and read a wrong value from memory when the variable
lives in a register.

At each sequence point the compiler is supposed to present a
coherent view of memory, with all assignments stored into
main memory.

The standard says:

" At certain specified points in the execution sequence called
sequence points, all side effects of previous evaluations shall
be complete and no side effects of subsequent evaluations
shall have taken place."

This can be guaranteed in a multi-processing/multi-threaded
environment only for automatic variables, not globals
or static variables.

Is this reasoning correct?

jacob


As far as the C standard is concerned, threads don't exist. AFAIK, a C
compiler can, if it chooses, hold static and/or global data in registers.
Only objects which are of type volatile sig_atomic_t are guarenteed to
have a nice state during an interupt in execution (i.e. signal handlers).
There's no other mechanism for 'interrupting' the execution of a C
program in the C standard AFAIK.

Individual implementations might make some guarentees about the behaviour
of different kinds of objects wrt threads, but they're extensions and not
part of C itself.

Ian Woods

--
"I'm a paranoid schizophrenic sado-masochist.
My other half's out to get me and I can't wait."
Richard Heathfield
Nov 14 '05 #2
"jacob navia" <ja***@jacob.remcomp.fr> wrote:
# As far as I understood the standard, non-automatic variables
# (static/global data) can't be cached in registers because in a
# multiprocessing or multi-threading environment, another
# thread/processor could read a wrong value from main
# memory, if the compiler would do so
#
# Even if the variable has local scope (static variable enclosed
# in a function scope) this would still hold since another
# processor/thread could run the same code at the same time
# and read a wrong value from memory when the variable
# lives in a register.
#
# At each sequence point the compiler is supposed to present a
# coherent view of memory, with all assignments stored into
# main memory.
#
# The standard says:
#
# " At certain specified points in the execution sequence called
# sequence points, all side effects of previous evaluations shall
# be complete and no side effects of subsequent evaluations
# shall have taken place."

That has to do with all aliases in one process referring to the same result.
If you don't take the address of the variable, you don't have aliasses.

# This can be guaranteed in a multi-processing/multi-threaded
# environment only for automatic variables, not globals
# or static variables.

That's what volatile is for: noting variables that can be changed by
something outside the current process.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Title does not dictate behaviour.
Nov 14 '05 #3

"Ian Woods" <ne******@wuggyNOCAPS.org> wrote in message
news:Xn*****************************@217.32.252.50 ...
"jacob navia" <ja***@jacob.remcomp.fr> wrote in news:c045ub$l09$1@news- reader5.wanadoo.fr:

As far as the C standard is concerned, threads don't exist. AFAIK, a C compiler can, if it chooses, hold static and/or global data in registers. Only objects which are of type volatile sig_atomic_t are guarenteed to have a nice state during an interupt in execution (i.e. signal handlers). There's no other mechanism for 'interrupting' the execution of a C
program in the C standard AFAIK.

Individual implementations might make some guarentees about the behaviour of different kinds of objects wrt threads, but they're extensions and not part of C itself.

Ian Woods


Consider:
static int a;
int fn(void)
{
a = a+1;
// sequence point <S> here
return a;
}

The compiler is supposed to have done the assignment when
the sequence point <S> is reached. If the variable is cached
in a register that is not the case.

The storage class of the variable has been changed from static to
register.

Note that I dropped any references to threads here.

Nov 14 '05 #4
jacob navia wrote:
As far as I understood the standard, non-automatic variables
(static/global data) can't be cached in registers
Correct, because...
because in a
multiprocessing or multi-threading environment, another
thread/processor could read a wrong value from main
memory, if the compiler would do so
Correct if you replace threads with signals.
Consider:
static int a;
int fn(void)
{
a = a+1;
// sequence point <S> here
return a;
}

The compiler is supposed to have done the assignment when
the sequence point <S> is reached. If the variable is cached
in a register that is not the case.
This is not correct. The implementation is required to act _as if_
variable updates happened at each sequence point, but only I/O and
volatile variables are required to actually be updated at those times.
If the compiler can prove that `a' will not be accessed during the
execution of some code, it need not ensure that `a' has the correct
value while that code is executing.

However, since the compiler would have to analyze the entire program to
check that no signal handlers are installed (or threads, if the compiler
is aware of threading), you are mostly right in practice. I doubt many
compilers do so. Maybe some compilers support pragmas to inform the
compiler that no thread/signal handler will do that to a particular set
of variables or in a particular translation unit, but I don't know if
you consider such extensions relevant to your question or not.
The storage class of the variable has been changed from static to
register.


No, it hasn't. The static/register storage classes are features of
variables in the abstract machine. The actual program only need to
behave _as if_ it executed the abstract machine.

--
Hallvard
Nov 14 '05 #5
In article <c0**********@news-reader3.wanadoo.fr>
jacob navia <ja***@jacob.remcomp.fr> writes:
Consider:
static int a;
int fn(void)
{
a = a+1;
// sequence point <S> here
return a;
}

The compiler is supposed to have done the assignment when
the sequence point <S> is reached. If the variable is cached
in a register that is not the case.


Here the variable "a" has file scope and internal linkage. A
C compiler would be allowed to rewrite the code as:

int fn(void) {
static int a;
register int temp = a;
... do all the work in temp ...
a = temp;
return a;
}

*if* it can prove that no other code in this translation unit
refers to this same object "a".

(The original code in fn() is short and simple enough that this
transformation would likely gain nothing as is, but if the code
were more complex than just "a = a + 1" it might be worthwhile.
Similar arguments apply if the code in fn() is hoisted into its
callers -- but note that fn() itself has file scope and external
linkage, so unless compilation is done at link time, there must
be a "vanilla version" accompanying the inline expanded versions.)

If fn() calls other functions which might drop back into "this
translation unit" and refer to the same "a", then the register
caching can *still* occur provided the cached value is stored
back into "memory" before and after those points. The variable
"a" may need to move back to file scope (from the block scope I
gave it above). If "a"'s address is never taken (or if the CPU
provides &cpuregister in hardware, as a few do) it would be
legal to use a "global CPU register" for the variable "a".

The "Big Hammer spelled volatile" is the way to tell a compiler
"please do not make provably-correct-according-to-the-Standard
optimizations", as one might need for multi-threaded code here. :-)
Most compilers simply do not bother attempting to optimize in these
cases, though, as the list of exceptions to "allowed register
shadowing" gets complicated quickly, and in most cases I suspect
there is not much benefit.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
In article <c0**********@news-reader5.wanadoo.fr>,
"jacob navia" <ja***@jacob.remcomp.fr> wrote:
As far as I understood the standard, non-automatic variables
(static/global data) can't be cached in registers because in a
multiprocessing or multi-threading environment, another
thread/processor could read a wrong value from main
memory, if the compiler would do so

Even if the variable has local scope (static variable enclosed
in a function scope) this would still hold since another
processor/thread could run the same code at the same time
and read a wrong value from memory when the variable
lives in a register.

At each sequence point the compiler is supposed to present a
coherent view of memory, with all assignments stored into
main memory.

The standard says:

" At certain specified points in the execution sequence called
sequence points, all side effects of previous evaluations shall
be complete and no side effects of subsequent evaluations
shall have taken place."

This can be guaranteed in a multi-processing/multi-threaded
environment only for automatic variables, not globals
or static variables.


The C Standard doesn't mention threads at all, so whatever a second
thread does lies outside the scope of the C Standard. So if a second
thread modifies a non-volatile static variable that my code accesses,
that's outside the C Standard already.

If you are interested, you might take a look at the Java Language and
Java Virtual Machine specifications, where this kind of stuff is
handled.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Duncan Welch | last post by:
Good morning, I have a classic ASP app that I'm converting to .NET. In the existing app when accessing infrequntly changed data, it reads a database once a day, and saves the results in an...
6
by: kevin | last post by:
Could someone explain, Caching. I understand Caching as simply saving a copy of something that is unlikely to change and to which you frequently refer as opposed to retrieving a fresh copy every...
0
by: Martin | last post by:
Hi. I had a very frustrating afternoon and evening but I have got it all under control now so all of a sudden I am in a good mood. I want to share some insights on output caching with you lot. ...
7
by: int main(void) | last post by:
Hi all, I know that register variables work fine at block scope. I tried putting a register variable in file scope, like this, #include <stdio.h> register int a = 2; int main(void) {...
33
by: Snis Pilbor | last post by:
With the "as if" rule in play, doesn't that effectively render the "register" keyword completely useless? Example: I make a silly compiler which creates code that goes out of its way to take a...
28
by: sowmiyakc18 | last post by:
Please clear my doubt. When do we declare a variable to be a register variable? What is its significance? What are the conditions to be adhered to when register variables are passed between...
1
by: Dinesh143 | last post by:
Where it is store the variables auto,static,extern,register,typedef,_declspec?
26
by: Vashna | last post by:
Hi Group, I have a doubt about register variables. I know that if we have a variable used very frequently in a function, then provided we never apply the & function to it, we can define it as...
21
by: JOYCE | last post by:
Look the subject,that's my problem! I hope someone can help me, thanks
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.