简体   繁体   中英

Atomic reads/writes of 64 bit values in 32 bit mode with gcc

Is there any intrinsic or other way in gcc of persuading it to do 64 bit reads and writes atomically rather than as 2 32 bit writes? (At least in one or two places).

Currently I'm having to do __sync_lock_test_and_set and __sync_fetch_and_add to get it to do the reads/writes atomically, but the CPUs in question have 64 bit fetch and store instructions.

I'd prefer not to write loads of inline assembler (presumably one for each CPU we have to support) with #ifs spread around.

I'm currently using gcc 4.4.6 and am unlikely to get that upgraded in the short term.

If you use "long long" the memory access won't be atomic.

However maybe a "double" memory access may be atomic when using a 64-bit CPU (even in a 32 bit program):

union {
    long long ll;
    double d;
} u;

u.ll = my_value;
*(double *)&my_long_long_variable = u.d;

However if the number "my_value" in this example will result in a "signalling NaN" value for ud then you may get an exception when doing this this way!

Maybe the compiler will not even use FPU registers for this code. In this case the memory access won't be atomic and assembler programming will be needed. You'll definitely need to use either FPU registers or XMM/MMX/... registers for atomic 64-bit accesses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM