Open ytgui opened 4 years ago
struct spinlock {
int locked;
};
void spin_lock(struct spinlock *lock)
{
// test_and_set: set flag to `1` and return old value
// if the func returns 0, flag 0->1, means get the lock
while (atomic_test_and_set(&lock->locked));
}
void spin_lock(struct spinlock *lock)
{
// `test_and_set` always write to cache line, which cause performance issue
// read only op before write, reduce cache line flush
while (!lock->locked && atomic_test_and_set(&lock->locked));
}
void spin_unlock(struct spinlock *lock)
{
lock->locked = 0;
}
array vs vector
false sharing
https://software.intel.com/en-us/articles/avoiding-and-identifying-false-sharing-among-threads
row major and slide window size
spinlock
https://zhuanlan.zhihu.com/p/133445693
MESI protocol