summaryrefslogtreecommitdiff
path: root/libddekit/lock.c
blob: 0f451dfd0292ba8a182c4cfe8da1bce9e6c8588a (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <l4/dde/ddekit/lock.h>
#include <l4/dde/ddekit/memory.h>

#include <l4/lock/lock.h>
#include <l4/util/macros.h>

#define DDEKIT_DEBUG_LOCKS 0

struct ddekit_lock {
	l4lock_t lock;
};

void _ddekit_lock_init(struct ddekit_lock **mtx) {
	*mtx = (struct ddekit_lock *) ddekit_simple_malloc(sizeof(struct ddekit_lock));
	(*mtx)->lock = L4LOCK_UNLOCKED;
}

void _ddekit_lock_deinit(struct ddekit_lock **mtx) {
	ddekit_simple_free(*mtx);
	*mtx = NULL;
}

void _ddekit_lock_lock(struct ddekit_lock **mtx) {
#if DDEKIT_DEBUG_LOCKS
	if (&(*mtx)->lock == 0x35ac)
		LOG("DOWN %p: "l4util_idfmt" <-> "l4util_idfmt, 
			&(*mtx)->lock,
			l4util_idstr(l4_myself()),
			l4util_idstr(l4thread_l4_id(l4lock_owner(&((*mtx)->lock)))));
#endif
	l4lock_lock(&(*mtx)->lock);
#if DDEKIT_DEBUG_LOCKS
	if (&(*mtx)->lock == 0x35ac)
		LOG("DOWN %p! "l4util_idfmt, &(*mtx)->lock, l4util_idstr(l4_myself()));
#endif
}

/* returns 0 on success, != 0 if it would block */
int _ddekit_lock_try_lock(struct ddekit_lock **mtx) {
	return l4lock_try_lock(&(*mtx)->lock) ? 0 : 1;
}

void _ddekit_lock_unlock(struct ddekit_lock **mtx) {
#if DDEKIT_DEBUG_LOCKS
	if (&(*mtx)->lock == 0x35ac)
		LOG("UP   %p: "l4util_idfmt" <-> "l4util_idfmt, 
			&(*mtx)->lock,
			l4util_idstr(l4_myself()),
			l4util_idstr(l4thread_l4_id(l4lock_owner(&((*mtx)->lock)))));
#endif
	l4lock_unlock(&(*mtx)->lock);
#if DDEKIT_DEBUG_LOCKS
	if (&(*mtx)->lock == 0x35ac)
		LOG("UP %p! "l4util_idfmt, &(*mtx)->lock, l4util_idstr(l4_myself()));
#endif
}


int _ddekit_lock_owner(struct ddekit_lock **mtx) {
	return (int)l4lock_owner(&(*mtx)->lock);
}