I was reading Operating System Principles by Abrahim, Galvin. In process synchronization topic, following definition is given for TestAndSet function which is later used to ensure mutual exclusion:
boolean TestAndSet(boolean *target) {
boolean rv = *target;
*target = TRUE;
return rv;
}
It is mentioned in the book that this instruction is a special hardware instruction and it is executed atomically. My question is how do I implement this in C? Do I need to provide its definition as I do for other user defined functions or do I need to import some library which already implements this? If the case is former and I define the function myself, what is the surety that it will be executed atomically?
By the word atomically what I understand is that it will be executed as a single instruction, i.e., if two processes call this function then the called functions will be executed sequentially and their statements would not be interleaved(not even after conversion to machine language).
Please Help. I have a lot of difficulties in understanding the text of this book.