threading_alt.c 源码
2026/7/20大约 1 分钟附录源码附录MBED TLS
threading_alt.c — MBED TLS 线程互斥适配层
路径: components/security/mbedtls/port/src/threading_alt.c
功能: 当 MBEDTLS_THREADING_ALT 宏启用时,将 MBED TLS 的线程互斥锁接口映射到 OneOS 的互斥锁 API。这是 MBED TLS 在 OneOS 多线程环境下安全运行的关键适配层。
引用章节: 7.3.1 MBED TLS 加密组件
完整源码
/**
***********************************************************************************************************************
* Copyright (c) 2020, China Mobile Communications Group Co.,Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file thread_alt.c
*
* @brief Implementation of the functions when MBEDTLS_THREADING_ALT defined for mbedtls.
*
* @details
*
* @revision
* Date Author Notes
* 2020-08-25 OneOs Team First Version
***********************************************************************************************************************
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#ifdef MBEDTLS_THREADING_ALT
#include "threading_alt.h"
#include "oneos_config.h"
#include "os_util.h"
void mbedtls_mutex_alt_init( mbedtls_threading_mutex_t *mutex )
{
static uint16_t tls_alt_mutex_cnt = 0;
char name[OS_NAME_MAX] = {0};
os_snprintf(name, sizeof(name), "tlsMu%02d", tls_alt_mutex_cnt++);
mutex->mutex = os_mutex_create(OS_NULL, name, OS_FALSE);
mutex->valid = mutex->mutex? OS_TRUE : OS_FALSE;
}
void mbedtls_mutex_alt_free( mbedtls_threading_mutex_t *mutex )
{
os_mutex_destroy(mutex->mutex);
mutex->valid = 0;
}
int mbedtls_mutex_alt_lock( mbedtls_threading_mutex_t *mutex )
{
return os_mutex_lock(mutex->mutex, OS_WAIT_FOREVER);
}
int mbedtls_mutex_alt_unlock( mbedtls_threading_mutex_t *mutex )
{
return os_mutex_unlock(mutex->mutex);
}
#endif关键说明
| 模块 | 说明 |
|---|---|
| 编译条件 | 仅在 MBEDTLS_THREADING_ALT 宏启用时编译,确保 TLS 多线程安全 |
| 互斥锁适配 | 将 MBED TLS 的 mbedtls_threading_mutex_t 映射到 OneOS 的 os_mutex_t |
| 命名规则 | 自动生成互斥锁名称 tlsMu00, tlsMu01, ... 便于调试时识别 |
| 阻塞等待 | mbedtls_mutex_alt_lock 使用 OS_WAIT_FOREVER 无限等待,保证 TLS 握手不被打断 |