Lock test mutex before doing mutex usage check

Although this again should only happen post all threads stopping, guard
this just in case things change.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
diff --git a/tests/src/threading_helpers.c b/tests/src/threading_helpers.c
index ff0c712..5eac029 100644
--- a/tests/src/threading_helpers.c
+++ b/tests/src/threading_helpers.c
@@ -317,22 +317,26 @@
 
 void mbedtls_test_mutex_usage_check(void)
 {
-    if (live_mutexes != 0) {
-        /* A positive number (more init than free) means that a mutex resource
-         * is leaking (on platforms where a mutex consumes more than the
-         * mbedtls_threading_mutex_t object itself). The rare case of a
-         * negative number means a missing init somewhere. */
-        mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes);
-        live_mutexes = 0;
-        mbedtls_test_set_mutex_usage_error("missing free");
+    if (mutex_functions.lock(&mbedtls_test_mutex_mutex) == 0) {
+        if (live_mutexes != 0) {
+            /* A positive number (more init than free) means that a mutex resource
+             * is leaking (on platforms where a mutex consumes more than the
+             * mbedtls_threading_mutex_t object itself). The rare case of a
+             * negative number means a missing init somewhere. */
+            mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes);
+            live_mutexes = 0;
+            mbedtls_test_set_mutex_usage_error("missing free");
+        }
+        if (mbedtls_test_get_mutex_usage_error() != NULL &&
+            mbedtls_test_get_result() != MBEDTLS_TEST_RESULT_FAILED) {
+            /* Functionally, the test passed. But there was a mutex usage error,
+             * so mark the test as failed after all. */
+            mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__);
+        }
+        mbedtls_test_set_mutex_usage_error(NULL);
+
+        mutex_functions.unlock(&mbedtls_test_mutex_mutex);
     }
-    if (mbedtls_test_get_mutex_usage_error() != NULL &&
-        mbedtls_test_get_result() != MBEDTLS_TEST_RESULT_FAILED) {
-        /* Functionally, the test passed. But there was a mutex usage error,
-         * so mark the test as failed after all. */
-        mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__);
-    }
-    mbedtls_test_set_mutex_usage_error(NULL);
 }
 
 void mbedtls_test_mutex_usage_end(void)