AnsweredAssumed Answered

Atomic operations: fetch and add

Question asked by Colin Cameron on Aug 10, 2015
Latest reply on Aug 10, 2015 by Colin Cameron

I'm trying to build a library of lock-less atomic operations for the 56800EX DSC. I can achieve basic reads and stores and also use the atomic bit mask operations for bit manipulation.


However I'm trying to work out if it's possible to build a function with a prototype like:


int atomic_fetch_and_add(atomic_t a, int i);


Which would add 'i' to the atomic value 'a' (which is really just an int) and then return the value of 'a' prior to the add. I imagine this will be an inline assembly function (the others I've do so far are). But as far as I can see there's no way to do this without turning off interrupts because the read and add actions have to be separate. I can't use a parallel move because I can't directly add to a memory location at the same time. I'm wondering if there's some instruction that I've missed that would help with this. Alternatively I could make the atomic_t more complex and have some sort of flag to detect an interrupted access and attempt the action again, but I can't see how to do that either.


Perhaps someone else has done this, or something similar. Obviously this function (and related sub/inc/dec functions) would be very useful in controlling a data structure that was shared between multiple interrupt levels. If it's not possible then I'd have to look at some other solution to provide safe data structures.