Question: CPU often provides atomic instructions for lock implementations. For example, x86s xchg instruction atomically exchanges the contents of two memory addresses. Implement a spin lock
CPU often provides atomic instructions for lock implementations. For example, x86s xchg instruction atomically exchanges the contents of two memory addresses. Implement a spin lock with this instruction, assuming xchg has been provided as a library function
// xchg sets the memory (addr) to newval, and returns the old value.
extern int xchg (int *addr, int newval);
struct spinlock { unsigned int locked;
};
{
}
void spinlock_release (struct spinlock *lock)
{
void spinlock_acquire (struct spinlock *lock)
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
