nuclear@86: #include nuclear@86: #include "mutex.h" nuclear@86: #include "sched.h" nuclear@86: #include "intr.h" nuclear@86: nuclear@86: void mutex_lock(mutex_t *m) nuclear@86: { nuclear@86: int istate = get_intr_state(); nuclear@86: disable_intr(); nuclear@86: nuclear@86: /* sleep while the mutex is held */ nuclear@86: while(*m > 0) { nuclear@86: wait(m); nuclear@86: } nuclear@86: /* then grab it... */ nuclear@86: (*m)++; nuclear@86: nuclear@86: set_intr_state(istate); nuclear@86: } nuclear@86: nuclear@86: void mutex_unlock(mutex_t *m) nuclear@86: { nuclear@86: int istate = get_intr_state(); nuclear@86: disable_intr(); nuclear@86: nuclear@86: assert(*m); nuclear@86: /* release the mutex and wakeup everyone waiting on it */ nuclear@86: (*m)--; nuclear@86: wakeup(m); nuclear@86: nuclear@86: set_intr_state(istate); nuclear@86: } nuclear@86: nuclear@86: int mutex_trylock(mutex_t *m) nuclear@86: { nuclear@86: int res = -1, istate = get_intr_state(); nuclear@86: disable_intr(); nuclear@86: nuclear@86: if(*m == 0) { nuclear@86: (*m)++; nuclear@86: res = 0; nuclear@86: } nuclear@86: set_intr_state(istate); nuclear@86: return res; nuclear@86: }