threading_alt.h 源码
2026/7/20大约 2 分钟附录源码附录MBED TLS线程安全
threading_alt.h — MBED TLS 线程互斥锁适配头文件
路径: components/security/mbedtls/port/inc/threading_alt.h
功能: 定义 MBED TLS 线程安全所需的互斥锁类型和接口,将 OneOS 互斥量(os_mutex_id)适配为 MBED TLS 的 mbedtls_threading_mutex_t。这是 MBED TLS 在多线程环境下安全运行的关键适配层。
引用章节: 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 threading_alt.h
*
* @brief mbedtls alternate threading functions header file
*
* @revision
* Date Author Notes
* 2020-11-25 OneOS Team First Version
***********************************************************************************************************************
*/
#ifndef __THREADING_ALT_H__
#define __THREADING_ALT_H__
#include "os_types.h"
#include "os_mutex.h"
#ifndef OS_USING_MUTEX
#error "MBEDTLS_THREADING_ALT need to define OS_USING_MUTEX!"
#endif
typedef struct mbedtls_threading_mutex
{
os_bool_t valid;
os_mutex_id mutex;
} mbedtls_threading_mutex_t;
extern void mbedtls_mutex_alt_init( mbedtls_threading_mutex_t *mutex );
extern void mbedtls_mutex_alt_free( mbedtls_threading_mutex_t *mutex );
extern int mbedtls_mutex_alt_lock( mbedtls_threading_mutex_t *mutex );
extern int mbedtls_mutex_alt_unlock( mbedtls_threading_mutex_t *mutex );
#endif /* __THREADING_ALT_H__ */关键说明
| 模块 | 说明 |
|---|---|
| 编译依赖 | 必须启用 OS_USING_MUTEX,否则编译报错 —— TLS 握手需要互斥锁保护共享状态 |
| valid 标志 | 用于标记互斥锁是否已初始化,防止重复初始化或释放未初始化的锁 |
| os_mutex_id | OneOS 内核互斥量句柄,通过 os_mutex_create() 创建 |
| 接口声明 | 四个 extern 函数分别对应初始化、销毁、加锁、解锁,由 threading_alt.c 实现 |
| 适配宏 | 通过 MBEDTLS_THREADING_ALT 宏启用,MBED TLS 内部所有临界区操作都通过此适配层 |