avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Avr-libc-user-manual: "Problems with reordering code"


From: Stu Bell
Subject: Re: [avr-gcc-list] Avr-libc-user-manual: "Problems with reordering code"
Date: Thu, 9 Feb 2017 19:58:47 -0700
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1

I hate to stick my nose in this after years away from the list, but... David is right about the problems with atomic functions in C. The good news is that there /is/ another solution: Use assembly language. That does not solve the problem within the domain of C, but it /does/ solve the problem you're having. And, as I'm sure someone will point out, you can use the intermediate assembly generated from C to create your custom "atomic" code. Does this mean the atomic.h functions are misleading? No more or less than someone assuming they know what an RTOS is; The problem is in the assumption.

This is an old argument about atomic C. Been discussed to death. Assembly was the only solution that seemed reasonable at the time, and I've seen nothing to change my mind on it. I've been hip-deep in ARM code for the last 4 years and David's observations are just as true there, bu definition. And I've heard the same howls of anguish from those hit with code moved over an ISB boundary.

If you're dealing with Real Time Systems, you need to be completely familiar with the tool set you're using, just as much as the hardware. This is a wart, a product of the C definition. It is what it is. Accept it and move on.

Stu Bell

PS: Yeah, I still write volumes when a sentence (or silence) will do. :-)

On 2/9/2017 1:20 PM, David Brown wrote:
On 09/02/17 19:49, Bob Paddock wrote:
On Thu, Feb 9, 2017 at 1:13 PM, David Brown <address@hidden> wrote:
So the only solution is to use different parts, such as ARM, with its
Instruction Synchronization Barrier (ISB) [that can waste thousands of
cycles]?


No. That won't help. The compiler can re-arrange "harmless" execution and calculations around an inline assembly instruction with the "ISB" opcode (or any similar opcode), even if the inline assembly is marked "volatile". Given "isb(); x = a/b; isb(); return x;" the compiler is free to order the division before or after either of these isb() calls, if the compiler knows that the isb() function/macro/inline assembly does not affect the results of the calculation.

In C, the only things that can be ordered are /visible/ effects. Those are volatile memory accesses, file I/O, program start/stop, and calling external code with unknown effect (since that external code could have visible effects). C11 adds some atomic access and synchronisation functions, and C implementations can add more - gcc adds volatile inline assembly. Other things - non-volatile memory accesses, and calculations, can be shuffled around at will, including back and forth across volatile accesses.

The only way I know of to force control of the order of execution is to make "visible" dependencies on the results or the perquisites to the calculations. (And if the calculation does not have any results, it is not actually needed at all as far as C is concerned.) You have to use the techniques I gave in my earlier posts here - they are as convenient, safe, and efficient as it gets. But it does mean that there is /no/ "general" execution barrier, in the same way that "asm ("":::"memory")" is a general memory barrier.

If someone knows differently, I'd be happy to be corrected here.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]