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 "FreeRtosMbedtlsMutex.h"
27 static inline void mutex_init(mbedtls_threading_mutex_t * p_mutex)
29 assert(p_mutex != NULL);
30 *p_mutex = xSemaphoreCreateMutex();
31 assert(*p_mutex != NULL);
34 static inline void mutex_free(mbedtls_threading_mutex_t * p_mutex)
36 assert(p_mutex != NULL);
37 assert(*p_mutex != NULL);
38 vSemaphoreDelete(*p_mutex);
41 static inline int mutex_lock(mbedtls_threading_mutex_t * p_mutex)
43 assert(p_mutex != NULL);
44 assert(*p_mutex != NULL);
45 return xSemaphoreTake(*p_mutex, portMAX_DELAY) != pdTRUE;
48 static inline int mutex_unlock(mbedtls_threading_mutex_t * p_mutex)
50 assert(p_mutex != NULL);
51 assert(*p_mutex != NULL);
52 return xSemaphoreGive(*p_mutex) != pdTRUE;
55 void freertos_mbedtls_mutex_init(void)
57 mbedtls_threading_set_alt(mutex_init, mutex_free, mutex_lock, mutex_unlock);
60 void freertos_mbedtls_mutex_free(void)
62 mbedtls_threading_free_alt();