在有多个任务调用创建弗雷托对象的函数的函数中,安全吗?
我有一个任务,可以根据存储在EEPROM中的模式字节来启动所有其他任务。这些任务调用其使用的所有硬件模块的初始化功能。但是,以I2C为例,几个任务使用相同的硬件。
这些初始化函数创建了Freertos对象,我确实可以防止它们不止一次创建,例如在下面的代码段中。我担心的是,我的启动任务是一次创建所有其他这些任务,并且在另一个任务已经调用它时,操作系统可能会导致任务调用INIT函数,尤其是在检查零值和实际创建Freertos之间目的。这是我应该更改设计的有效问题。
void I2C_Control_Init(void) {
for(int i2c_control = 0; i2c_control < I2C_CONTROL_NUM; i2c_control++) {
if (NULL == i2c_control_mutex[i2c_control]) {
i2c_control_mutex[i2c_control] = xSemaphoreCreateMutex();
configASSERT(i2c_control_mutex[i2c_control]); // Do not continue with null pointer.
}
}
return;
}
I have a task that starts up all of the other tasks depending upon a mode byte that is stored in EEPROM. These tasks call the init functions for all of the hardware modules they use. Several tasks use the same hardware, however, with I2C as an example.
These init functions create FreeRTOS objects and I do protect against them from being created more than once, such as in the code snippet below. My concern is that my Startup Task is creating all of these other tasks at once and the OS might be causing a task to call the init function when another task had already called it, particularly in between checking for the NULL value and actually creating the FreeRTOS object. Is this a valid concern for which I should change my design.
void I2C_Control_Init(void) {
for(int i2c_control = 0; i2c_control < I2C_CONTROL_NUM; i2c_control++) {
if (NULL == i2c_control_mutex[i2c_control]) {
i2c_control_mutex[i2c_control] = xSemaphoreCreateMutex();
configASSERT(i2c_control_mutex[i2c_control]); // Do not continue with null pointer.
}
}
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您标题问题的答案:是的,弗雷托斯(Freertos)处理OS对象的“并发”创建,这是安全的。您可以编写安全管理的函数。
但是,您共享的代码并不是线程安全/重新进入它的存储方式并处理这些创建的对象。如您所见,线程可能会迭代您的信号量数组,找到空,创建信号量并开始分配它,然后将任务切换为“转换”,而另一个线程则相同,当它返回时,您的原始线程将覆盖什么”中断“线程刚刚写的,您已经索要一个条目,并且在某个地方有一个潜在的孤儿信号量,具体取决于弗雷托斯的实施方式。
如何解决?
RTOS提供同步对象,例如静音等。您应该使用它们。
IE操作系统101
The answer to your title question: Yes, FreeRTOS handles "concurrent" creation of OS objects and that's safe. You can write a function that manages this safely.
Your shared code, however, is not thread safe/reentrant with how it stores off and deals with these created objects. As you see yourself, a thread could be iterating your semaphore array, find a NULL, create the semaphore and begin to assign it, then get task switched away and another thread does the same, when it returns your original thread will overwrite what the "interrupting" thread just wrote and you've double claimed an entry and there's a potentially orphan semaphore floating around somewhere, depending on how FreeRTOS is implemented.
How to solve?
The RTOS provides synchronization objects like mutexes, etc. You should use them.
i.e. operating systems 101