kern
view src/mutex.c @ 98:921a264297a4
merged the filesystem stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Apr 2014 17:03:30 +0300 |
parents | |
children |
line source
1 #include <assert.h>
2 #include "mutex.h"
3 #include "sched.h"
4 #include "intr.h"
6 void mutex_lock(mutex_t *m)
7 {
8 int istate = get_intr_state();
9 disable_intr();
11 /* sleep while the mutex is held */
12 while(*m > 0) {
13 wait(m);
14 }
15 /* then grab it... */
16 (*m)++;
18 set_intr_state(istate);
19 }
21 void mutex_unlock(mutex_t *m)
22 {
23 int istate = get_intr_state();
24 disable_intr();
26 assert(*m);
27 /* release the mutex and wakeup everyone waiting on it */
28 (*m)--;
29 wakeup(m);
31 set_intr_state(istate);
32 }
34 int mutex_trylock(mutex_t *m)
35 {
36 int res = -1, istate = get_intr_state();
37 disable_intr();
39 if(*m == 0) {
40 (*m)++;
41 res = 0;
42 }
43 set_intr_state(istate);
44 return res;
45 }