Side Effects in ISRs and Signal Handlers Confuse OptimizerLast reviewed: July 17, 1997Article ID: Q44723 |
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50
MS-DOS | OS/2 | WINDOWSkbtool The information in this article applies to:
SUMMARYIt can be dangerous to allow the Microsoft C compiler to perform loop optimization on code that contains global variables that may be modified in interrupt service routines (ISR) or signal handlers. For example, consider the following code
int TimerCount = 1000; while( TimerCount ); printf( "Timer expired." );where an ISR is concurrently executing the following:
void interrupt far ISR() { TimerCount--; } MORE INFORMATIONThis is acceptable code and performs a simple task--it waits for a global timer variable to reach 0, indicating that the ISR has been executed a specified number of times. However, the loop optimization optionally performed by the compiler results in the while() loop being completely removed from the executable code. Because the compiler does not know that the ISR is simultaneously accessing TimerCount, it sees no reason to keep an expression that obviously evaluates to true and does nothing but waste time. This is NOT a problem with the compiler. Section 2.1.2.3 of the 7 December 1988 ANSI C draft standard states the following:
An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).Because C 5.1 does not semantically implement the "volatile" keyword, there is no way to indicate that TimerCount is volatile and that the expression involving TimerCount should not be removed from the program by the optimizer. Thus, it is reasonable for the compiler to assume that this expression is not necessary and may be safely removed. With C versions 6.0 and later, the volatile keyword has been implemented and can be used to solve this problem. When writing code for ISRs or signal handlers that expect conditions that the compiler cannot predict, care should be used in the choice of optimizations; if needed, an assembly code listing should be generated with the /Fc or /Fs /Fa switches and examined for the optimization's effect. Disabling optimization with /Od may result in slower executable programs but will prevent optimizer side effects.
|
Additional reference words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |