Move handling of mutex->is_valid into threading_helpers.c

This is now a field only used for testing.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
This commit is contained in:
Paul Elliott 2023-11-10 14:05:09 +00:00
parent 9e80a91f27
commit 5fa986c8cb
3 changed files with 29 additions and 19 deletions

View file

@ -64,9 +64,9 @@ enum value_of_mutex_is_valid_field {
* compatibility with threading_mutex_init_pthread() and
* threading_mutex_free_pthread(). MUTEX_LOCKED could be any nonzero
* value. */
MUTEX_FREED = 0, //!< Set by threading_mutex_free_pthread
MUTEX_IDLE = 1, //!< Set by threading_mutex_init_pthread and by our unlock
MUTEX_LOCKED = 2, //!< Set by our lock
MUTEX_FREED = 0, //! < Set by mbedtls_test_wrap_mutex_free
MUTEX_IDLE = 1, //! < Set by mbedtls_test_wrap_mutex_init and by mbedtls_test_wrap_mutex_unlock
MUTEX_LOCKED = 2, //! < Set by mbedtls_test_wrap_mutex_lock
};
typedef struct {
@ -101,8 +101,12 @@ static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex,
static void mbedtls_test_wrap_mutex_init(mbedtls_threading_mutex_t *mutex)
{
mutex_functions.init(mutex);
if (mutex->is_valid) {
if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) {
mutex->state = MUTEX_IDLE;
++live_mutexes;
mutex_functions.unlock(&mbedtls_test_mutex_mutex);
}
}
@ -123,7 +127,11 @@ static void mbedtls_test_wrap_mutex_free(mbedtls_threading_mutex_t *mutex)
mbedtls_test_mutex_usage_error(mutex, "corrupted state");
break;
}
/* Mark mutex as free'd first, because we need to release the mutex. If
* free fails, this could end up with inconsistent state. */
if (mutex->is_valid) {
mutex->is_valid = MUTEX_FREED;
--live_mutexes;
}
mutex_functions.free(mutex);
@ -138,7 +146,7 @@ static int mbedtls_test_wrap_mutex_lock(mbedtls_threading_mutex_t *mutex)
break;
case MUTEX_IDLE:
if (ret == 0) {
mutex->is_valid = 2;
mutex->is_valid = MUTEX_LOCKED;
}
break;
case MUTEX_LOCKED: