2021-10-26 04:24:54 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-03-30 06:24:59 -04:00
|
|
|
|
2023-08-25 02:28:44 -04:00
|
|
|
#include "mesh/common.h"
|
2020-03-30 06:24:59 -04:00
|
|
|
|
|
|
|
static bt_mesh_mutex_t alarm_lock;
|
|
|
|
static bt_mesh_mutex_t list_lock;
|
|
|
|
static bt_mesh_mutex_t buf_lock;
|
|
|
|
static bt_mesh_mutex_t atomic_lock;
|
|
|
|
|
|
|
|
void bt_mesh_mutex_create(bt_mesh_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (!mutex) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Create, invalid mutex");
|
2020-03-30 06:24:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-03 04:33:36 -04:00
|
|
|
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
|
|
|
|
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
|
|
|
|
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
|
|
|
|
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
|
|
|
|
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
|
|
|
|
#endif
|
|
|
|
__ASSERT(mutex->buffer, "Failed to create mutex buffer");
|
2020-03-30 06:24:59 -04:00
|
|
|
mutex->mutex = xSemaphoreCreateMutexStatic(mutex->buffer);
|
2020-07-03 04:33:36 -04:00
|
|
|
__ASSERT(mutex->mutex, "Failed to create static mutex");
|
|
|
|
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
|
2020-03-30 06:24:59 -04:00
|
|
|
mutex->mutex = xSemaphoreCreateMutex();
|
2020-07-03 04:33:36 -04:00
|
|
|
__ASSERT(mutex->mutex, "Failed to create mutex");
|
|
|
|
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (!mutex) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Free, invalid mutex");
|
2020-03-30 06:24:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutex->mutex) {
|
|
|
|
vSemaphoreDelete(mutex->mutex);
|
|
|
|
mutex->mutex = NULL;
|
2020-07-03 04:33:36 -04:00
|
|
|
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
|
2020-03-30 06:24:59 -04:00
|
|
|
heap_caps_free(mutex->buffer);
|
|
|
|
mutex->buffer = NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (!mutex) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Lock, invalid mutex");
|
2020-03-30 06:24:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutex->mutex) {
|
|
|
|
xSemaphoreTake(mutex->mutex, portMAX_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex)
|
|
|
|
{
|
|
|
|
if (!mutex) {
|
2020-07-04 06:35:08 -04:00
|
|
|
BT_ERR("Unlock, invalid mutex");
|
2020-03-30 06:24:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutex->mutex) {
|
|
|
|
xSemaphoreGive(mutex->mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex)
|
2020-03-30 06:24:59 -04:00
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
if (!mutex) {
|
|
|
|
BT_ERR("Create, invalid recursive mutex");
|
|
|
|
return;
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
2023-08-28 04:09:37 -04:00
|
|
|
|
|
|
|
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC
|
|
|
|
#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL
|
|
|
|
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
|
|
|
|
#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT
|
|
|
|
mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
|
|
|
|
#endif
|
|
|
|
__ASSERT(mutex->buffer, "Failed to create recursive mutex buffer");
|
|
|
|
mutex->mutex = xSemaphoreCreateRecursiveMutexStatic(mutex->buffer);
|
|
|
|
__ASSERT(mutex->mutex, "Failed to create static recursive mutex");
|
|
|
|
#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
|
|
|
|
mutex->mutex = xSemaphoreCreateRecursiveMutex();
|
|
|
|
__ASSERT(mutex->mutex, "Failed to create recursive mutex");
|
|
|
|
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex)
|
2020-03-30 06:24:59 -04:00
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
bt_mesh_mutex_free(mutex);
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex)
|
2020-03-30 06:24:59 -04:00
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
if (!mutex) {
|
|
|
|
BT_ERR("Lock, invalid recursive mutex");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutex->mutex) {
|
|
|
|
xSemaphoreTakeRecursive(mutex->mutex, portMAX_DELAY);
|
|
|
|
}
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex)
|
2020-03-30 06:24:59 -04:00
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
if (!mutex) {
|
|
|
|
BT_ERR("Unlock, invalid recursive mutex");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mutex->mutex) {
|
|
|
|
xSemaphoreGiveRecursive(mutex->mutex);
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 04:09:37 -04:00
|
|
|
void bt_mesh_alarm_lock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_lock(&alarm_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_alarm_unlock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_unlock(&alarm_lock);
|
|
|
|
}
|
|
|
|
|
2020-03-30 06:24:59 -04:00
|
|
|
void bt_mesh_list_lock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_lock(&list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_list_unlock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_unlock(&list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_buf_lock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_lock(&buf_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_buf_unlock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_unlock(&buf_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_atomic_lock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_lock(&atomic_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_atomic_unlock(void)
|
|
|
|
{
|
|
|
|
bt_mesh_mutex_unlock(&atomic_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_mesh_mutex_init(void)
|
|
|
|
{
|
2023-08-28 04:09:37 -04:00
|
|
|
bt_mesh_mutex_create(&alarm_lock);
|
|
|
|
bt_mesh_mutex_create(&list_lock);
|
|
|
|
bt_mesh_mutex_create(&buf_lock);
|
|
|
|
bt_mesh_mutex_create(&atomic_lock);
|
2020-03-30 06:24:59 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 22:19:31 -04:00
|
|
|
#if CONFIG_BLE_MESH_DEINIT
|
2023-08-28 04:09:37 -04:00
|
|
|
void bt_mesh_mutex_deinit(void)
|
2020-10-15 22:19:31 -04:00
|
|
|
{
|
|
|
|
bt_mesh_mutex_free(&alarm_lock);
|
|
|
|
bt_mesh_mutex_free(&list_lock);
|
|
|
|
bt_mesh_mutex_free(&buf_lock);
|
|
|
|
bt_mesh_mutex_free(&atomic_lock);
|
|
|
|
}
|
2020-10-15 23:50:39 -04:00
|
|
|
#endif /* CONFIG_BLE_MESH_DEINIT */
|