This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

为什么要用临界区代码保护?使用场景是怎样的?

GateHwi_create是使用临界区代码保护的相关方法吧? 为什么要用临界区代码保护?使用场景是怎样的?

  • GateHwi_Handle GateHwi_create(const GateHwi_Params * params,Error_Block * eb);
    //分配并初始化一个新的实例对象并返回其句柄

    因为线程可以被更高优先级线程抢占,一些代码段在被另一个线程执行前需要被一个线程执行完毕。使用代码改变关键区域的全局变量这样的普通应用需要通过Gate来进行保护。

    Gates通常用于禁用一些级别的抢占,如禁用task切换或甚至硬件中断,或用于二元semaphore。通过使用一个key,所有Gate实现支持嵌套。

    请参考

    software-dl.ti.com/.../GateHwi.html

    GateHwi uses disabling and enabling of interrupts as the resource locking mechanism. Such a gate guarantees exclusive access to the CPU. This gate can be used whenever the resource is being shared by Tasks, Swis, and/or Hwis.
  • 这个是TI RTOS的内容了,你有兴趣了解的话,可以看下TI RTOS相关的文档:
    dev.ti.com/.../node
  • 这个功能是否可用条件变量的方法来替代?
  • 很抱歉,我没有这样试验过。

    Gates主要作用是 用来保护并发访问的代码关键数据区域(全局变量),防止线程被抢占后,同时修改同一资源。
    GateHwi是启用和禁用中断的锁定机构。这种门保证了CPU的独占访问。