Download The Raven Kernel: a Microkernel for Shared Memory
Transcript
Chapter 3. Kernel level implementation 22 An optimization can be achieved by relying on the data cache to maintain a coherent copy of the lock variable. In this case, initially the processor spins on a cached copy of the lock, generating negligible memory bus accesses. If the lock is freed, the processor performs an xmem to grab the lock. If this xmem fails, then return to spinning on the cached copy. This algorithm is implemented as follows, and can be found in lock.s: void lock_wait(int *lock_addr); _lock_wait: ld bcnd or xmem bcnd jmp r10, ne0, r10, r10, ne0, r1 r2, 0 r10, _lock_wait r0, 1 r2, r0 r10, _lock_wait ; ; ; ; ; ; ; r2 <- lock variable addr read the lock value loop if lock is busy. set a lock value. atomic exchange try lock again if nonzero return to caller 3.2.3 lock free() implementation Freeing a lock is trivial. All that needs to be done is to store a \free" value to the lock variable. This can be accomplished in a single st instruction on the 88100. void lock_free(int *lock_addr); _lock_free: jmp.n r1 st r0, r2, 0 ; r2 <- lock variable addr ; return to caller ; store "free" value In this routine, the jmp.n r1 instruction demonstrates the use of delayed branching on the 88100. The instruction cycle immediately after a control transfer instruction, such as jump, is known as the delay slot. This is where the processor gures out where execution should continue. While it does this, another instruction can be executed. In this case, the st r0, r2, 0 instruction is executed in the jmp r1 delay slot. 3.2.4 Lock initialization Before using a spin lock, a lock variable must be allocated and initialized. The lock variable is of type int and can be allocated in any appropriate fashion, such as statically at compile time.