From 2189b0ec2a92fc683e40f48f54a7cbb0f161b4c4 Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Fri, 23 Oct 2020 16:34:38 -0400 Subject: [PATCH] Fix #623, add debug messages for mutex double locks if OS_DEBUG is enabled, this adds a message if mutex give/take actions occur outside the expected sequence. In particular, this warns if a task takes a mutex more than once. --- src/os/shared/inc/os-shared-mutex.h | 3 ++- src/os/shared/src/osapi-mutex.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/os/shared/inc/os-shared-mutex.h b/src/os/shared/inc/os-shared-mutex.h index 3f8af42b0..218c60343 100644 --- a/src/os/shared/inc/os-shared-mutex.h +++ b/src/os/shared/inc/os-shared-mutex.h @@ -32,7 +32,8 @@ typedef struct { - char obj_name[OS_MAX_API_NAME]; + char obj_name[OS_MAX_API_NAME]; + osal_id_t last_owner; } OS_mutex_internal_record_t; /* diff --git a/src/os/shared/src/osapi-mutex.c b/src/os/shared/src/osapi-mutex.c index 53a52a377..28cbc73ee 100644 --- a/src/os/shared/src/osapi-mutex.c +++ b/src/os/shared/src/osapi-mutex.c @@ -162,11 +162,24 @@ int32 OS_MutSemGive(osal_id_t sem_id) OS_common_record_t *record; uint32 local_id; int32 return_code; + osal_id_t self_task; /* Check Parameters */ return_code = OS_ObjectIdGetById(OS_LOCK_MODE_NONE, LOCAL_OBJID_TYPE, sem_id, &local_id, &record); if (return_code == OS_SUCCESS) { + self_task = OS_TaskGetId(); + + if (!OS_ObjectIdEqual(OS_mutex_table[local_id].last_owner, self_task)) + { + OS_DEBUG("WARNING: Task %lu giving mutex %lu while owned by task %lu\n", + OS_ObjectIdToInteger(self_task), + OS_ObjectIdToInteger(sem_id), + OS_ObjectIdToInteger(OS_mutex_table[local_id].last_owner)); + } + + OS_mutex_table[local_id].last_owner = OS_OBJECT_ID_UNDEFINED; + return_code = OS_MutSemGive_Impl(local_id); } @@ -187,12 +200,27 @@ int32 OS_MutSemTake(osal_id_t sem_id) OS_common_record_t *record; uint32 local_id; int32 return_code; + osal_id_t self_task; /* Check Parameters */ return_code = OS_ObjectIdGetById(OS_LOCK_MODE_NONE, LOCAL_OBJID_TYPE, sem_id, &local_id, &record); if (return_code == OS_SUCCESS) { return_code = OS_MutSemTake_Impl(local_id); + if (return_code == OS_SUCCESS) + { + self_task = OS_TaskGetId(); + + if (OS_ObjectIdDefined(OS_mutex_table[local_id].last_owner)) + { + OS_DEBUG("WARNING: Task %lu taking mutex %lu while owned by task %lu\n", + OS_ObjectIdToInteger(self_task), + OS_ObjectIdToInteger(sem_id), + OS_ObjectIdToInteger(OS_mutex_table[local_id].last_owner)); + } + + OS_mutex_table[local_id].last_owner = self_task; + } } return return_code;