This code only serves to help explain the behaviour of test-and-set; atomicity requires explicit hardware support and hence can't be implemented as a simple function.
Although implementations vary depending on factors such as concurrency issues, the principle of atomicity i.e. complete success or complete failure remain.