Pegboy said:
What does it mean to make a function atomic?
Something I read on it wasn't very clear, but made me think that I needed to
disable interrupts for that function. Whether that's the case or not, how
would I make the following function atomic?
int Test;
int GetTestValue( void )
{
return Test;
}
It means that the entire function is completed without any intervening
actions. That is a = b + c fetches the two operands, does the add and stores
the result in a without any outside observer being able to insert anything
else into the "cracks". As far as I know this is impossible with the normal
instructions available to a C programmer, in my experience it requires a
special hardware instruction called, perhaps, "test & set" in which the
memory read and write are tightly bound to each other. So if you have
multiple processors, multiple threads, caches, and so on the code still
works. I saw some hand waving implying wonderful breakthroughs in
semaphores WRT mutexes a few days ago in one of these groups. but I wasn't
sufficiently enthused or believing to chase that rabbit,
I would expect modern microprocessors to have the needed special instruction
.. But it might be reserved for the OS to handle as part of the API or pass
through at it's discretion. Keywords mutex, semaphore, flag, "test +and
set" .....
The test & set instruction protects a body of code. Test & set a flag, do
your thing, clear the flag. Only the one instruction was special.