# 64 bit atomic in 32 bit machine



## viswesh (Sep 2, 2011)

Hi,

I am working in a 32 bit machine, where i need to use atomic on 64 bit variables.

Is there a wrapper provided in freebsd, which i could use.

Had a look at the man page of atomic - but couldnt find anything.

One option, which i think, if there is no inbuilt wrapper, is to have a structure with mtx and the 64 bit variable and use the mtx to protect it.

Any other ideas ?

Thanks,
Viswesh


----------



## expl (Sep 6, 2011)

Atomic wrappers can not protect memory operations on variables bigger than word for your architecture. Like you said you should use a mutex to solve this.


----------



## jilles@ (Sep 8, 2011)

In the particular case that this 32 bit machine has a Pentium or higher x86 CPU, the CPU provides a 64-bit atomic compare-and-exchange operation CMPXCHG8B. The PAE kernel uses this instruction, see src/sys/i386/include/pmap.h and look for atomic_cmpset_64. This operation is however not generally available. (FreeBSD wants to support 32-bit architectures that do not have such an operation.)

It appears to work to use GCC's __sync_bool_compare_and_swap builtin function with 64-bit operands, provided an appropriate -march= option is given (by default, FreeBSD's GCC will generate code that will run on a 486 and thus cannot use CMPXCHG8B).


----------

