master-degree-notes/Concurrent Systems/notes/3.md

1 KiB

Hardware primitives

Atomic R/W registers provide quite a basic computational model.

We can strenghten the model by adding specialized HW primitives, that essentially perform in an atomic way the combination of some atomic instructions. Usually, every operating system provides at least one specilized HW primitive.

Most common ones:
  • Test&set: atomic read+write of a boolean register
  • Swap: atomic read+write of a general register (generalization of the above)
  • Fetch&add: atomic read+increase of an integer register
  • Compare&swap: tests the value of a general register, returns a boolean (result of the comparison, true if it is the same).

Test&Set

Let X be a boolean register

X.test&set() :=
	tmp <- X
	X <- 1
	return tmp

(the function is implemented in an atomic way by the hardware by suspending the interruptions!)
How do we use it?
lock() :=
	wait X.test&set() = 0
	return


unlock() :=
	X <- 0
	return

Swap

X general register

X.swap(v) :=
	tmp <- X
	X <- v
	return tmp