feat(ble_mesh): add adv lock to avoid allocated buffers

cherry-picked from 4905b6eae1
This commit is contained in:
luoxu
2025-09-11 16:16:29 +08:00
parent 255791b034
commit 191436cd0b

View File

@@ -2,7 +2,7 @@
/*
* SPDX-FileCopyrightText: 2017 Intel Corporation
* SPDX-FileContributor: 2018-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileContributor: 2018-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -56,6 +56,8 @@ struct bt_mesh_queue {
};
static struct bt_mesh_queue adv_queue;
static bt_mesh_mutex_t adv_buf_alloc_lock;
/* We reserve one queue item for bt_mesh_adv_update() */
#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
#define BLE_MESH_ADV_QUEUE_SIZE (CONFIG_BLE_MESH_ADV_BUF_COUNT + CONFIG_BLE_MESH_BLE_ADV_BUF_COUNT + 1)
@@ -352,8 +354,10 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool,
return NULL;
}
bt_mesh_r_mutex_lock(&adv_buf_alloc_lock);
buf = net_buf_alloc(pool, timeout);
if (!buf) {
bt_mesh_r_mutex_unlock(&adv_buf_alloc_lock);
return NULL;
}
@@ -368,6 +372,7 @@ struct net_buf *bt_mesh_adv_create_from_pool(struct net_buf_pool *pool,
adv->type = type;
adv->xmit = xmit;
bt_mesh_r_mutex_unlock(&adv_buf_alloc_lock);
return buf;
}
@@ -653,6 +658,7 @@ void bt_mesh_adv_init(void)
__ASSERT(ret == pdTRUE, "Failed to create adv thread");
(void)ret;
#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL && (CONFIG_SPIRAM_CACHE_WORKAROUND || !CONFIG_IDF_TARGET_ESP32) && CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY */
bt_mesh_r_mutex_create(&adv_buf_alloc_lock);
}
#if CONFIG_BLE_MESH_DEINIT
@@ -707,7 +713,8 @@ void bt_mesh_adv_deinit(void)
#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
bt_mesh_ble_adv_deinit();
#endif
#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */
bt_mesh_r_mutex_free(&adv_buf_alloc_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */