sandbox: add locking primitives

This commit is contained in:
2023-02-02 21:00:40 +00:00
parent 7d3000e84d
commit 799a23014a
3 changed files with 26 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
#ifndef SOCKS_LOCKS_H_
#define SOCKS_LOCKS_H_
typedef int __attribute__((aligned(8))) spin_lock_t;
#define SPIN_LOCK_INIT ((spin_lock_t)0)
extern void spin_lock_irqsave(spin_lock_t *lck, unsigned long *flags);
extern void spin_unlock_irqrestore(spin_lock_t *lck, unsigned long flags);
#endif

14
sandbox/locks/spinlock.c Normal file
View File

@@ -0,0 +1,14 @@
#include <socks/locks.h>
void spin_lock_irqsave(spin_lock_t *lck, unsigned long *flags)
{
while (!__sync_bool_compare_and_swap(lck, 0, 1)) {
/* pause */
}
}
void spin_unlock_irqrestore(spin_lock_t *lck, unsigned long flags)
{
__sync_lock_release(lck);
}