kern

annotate src/mutex.c @ 90:7ff2b4971216

started work on the filesystem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 13:44:15 +0200
parents
children
rev   line source
nuclear@86 1 #include <assert.h>
nuclear@86 2 #include "mutex.h"
nuclear@86 3 #include "sched.h"
nuclear@86 4 #include "intr.h"
nuclear@86 5
nuclear@86 6 void mutex_lock(mutex_t *m)
nuclear@86 7 {
nuclear@86 8 int istate = get_intr_state();
nuclear@86 9 disable_intr();
nuclear@86 10
nuclear@86 11 /* sleep while the mutex is held */
nuclear@86 12 while(*m > 0) {
nuclear@86 13 wait(m);
nuclear@86 14 }
nuclear@86 15 /* then grab it... */
nuclear@86 16 (*m)++;
nuclear@86 17
nuclear@86 18 set_intr_state(istate);
nuclear@86 19 }
nuclear@86 20
nuclear@86 21 void mutex_unlock(mutex_t *m)
nuclear@86 22 {
nuclear@86 23 int istate = get_intr_state();
nuclear@86 24 disable_intr();
nuclear@86 25
nuclear@86 26 assert(*m);
nuclear@86 27 /* release the mutex and wakeup everyone waiting on it */
nuclear@86 28 (*m)--;
nuclear@86 29 wakeup(m);
nuclear@86 30
nuclear@86 31 set_intr_state(istate);
nuclear@86 32 }
nuclear@86 33
nuclear@86 34 int mutex_trylock(mutex_t *m)
nuclear@86 35 {
nuclear@86 36 int res = -1, istate = get_intr_state();
nuclear@86 37 disable_intr();
nuclear@86 38
nuclear@86 39 if(*m == 0) {
nuclear@86 40 (*m)++;
nuclear@86 41 res = 0;
nuclear@86 42 }
nuclear@86 43 set_intr_state(istate);
nuclear@86 44 return res;
nuclear@86 45 }