forked from dayne/zunkfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.c
39 lines (33 loc) · 694 Bytes
/
mutex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "mutex.h"
#include "utils.h"
/*
* Paranoid locking ops.
*/
void init_mutex(struct mutex *m)
{
*m = (struct mutex)INIT_MUTEX;
}
void lock(struct mutex *m)
{
int err = pthread_mutex_lock(&m->mutex);
if (err)
panic("pthread_mutex_lock: %s\n", strerror(err));
m->owner = pthread_self();
}
void unlock(struct mutex *m)
{
int err;
m->owner = (pthread_t)-1;
err = pthread_mutex_unlock(&m->mutex);
if (err)
panic("pthread_mutex_lock: %s\n", strerror(err));
}
int trylock(struct mutex *m)
{
int err = pthread_mutex_trylock(&m->mutex);
if (err && err != EBUSY)
panic("pthread_mutex_lock: %s\n", strerror(err));
if (!err)
m->owner = pthread_self();
return !err;
}