Otherwise, return the value false to the calling program.
Some boolean variable called lock is used to lock or unlock the resource. If the resource is not
locked at the time of the call, the instruction will lock it and return true meaning, ???It??™s OK to proceed.???
If the resource is locked at the time of the call, the instruction will return false meaning, ???You must
wait.???
If such an instruction is part of the machine instruction set, code like this can take advantage of it:
boolean thisLock;
. . .
while( !testAndSet( thisLock ) ) { /* Do nothing */ }
/* Now do whatever needs to be done in isolation */
. . .
/* Now free the lock */
thisLock = false;
. . .
/* Carry on */
. . .
This code says:
While testAndSet returns false (!testAndSet), do nothing.
When testAndSet returns true,
do whatever needs to be done in the critical section.
(Note that the locking variable is now set ???true, and competing
processes using the same testAndSet routine will have to wait.)
When the critical section is complete,
indicate that by changing thisLock, the locking variable, to false.??™
Because the test-and-set instruction requires a busy wait, it often is not the best choice of synchronization
mechanism.
Pages:
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288