mutex
is just an integer in memory. But how it works?
- atomic
- memory barrier
- system call
how lock works?
#define SPINLOCK(spinlock) if(1){ \
register VOLATILE long *addr = &((spinlock)->v); \
register int value = 0; \ # value = 1 lock, value = 0 unlock
\
__asm__ __volatile__( \
"xchgl %0, %1" \
:"=r" (value) \
:"m" (* addr), "0" (value): "memory" ); \
\
} else (void)0
1.https://mortoray.com/2019/02/20/how-does-a-mutex-work-what-does-it-cost/
4. https://preshing.com/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation/
5. https://eli.thegreenplace.net/2018/basics-of-futexes/