简体   繁体   中英

HP-UX Itanium Compare and Swap

I am developing C/C++ cross-platform code, and the last platform is Itanium based HP-UX. Relevant machine an processor information can be found at the end of the question.

I need to implement or find an atomic compare and swap for the machine and compiler specifications given below.

I have found a few possibilities for solutions, but haven't been able to find how to use them.

The first possible solution is through the use of _Asm_cmpxchg ( documentation here ). I'm unable to find what header to include for this or how to get it to compile.

The second possible solution is to write my own inline assembly with the direct use of the cmpxchg and cmpxchg8b commands, but I haven't been able to find how to correctly do this either. I've found various resources, most of which are directly writing assembly, not for the processor architecture I require, or don't show a specific enough example.

I found more documentation about cmpxchg and cmpxchg8 instructions (as well as tzcnt and lzcnt which are two that are nice to have, but not necessary) here . If you are viewing in google chrome, abosulte page values are 234 for cmpxchg and 236 for cmpxchg8.

Limitations: I am unable to use a third party library due to constraints beyond my control.

Result of uname -smr: HP-UX B.11.31 ia64

Processor Model: Intel(R) Itanium(R) Processor 9340

Compiler -v: aCC: HP C/aC++ B3910B A.06.28

Update: I was able to get _Asm_cmpxchg to compile, but it doesn't seem to work (the value remains unchanged). For parameters, I passed _SZ_W for the _Asm_sz, _SEM_ACQ for _Asm_sem, _LDHINT_NONE for _Asm_ldhint, a pointer to the original 32 bit integer value for r3, and the desired new value for r2. I'm guessing at the meaning of the parameters, given that documentation is very lackluster.

I ended up finding the solution on my own, using option 1. Below is the sample code to get it to work:

bool compare_and_swap(unsigned int* var, unsigned int oldval, unsigned int newval)
{
    // Move the old value into register _AREG_CCV because this is the register
    // that var will be compared against
    _Asm_mov_to_ar(_AREG_CCV, oldval);
    // Do the compare and swap
    return oldval == _Asm_cmpxchg(
        _SZ_W /* 4 byte word */, 
        _SEM_ACQ /* acquire the semaphore */, 
        var, 
        newval, 
        _LDHINT_NONE /* locality hint */);
}

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