Skip to content

Instantly share code, notes, and snippets.

@baioc
Last active September 30, 2022 14:56
Show Gist options
  • Save baioc/f879a5e53ae3238173ba207247fe466b to your computer and use it in GitHub Desktop.
Save baioc/f879a5e53ae3238173ba207247fe466b to your computer and use it in GitHub Desktop.
Spin-Lock Mutex in MIPS-32
.text
# void lock(mutex)
lock:
ll $t0, 0($a0) # read the mutex (and set LLbit)
bne $t0, $zero, lock # if occupied (1), start over
addi $t1, $zero, 1
sc $t1, 0($a0) # try grabing the mutex
beq $t1, $zero, lock # if wasn't atomic (0), start over
# return only when sucessfully grabbed the mutex in an atomic exchange
jr $ra
# void unlock(mutex)
unlock:
sw $zero, 0($a0)
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment