Skip to content

Commit 4914535

Browse files
authored
Merge pull request #2929 from ARMmbed/update_trace
Update trace
2 parents 94e1cbc + 35c4ceb commit 4914535

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

features/FEATURE_COMMON_PAL/mbed-trace/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)