]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dde/ddekit/src/lock.c
Update
[l4.git] / l4 / pkg / dde / ddekit / src / lock.c
1 /*
2  * This file is part of DDEKit.
3  *
4  * (c) 2006-2012 Bjoern Doebel <doebel@os.inf.tu-dresden.de>
5  *               Christian Helmuth <ch12@os.inf.tu-dresden.de>
6  *               Thomas Friebel <tf13@os.inf.tu-dresden.de>
7  *     economic rights: Technische Universitaet Dresden (Germany)
8  *
9  * This file is part of TUD:OS and distributed under the terms of the
10  * GNU General Public License 2.
11  * Please see the COPYING-GPL-2 file for details.
12  */
13
14 #include <l4/dde/ddekit/lock.h>
15 #include <l4/dde/ddekit/memory.h>
16 #include <l4/dde/ddekit/panic.h>
17 #include <l4/dde/ddekit/assert.h>
18
19 #include <pthread-l4.h> // pthread_l4_cap
20 #include <bits/pthreadtypes.h> // mutex->__m_owner
21
22 #include "internals.h"
23
24 #define DDEKIT_DEBUG_LOCKS 1
25
26 void ddekit_lock_init    (ddekit_lock_t *mtx)
27 {
28         *mtx = ddekit_simple_malloc(sizeof(struct ddekit_lock));
29         Assert(pthread_mutex_init(&(*mtx)->mtx, NULL) == 0);
30 }
31
32
33 void ddekit_lock_deinit  (ddekit_lock_t *mtx)
34 {
35         pthread_mutex_destroy(&(*mtx)->mtx);
36         ddekit_simple_free(*mtx);
37 }
38
39
40 void ddekit_lock_lock    (ddekit_lock_t *mtx)
41 {
42         int ret = pthread_mutex_lock(&(*mtx)->mtx);
43         Assert(ret == 0);
44 }
45
46
47 int  ddekit_lock_try_lock(ddekit_lock_t *mtx)
48 {
49         return pthread_mutex_trylock(&(*mtx)->mtx);
50 }
51
52
53 void ddekit_lock_unlock  (ddekit_lock_t *mtx)
54 {
55         int ret = pthread_mutex_unlock(&(*mtx)->mtx);
56         Assert(ret == 0);
57 }
58
59
60 int ddekit_lock_owner(ddekit_lock_t *mtx)
61 {
62         return (int)((*mtx)->mtx.__m_owner);
63 }
64
65
66 int ddekit_lock_is_locked(ddekit_lock_t *mtx)
67 {
68   return (*mtx)->mtx.__m_lock.__status;
69 }