Refactor macros

Signed-off-by: Ryan Everett <ryan.everett@arm.com>
This commit is contained in:
Ryan Everett 2024-01-24 13:26:26 +00:00
parent cb05ce30e9
commit 3877d4858b

View file

@ -114,88 +114,47 @@ typedef struct {
} key; } key;
} psa_key_slot_t; } psa_key_slot_t;
typedef enum { #if defined(MBEDTLS_THREADING_C)
PSA_MUTEX_LOCK = 0,
PSA_MUTEX_UNLOCK,
} psa_mutex_operation_t;
/** If threading is enabled: perform a lock or unlock operation on the /** Perform a mutex operation and return immediately upon failure.
* key slot mutex. *
* Call with parameter PSA_MUTEX_LOCK to perform a lock operation.
* Call with parameter PSA_MUTEX_UNLOCK to perform an unlock operation.
* Returns PSA_ERROR_SERVICE_FAILURE if the operation fails * Returns PSA_ERROR_SERVICE_FAILURE if the operation fails
* and status was PSA_SUCCESS. * and status was PSA_SUCCESS.
* If threading is not enabled, do nothing.
* *
* Assumptions: * Assumptions:
* psa_status_t status exists. * psa_status_t status exists.
* op is PSA_MUTEX_LOCK or PSA_MUTEX_UNLOCK. * f is a mutex operation which returns 0 upon success.
*/ */
#if defined(MBEDTLS_THREADING_C) #define PSA_THREADING_CHK_RET(f) \
#define PSA_KEY_SLOT_MUTEX_LOCKFUNC_RETURN(op) \
do \ do \
{ \ { \
if (op == PSA_MUTEX_LOCK) { \ if ((f) != 0) { \
if (mbedtls_mutex_lock( \
&mbedtls_threading_key_slot_mutex) != 0) { \
if (status == PSA_SUCCESS) { \ if (status == PSA_SUCCESS) { \
return PSA_ERROR_SERVICE_FAILURE; \ return PSA_ERROR_SERVICE_FAILURE; \
} \ } \
return status; \ return status; \
} \ } \
} \
else if (op == PSA_MUTEX_UNLOCK) { \
if (mbedtls_mutex_unlock( \
&mbedtls_threading_key_slot_mutex) != 0) { \
if (status == PSA_SUCCESS) { \
return PSA_ERROR_SERVICE_FAILURE; \
} \
return status; \
} \
} \
} while (0); } while (0);
#else
#define PSA_KEY_SLOT_MUTEX_LOCKFUNC_RETURN(op) do { } while (0)
#endif
/** If threading is enabled: perform a lock or unlock operation on the /** Perform a mutex operation and goto exit on failure.
* key slot mutex. *
* Call with parameter PSA_MUTEX_LOCK to perform a lock operation. * Sets status to PSA_ERROR_SERVICE_FAILURE if status was PSA_SUCCESS.
* Call with parameter PSA_MUTEX_UNLOCK to perform an unlock operation.
* This will goto the exit label if the operation fails,
* setting status to PSA_ERROR_SERVICE_FAILURE if status was PSA_SUCCESS.
* If threading is not enabled, do nothing.
* *
* Assumptions: * Assumptions:
* psa_status_t status exists. * psa_status_t status exists.
* Label exit: exists. * Label exit: exists.
* op is PSA_MUTEX_LOCK or PSA_MUTEX_UNLOCK. * f is a mutex operation which returns 0 upon success.
*/ */
#if defined(MBEDTLS_THREADING_C) #define PSA_THREADING_CHK_GOTO_EXIT(f) \
#define PSA_KEY_SLOT_MUTEX_LOCKFUNC_GOTO_EXIT(op) \
do \ do \
{ \ { \
if (op == PSA_MUTEX_LOCK) { \ if ((f) != 0) { \
if (mbedtls_mutex_lock( \
&mbedtls_threading_key_slot_mutex) != 0) { \
if (status == PSA_SUCCESS) { \ if (status == PSA_SUCCESS) { \
status = PSA_ERROR_SERVICE_FAILURE; \ status = PSA_ERROR_SERVICE_FAILURE; \
} \ } \
goto exit; \ goto exit; \
} \ } \
} \
else if (op == PSA_MUTEX_UNLOCK) { \
if (mbedtls_mutex_unlock( \
&mbedtls_threading_key_slot_mutex) != 0) { \
if (status == PSA_SUCCESS) { \
status = PSA_ERROR_SERVICE_FAILURE; \
} \
goto exit; \
} \
} \
} while (0); } while (0);
#else
#define PSA_KEY_SLOT_MUTEX_LOCKFUNC_GOTO_EXIT(op) do { } while (0)
#endif #endif
/* A mask of key attribute flags used only internally. /* A mask of key attribute flags used only internally.