4747 * function into both @ref mutex_lock and @ref mutex_lock_cancelable is,
4848 * therefore, beneficial for the majority of applications.
4949 */
50- static inline __attribute__((always_inline )) void _block (mutex_t * mutex ,
51- unsigned irq_state )
50+ static inline __attribute__((always_inline ))
51+ void _block (mutex_t * mutex ,
52+ unsigned irq_state ,
53+ uinttxtptr_t pc )
5254{
55+ /* pc is only used when MODULE_CORE_MUTEX_DEBUG */
56+ (void )pc ;
57+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
58+ printf ("[mutex] waiting for thread %" PRIkernel_pid " (pc = 0x%" PRIxTXTPTR
59+ ")\n" ,
60+ mutex -> owner , mutex -> owner_calling_pc );
61+ #endif
5362 thread_t * me = thread_get_active ();
5463
5564 /* Fail visibly even if a blocking action is called from somewhere where
@@ -80,10 +89,17 @@ static inline __attribute__((always_inline)) void _block(mutex_t *mutex,
8089 irq_restore (irq_state );
8190 thread_yield_higher ();
8291 /* We were woken up by scheduler. Waker removed us from queue. */
92+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
93+ mutex -> owner_calling_pc = pc ;
94+ #endif
8395}
8496
8597bool mutex_lock_internal (mutex_t * mutex , bool block )
8698{
99+ uinttxtptr_t pc = 0 ;
100+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
101+ pc = cpu_get_caller_pc ();
102+ #endif
87103 unsigned irq_state = irq_disable ();
88104
89105 DEBUG ("PID[%" PRIkernel_pid "] mutex_lock_internal(block=%u).\n" ,
@@ -92,9 +108,15 @@ bool mutex_lock_internal(mutex_t *mutex, bool block)
92108 if (mutex -> queue .next == NULL ) {
93109 /* mutex is unlocked. */
94110 mutex -> queue .next = MUTEX_LOCKED ;
95- #ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
111+ #if IS_USED (MODULE_CORE_MUTEX_PRIORITY_INHERITANCE ) \
112+ || IS_USED (MODULE_CORE_MUTEX_DEBUG )
96113 thread_t * me = thread_get_active ();
97114 mutex -> owner = me -> pid ;
115+ #endif
116+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
117+ mutex -> owner_calling_pc = pc ;
118+ #endif
119+ #if IS_USED (MODULE_CORE_MUTEX_PRIORITY_INHERITANCE )
98120 mutex -> owner_original_priority = me -> priority ;
99121#endif
100122 DEBUG ("PID[%" PRIkernel_pid "] mutex_lock(): early out.\n" ,
@@ -106,14 +128,18 @@ bool mutex_lock_internal(mutex_t *mutex, bool block)
106128 irq_restore (irq_state );
107129 return false;
108130 }
109- _block (mutex , irq_state );
131+ _block (mutex , irq_state , pc );
110132 }
111133
112134 return true;
113135}
114136
115137int mutex_lock_cancelable (mutex_cancel_t * mc )
116138{
139+ uinttxtptr_t pc = 0 ;
140+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
141+ pc = cpu_get_caller_pc ();
142+ #endif
117143 unsigned irq_state = irq_disable ();
118144
119145 DEBUG ("PID[%" PRIkernel_pid "] mutex_lock_cancelable()\n" ,
@@ -131,9 +157,15 @@ int mutex_lock_cancelable(mutex_cancel_t *mc)
131157 if (mutex -> queue .next == NULL ) {
132158 /* mutex is unlocked. */
133159 mutex -> queue .next = MUTEX_LOCKED ;
134- #ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
160+ #if IS_USED (MODULE_CORE_MUTEX_PRIORITY_INHERITANCE ) \
161+ || IS_USED (MODULE_CORE_MUTEX_DEBUG )
135162 thread_t * me = thread_get_active ();
136163 mutex -> owner = me -> pid ;
164+ #endif
165+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
166+ mutex -> owner_calling_pc = pc ;
167+ #endif
168+ #if IS_USED (MODULE_CORE_MUTEX_PRIORITY_INHERITANCE )
137169 mutex -> owner_original_priority = me -> priority ;
138170#endif
139171 DEBUG ("PID[%" PRIkernel_pid "] mutex_lock_cancelable() early out.\n" ,
@@ -142,7 +174,7 @@ int mutex_lock_cancelable(mutex_cancel_t *mc)
142174 return 0 ;
143175 }
144176 else {
145- _block (mutex , irq_state );
177+ _block (mutex , irq_state , pc );
146178 if (mc -> cancelled ) {
147179 DEBUG ("PID[%" PRIkernel_pid "] mutex_lock_cancelable() "
148180 "cancelled.\n" , thread_getpid ());
@@ -185,7 +217,7 @@ void mutex_unlock(mutex_t *mutex)
185217
186218 uint16_t process_priority = process -> priority ;
187219
188- #ifdef MODULE_CORE_MUTEX_PRIORITY_INHERITANCE
220+ #if IS_USED ( MODULE_CORE_MUTEX_PRIORITY_INHERITANCE )
189221 thread_t * owner = thread_get (mutex -> owner );
190222 if ((owner ) && (owner -> priority != mutex -> owner_original_priority )) {
191223 DEBUG ("PID[%" PRIkernel_pid "] prio %u --> %u\n" ,
@@ -194,6 +226,9 @@ void mutex_unlock(mutex_t *mutex)
194226 sched_change_priority (owner , mutex -> owner_original_priority );
195227 }
196228#endif
229+ #if IS_USED (MODULE_CORE_MUTEX_DEBUG )
230+ mutex -> owner_calling_pc = 0 ;
231+ #endif
197232
198233 irq_restore (irqstate );
199234 sched_switch (process_priority );
0 commit comments