How to perform atomic operations on Linux that work on x86, arm, GCC and icc?

前端 未结 9 1556
無奈伤痛
無奈伤痛 2020-12-02 11:52

Every Modern OS provides today some atomic operations:

  • Windows has Interlocked* API
  • FreeBSD has
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 12:44

    __sync* certainly is (and has been) supported by the Intel compiler, because GCC adopted these build-ins from there. Read the first paragraph on this page. Also see "Intel® C++ Compiler for Linux* Intrinsics Reference", page 198. It's from 2006 and describes exactly those built-ins.

    Regarding ARM support, for older ARM CPUs: it cannot be done entirely in userspace, but it can be done in kernelspace (by disabling interrupts during the operation), and I think I read somewhere that it is supported for quite a while now.

    According to this PHP bug, dated 2011-10-08, __sync_* will only fail on

    • PA-RISC with anything other than Linux
    • SPARCv7 and lower
    • ARM with GCC < 4.3
    • ARMv5 and lower with anything other than Linux
    • MIPS1

    So with GCC > 4.3 (and 4.7 is the current one), you shouldn't have a problem with ARMv6 and newer. You shouldn't have no problem with ARMv5 either as long as compiling for Linux.

提交回复
热议问题