kern

diff src/mutex.c @ 86:379332fc1667

implementing ata read/write
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 07 Dec 2011 14:03:11 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/mutex.c	Wed Dec 07 14:03:11 2011 +0200
     1.3 @@ -0,0 +1,45 @@
     1.4 +#include <assert.h>
     1.5 +#include "mutex.h"
     1.6 +#include "sched.h"
     1.7 +#include "intr.h"
     1.8 +
     1.9 +void mutex_lock(mutex_t *m)
    1.10 +{
    1.11 +	int istate = get_intr_state();
    1.12 +	disable_intr();
    1.13 +
    1.14 +	/* sleep while the mutex is held */
    1.15 +	while(*m > 0) {
    1.16 +		wait(m);
    1.17 +	}
    1.18 +	/* then grab it... */
    1.19 +	(*m)++;
    1.20 +
    1.21 +	set_intr_state(istate);
    1.22 +}
    1.23 +
    1.24 +void mutex_unlock(mutex_t *m)
    1.25 +{
    1.26 +	int istate = get_intr_state();
    1.27 +	disable_intr();
    1.28 +
    1.29 +	assert(*m);
    1.30 +	/* release the mutex and wakeup everyone waiting on it */
    1.31 +	(*m)--;
    1.32 +	wakeup(m);
    1.33 +
    1.34 +	set_intr_state(istate);
    1.35 +}
    1.36 +
    1.37 +int mutex_trylock(mutex_t *m)
    1.38 +{
    1.39 +	int res = -1, istate = get_intr_state();
    1.40 +	disable_intr();
    1.41 +
    1.42 +	if(*m == 0) {
    1.43 +		(*m)++;
    1.44 +		res = 0;
    1.45 +	}
    1.46 +	set_intr_state(istate);
    1.47 +	return res;
    1.48 +}