123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #define SPINLOCK_INLINE
- #include <types.h>
- #include <lib.h>
- #include <cpu.h>
- #include <spl.h>
- #include <spinlock.h>
- #include <current.h> /* for curcpu */
- void
- spinlock_init(struct spinlock *lk)
- {
- spinlock_data_set(&lk->lk_lock, 0);
- lk->lk_holder = NULL;
- }
- void
- spinlock_cleanup(struct spinlock *lk)
- {
- KASSERT(lk->lk_holder == NULL);
- KASSERT(spinlock_data_get(&lk->lk_lock) == 0);
- }
- void
- spinlock_acquire(struct spinlock *lk)
- {
- struct cpu *mycpu;
- splraise(IPL_NONE, IPL_HIGH);
-
- if (CURCPU_EXISTS()) {
- mycpu = curcpu->c_self;
- if (lk->lk_holder == mycpu) {
- panic("Deadlock on spinlock %p\n", lk);
- }
- }
- else {
- mycpu = NULL;
- }
- while (1) {
-
- if (spinlock_data_get(&lk->lk_lock) != 0) {
- continue;
- }
- if (spinlock_data_testandset(&lk->lk_lock) != 0) {
- continue;
- }
- break;
- }
- lk->lk_holder = mycpu;
- }
- void
- spinlock_release(struct spinlock *lk)
- {
-
- if (CURCPU_EXISTS()) {
- KASSERT(lk->lk_holder == curcpu->c_self);
- }
- lk->lk_holder = NULL;
- spinlock_data_set(&lk->lk_lock, 0);
- spllower(IPL_HIGH, IPL_NONE);
- }
-
- bool
- spinlock_do_i_hold(struct spinlock *lk)
- {
- if (!CURCPU_EXISTS()) {
- return true;
- }
-
- return (lk->lk_holder == curcpu->c_self);
- }
|