3 * Copyright (c) 2020 Google LLC.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "FreeRtosMbedtlsUtils.h"
30 static inline void mutex_init(mbedtls_threading_mutex_t * p_mutex)
32 assert(p_mutex != NULL);
33 *p_mutex = xSemaphoreCreateMutex();
34 assert(*p_mutex != NULL);
37 static inline void mutex_free(mbedtls_threading_mutex_t * p_mutex)
39 assert(p_mutex != NULL);
40 assert(*p_mutex != NULL);
41 vSemaphoreDelete(*p_mutex);
44 static inline int mutex_lock(mbedtls_threading_mutex_t * p_mutex)
46 assert(p_mutex != NULL);
47 assert(*p_mutex != NULL);
48 return xSemaphoreTake(*p_mutex, portMAX_DELAY) != pdTRUE;
51 static inline int mutex_unlock(mbedtls_threading_mutex_t * p_mutex)
53 assert(p_mutex != NULL);
54 assert(*p_mutex != NULL);
55 return xSemaphoreGive(*p_mutex) != pdTRUE;
58 void freertos_mbedtls_mutex_init(void)
60 mbedtls_threading_set_alt(mutex_init, mutex_free, mutex_lock, mutex_unlock);
63 void freertos_mbedtls_mutex_free(void)
65 mbedtls_threading_free_alt();