Skip to content

Commit 7a4b3d1

Browse files
author
Seppo Takalo
committed
Squashed 'features/FEATURE_COMMON_PAL/mbed-trace/' changes from b17e969..31e338c
31e338c Use temp variable in mutex release loop (#52) git-subtree-dir: features/FEATURE_COMMON_PAL/mbed-trace git-subtree-split: 31e338c23934491fcb852ee4d2788d34851d01a2
1 parent 5e1e1d8 commit 7a4b3d1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

source/mbed_trace.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,20 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
454454

455455
end:
456456
if ( m_trace.mutex_release_f ) {
457-
while (m_trace.mutex_lock_count > 0) {
458-
m_trace.mutex_lock_count--;
457+
// Store the mutex lock count to temp variable so that it won't get
458+
// clobbered during last loop iteration when mutex gets released
459+
int count = m_trace.mutex_lock_count;
460+
m_trace.mutex_lock_count = 0;
461+
// Since the helper functions (eg. mbed_trace_array) are used like this:
462+
// mbed_tracef(TRACE_LEVEL_INFO, "grp", "%s", mbed_trace_array(some_array))
463+
// The helper function MUST acquire the mutex if it modifies any buffers. However
464+
// it CANNOT unlock the mutex because that would allow another thread to acquire
465+
// the mutex after helper function unlocks it and before mbed_tracef acquires it
466+
// for itself. This means that here we have to unlock the mutex as many times
467+
// as it was acquired by trace function and any possible helper functions.
468+
do {
459469
m_trace.mutex_release_f();
460-
}
470+
} while (--count > 0);
461471
}
462472
}
463473
static void mbed_trace_reset_tmp(void)

0 commit comments

Comments
 (0)