3 #include <ddekit/assert.h>
4 #include <ddekit/memory.h>
5 #include <ddekit/semaphore.h>
7 #ifdef DDEBUG_LEVEL_LOCK
9 #define DDEBUG DDEBUG_LEVEL_LOCK
16 ddekit_thread_t
*owner
;
17 ddekit_thread_t
*wait_queue
;
21 /******************************************************************************
22 * ddekit_lock_init_locked *
23 *****************************************************************************/
24 void ddekit_lock_init_locked(ddekit_lock_t
*mtx
)
26 (*mtx
) = (struct ddekit_lock
*)
27 ddekit_simple_malloc(sizeof(struct ddekit_lock
));
29 (*mtx
)->wait_queue
= NULL
;
30 (*mtx
)->owner
= ddekit_thread_myself();
33 /******************************************************************************
34 * ddekit_lock_init_unlocked *
35 *****************************************************************************/
36 void ddekit_lock_init_unlocked(ddekit_lock_t
*mtx
)
38 (*mtx
) = (struct ddekit_lock
*)
39 ddekit_simple_malloc(sizeof(struct ddekit_lock
));
41 (*mtx
)->wait_queue
= NULL
;
44 /******************************************************************************
45 * ddekit_lock_deinit *
46 *****************************************************************************/
47 void ddekit_lock_deinit (ddekit_lock_t
*mtx
)
49 ddekit_simple_free(*mtx
);
52 /******************************************************************************
54 *****************************************************************************/
55 void ddekit_lock_lock (ddekit_lock_t
*mtx
)
57 if ((*mtx
)->owner
== NULL
) {
58 (*mtx
)->owner
= ddekit_thread_myself();
61 if ((*mtx
)->wait_queue
== NULL
) {
62 (*mtx
)->wait_queue
= ddekit_thread_myself();
64 ddekit_thread_t
*pos
= (*mtx
)->wait_queue
;
65 while(pos
->next
!= NULL
) {
68 pos
->next
= ddekit_thread_myself();
71 _ddekit_thread_schedule();
73 if ((*mtx
)->owner
!= NULL
) {
74 _ddekit_print_backtrace((*mtx
)->owner
);
75 _ddekit_print_backtrace(ddekit_thread_myself());
76 ddekit_panic("owner!=NULL: %s (I am %s)\n",
77 (*mtx
)->owner
->name
, ddekit_thread_myself()->name
);
80 (*mtx
)->owner
= ddekit_thread_myself();
84 /******************************************************************************
85 * ddekit_lock_try_lock *
86 *****************************************************************************/
87 int ddekit_lock_try_lock(ddekit_lock_t
*mtx
)
89 if ((*mtx
)->owner
== NULL
) {
90 (*mtx
)->owner
= ddekit_thread_myself();
97 /******************************************************************************
98 * ddekit_lock_unlock *
99 *****************************************************************************/
100 void ddekit_lock_unlock (ddekit_lock_t
*mtx
) {
101 ddekit_assert((*mtx
)->owner
!= NULL
);
102 (*mtx
)->owner
= NULL
;
103 if((*mtx
)->wait_queue
) {
104 ddekit_thread_t
*waiter
= (*mtx
)->wait_queue
;
105 (*mtx
)->wait_queue
= waiter
->next
;
107 _ddekit_thread_enqueue(waiter
);
112 /******************************************************************************
113 * ddekit_lock_owner *
114 *****************************************************************************/
115 int ddekit_lock_owner(ddekit_lock_t
*mtx
) {
116 return ddekit_thread_get_id((*mtx
)->owner
);