9.135 __strex intrinsic

The __strex intrinsic lets you use an STREX[size] instruction in your C or C++ code to store data to memory.

Note

The compiler does not guarantee that it will preserve the state of the exclusive monitor. It may generate load and store instructions between the LDREX instruction generated for the __ldrex intrinsic and the STREX instruction generated for the __strex intrinsic. Because memory accesses can clear the exclusive monitor, code using the __ldrex and __strex intrinsics can have unexpected behavior. Where LDREX and STREX instructions are needed, ARM recommends using embedded assembly.

Syntax

int __strex(unsigned int val, volatile void *ptr)
Where:
val
is the value to be written to memory.
ptr
points to the address of the data to be written to in memory. To specify the size of the data to be written, cast the parameter to an appropriate integral type.

Table 9-11 Access widths that the __strex intrinsic supports

Instruction Size of data stored Pointer type
STREXB byte char *
STREXH halfword short *
STREX word int *

Return value

The __strex intrinsic returns:
0
if the STREX instruction succeeds
1
if the STREX instruction is locked out.

Errors

The compiler does not recognize the __strex intrinsic when compiling for a target that does not support the STREX instruction. The compiler generates either a warning or an error in this case, depending on the source language:
  • In C code: Warning: #223-D: function "__strex" declared implicitly.
  • In C++ code: Error: #20: identifier "__strex" is undefined.
The __strex intrinsic does not support access to doubleword data. The compiler generates an error if you specify an access width that is not supported.

Examples

int foo(void)
{
    int loc=0xff;
    return(!__strex(0x20, (volatile char *)loc));
}
Compiling this code with the command-line option --cpu=6k produces
||foo|| PROC
    MOV      r0,#0xff
    MOV      r2,#0x20
    STREXB   r1,r2,[r0]
    CMP      r1,#0
    MOVEQ    r0,#1
    MOVNE    r0,#0
    BX       lr
    ENDP
Related reference
9.113 __ldrex intrinsic
9.114 __ldrexd intrinsic
9.136 __strexd intrinsic
Related information
STREX
Non-ConfidentialPDF file icon PDF versionARM DUI0375E
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.