0

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.

shiva
  • 2,535
  • 2
  • 18
  • 32
  • Such functionality is provided by your ABI and OS, not directly by C. Check out your OS API first before going near the intercore-lock instructions available on your processor. – Martin James Oct 20 '15 at 19:47
  • http://stackoverflow.com/questions/2287451/how-to-perform-atomic-operations-on-linux-that-work-on-x86-arm-gcc-and-icc – kaylum Oct 20 '15 at 19:49

1 Answers1

0

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?

The book provided a C function, which you presented, that is intended to explain the behavior. It already does everything, except without any guarantee of atomicity.

C2011 provides essentially what you describe, pre-built, in the form of atomic_flag_test_and_set(). If you can assume C2011 as your implementation language then you can use that.

Earlier versions of standard C do not provide what you would need to implement such a function, however, and indeed, even C2011 does not provide mechanisms for you to implement your own atomic operations without relying on C's built-in atomic types and functions.

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?

The code presented seems intended to be explanatory rather than functions. Atomic test-and-set behaves as that function would, if that function could be guaranteed to operate atomically.

If the case is former and I define the function myself, what is the surety that it will be executed atomically?

There is none.

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).

It's perhaps more descriptive to say that every possible observer will see either the state before the atomic operation starts, or the state after it completes, never a state where it is somewhere in the middle.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thank you. However I cant find any code implementing atomic testandset. Can you please help me. – shiva Jan 04 '16 at 10:26
  • @shiva, I cannot help you any further than my answer already does. If you get nothing else from it, at least get that there is no way to roll your own atomic test-and-set in C. You would need to implement it in assembly, though you might *wrap* the assembly routine in a C function\. – John Bollinger Jan 04 '16 at 18:32