硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3814|回复: 3
收起左侧

[μCOS-II] 关于uCOS中OS_TLS_TBL_SIZE这个宏的理解?

[复制链接]

16

主题

148

回帖

196

积分

初级会员

积分
196
发表于 2020-3-6 14:05:42 | 显示全部楼层 |阅读模式
不管是在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 Safety

In 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.



os_tls.c

32.29 KB, 下载次数: 4

适配IAR

os_tls_v8.c

31.99 KB, 下载次数: 3

适配IAR v8

回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115787
QQ
发表于 2020-3-6 14:37:50 | 显示全部楼层
MDK的是这样的:

不支持多线程的API,MDK里面的RTX和FreeRTOS是在xxx_lib.C里面建立了一批锁机制:






回复

使用道具 举报

16

主题

148

回帖

196

积分

初级会员

积分
196
 楼主| 发表于 2020-3-6 14:45:20 | 显示全部楼层
eric2013 发表于 2020-3-6 14:37
MDK的是这样的:

不支持多线程的API,MDK里面的RTX和FreeRTOS是在xxx_lib.C里面建立了一批锁机制:

硬汉兄 在实际项目中 其实这些不支持可重入的api 使用的也比较少把
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
115787
QQ
发表于 2020-3-6 14:58:25 | 显示全部楼层
dyhfaily 发表于 2020-3-6 14:45
硬汉兄 在实际项目中 其实这些不支持可重入的api 使用的也比较少把

比较少。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-5-18 17:23 , Processed in 0.218275 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表