Last active
September 30, 2022 14:56
-
-
Save baioc/f879a5e53ae3238173ba207247fe466b to your computer and use it in GitHub Desktop.
Spin-Lock Mutex in MIPS-32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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