Multicore & Synchronization
With an instruction pair such as RISC-V LR/SC (load-reserved / store-conditional), when does the SC fail?
- AWhen the value SC writes equals the value LR read
- BWhen the reservation is lost, for example because another core wrote that address between the LR and the SC
- CWhenever any other instruction is executed between LR and SC
Show answer and explanation
Answer: B. When the reservation is lost, for example because another core wrote that address between the LR and the SC
LR reads a value and at the same time "reserves" the address. When SC later writes, it succeeds if the reservation is intact and fails without writing if the reservation was lost, for example because another core wrote there. Whereas CAS checks for a matching value, LR/SC checks whether any write happened, so it also detects a value that changed and changed back. Arm has a similar instruction pair.
- A. Writing the same value still succeeds.
- B. Correct. It checks whether a write occurred, not the value.
- C. A short sequence of instructions may come in between.