Last active
July 4, 2018 13:39
-
-
Save jeehoonkang/6dcb7b88d1817ce9d5788801f9e8c05a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <atomic> | |
class SpinLock { | |
std::atomic_flag locked = ATOMIC_FLAG_INIT ; | |
public: | |
void lock() { | |
while (locked.test_and_set(std::memory_order_acquire)) { ; } | |
} | |
void unlock() { | |
locked.clear(std::memory_order_release); | |
} | |
}; | |
int main() { | |
SpinLock l; | |
l.lock(); | |
l.unlock(); | |
} |
This file contains hidden or 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
std::operator&(std::memory_order, std::__memory_order_modifier): | |
sub sp, sp, #16 | |
str w0, [sp, 12] | |
str w1, [sp, 8] | |
ldr w1, [sp, 12] | |
ldr w0, [sp, 8] | |
and w0, w1, w0 | |
add sp, sp, 16 | |
ret | |
SpinLock::lock(): | |
sub sp, sp, #32 | |
str x0, [sp, 8] | |
.L6: | |
ldr x0, [sp, 8] | |
str x0, [sp, 24] | |
mov w0, 2 | |
str w0, [sp, 20] | |
ldr x0, [sp, 24] | |
mov w1, 1 | |
.L8: | |
ldaxrb w2, [x0] | |
stlxrb w3, w1, [x0] | |
cbnz w3, .L8 | |
uxtb w0, w2 | |
cmp w0, 0 | |
beq .L7 | |
b .L6 | |
.L7: | |
nop | |
add sp, sp, 32 | |
ret | |
SpinLock::unlock(): | |
stp x29, x30, [sp, -48]! | |
add x29, sp, 0 | |
str x0, [x29, 24] | |
ldr x0, [x29, 24] | |
str x0, [x29, 40] | |
mov w0, 3 | |
str w0, [x29, 36] | |
mov w1, 65535 | |
ldr w0, [x29, 36] | |
bl std::operator&(std::memory_order, std::__memory_order_modifier) | |
str w0, [x29, 32] | |
ldr x0, [x29, 40] | |
stlrb wzr, [x0] | |
nop | |
ldp x29, x30, [sp], 48 | |
ret | |
main: | |
stp x29, x30, [sp, -32]! | |
add x29, sp, 0 | |
strb wzr, [x29, 24] | |
add x0, x29, 24 | |
bl SpinLock::lock() | |
add x0, x29, 24 | |
bl SpinLock::unlock() | |
mov w0, 0 | |
ldp x29, x30, [sp], 32 | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment