Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C atomic read modify write

Tags:

c

atomic

powerpc

Are there any functions in C to do atomic read-modify-write? I'm looking to read a value, then set to 0, in a single atomic block.

For C++ there is std::atomic::exchange() which is exactly what I'm looking for. Is there something equivalent in C?

Here's the code:

void interruptHandler(void) {
    /* Callback attached to 3rd party device driver, indicating hardware fault */
    /* Set global variable bit masked flag to indicate interrupt */
    faultsBitMask |= 0x1;
}

void auditPoll(*faults) {
    *faults = faultsBitMask;
    /* !!! Need to prevent interrupt pre-empt here !!! */
    /* Combine these two lines to a single read-modify-write? */
    faultsBitMask = 0;
}

The target architecture is PowerPC.

Thanks for the help!

like image 362
Splaty Avatar asked Nov 01 '25 05:11

Splaty


1 Answers

Yes, the <stdatomic.h> header contains a type-generic function atomic_exchange that's very similar to the C++ version:

_Atomic int n = 10;

#include <stdatomic.h>

int main(void) { return atomic_exchange(&n, 0); }
like image 112
Kerrek SB Avatar answered Nov 02 '25 22:11

Kerrek SB