Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
kernel: Add spin lock
  • Loading branch information
penberg committed Sep 18, 2021
1 parent 3cfbcab commit bf5d09a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/kernel/spinlock.h
@@ -0,0 +1,22 @@
#ifndef __MANTICORE_KERNEL_SPINLOCK_H
#define __MANTICORE_KERNEL_SPINLOCK_H

typedef unsigned long spinlock_t;

#define SPIN_LOCK_UNLOCKED 0
#define SPIN_LOCK_LOCKED 1

#define DEFINE_SPIN_LOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED;

static inline void spin_lock(spinlock_t *lock)
{
while (!__sync_bool_compare_and_swap(lock, SPIN_LOCK_UNLOCKED, SPIN_LOCK_LOCKED))
;;
}

static inline void spin_unlock(spinlock_t *lock)
{
__sync_bool_compare_and_swap(lock, SPIN_LOCK_LOCKED, SPIN_LOCK_UNLOCKED);
}

#endif

0 comments on commit bf5d09a

Please sign in to comment.