不管是在uCOSII 还是III中 都这么一个宏名 OS_TLS_TBL_SIZE,它使能后具体有什么作用我一直没去搞明白,今天我大概看了一下,说一下我的看法。
全局搜索 OS_TLS_TBL_SIZE 宏名 发现用到的地方不是特别多,主要集中在OSInit(),任务创建 创建,还有任务控制块中。
OSInit()算是最早和OS_TLS_TBL_SIZE 扯上关系的地方 ,通过这个宏来开关一个函数 OS_TLS_Init(&err);
在谷歌搜索 OS_TLS_Init函数时,找到了官方的文档说明链接:https://doc.micrium.com/pages/viewpage.action?pageId=16880163
按照文档我的理解是:在一些c标准库使用时要保证线程安全,比如malloc() free()等 ,这时需要包含一个文件 os_tls.c 这个文件有官方提供,需要注意的是这个文件是分编译器的,官方给出了IAR的,没有给MDK的,该文件我是在最新的uCOS II v2.93.00中找到的
最新uCOS全家桶源码地址:https://github.com/SiliconLabs 该文件在此目录下:uC-OS2-master\TLS\IAR 我也会在附件中上传该文件
这个文件主要实现了一下几个函数(通过信号量来达到互斥的目的)
void __iar_system_Mtxinit(__iar_Rmtx *); // Initialize a system lock
void __iar_system_Mtxdst(__iar_Rmtx *); // Destroy a system lock
void __iar_system_Mtxlock(__iar_Rmtx *); // Lock a system lock
void __iar_system_Mtxunlock(__iar_Rmtx *); // Unlock a system lock
void __iar_file_Mtxinit(__iar_Rmtx *); // Initialize a file lock
void __iar_file_Mtxdst(__iar_Rmtx *); // Destroy a file lock
void __iar_file_Mtxlock(__iar_Rmtx *); // Lock a file lock
void __iar_file_Mtxunlock(__iar_Rmtx *); // Unlock a file lock
以上函数均在IAR的Dlib_Threads.h文件中有声明
我们在使用c标准库时 需要包含 os_tls.c 文件 并且 OS_TLS_TBL_SIZE设置成一个合适的值 这个值 官方文档中的原文建议:
- Set OS_TLS_TBL_SIZE in os_cfg.h to a value greater than 1. The actual value depends on the number of entries needed by the compiler used. In most cases you would set this to 5 but you should consult the os_tls.c that you plan to use for additional information.
以下是官方使能线程安全的步骤
Enabling Thread SafetyIn order to enable thread safety, you need to do the following: - Set OS_TLS_TBL_SIZE in os_cfg.h to a value greater than 1. The actual value depends on the number of entries needed by the compiler used. In most cases you would set this to 5 but you should consult the os_tls.c that you plan to use for additional information.
- Add to your build, the os_tls.c file that corresponds to the compiler you are using.
- Depending on the compiler and how TLS is allocated, you may also need to make sure that you have a heap. Consult your compiler documentation on how you can enable the heap and determine its size.
- Most likely, os_tls.c will make use of semaphores to guard access to shared resources (such as the heap or files) then you need to make sure OS_SEM_EN is set to 1 in os_cfg.h. Also, the run-time library may already define APIs to lock and unlock sections of code. The implementation of these functions should also be part of os_tls.c.
|