ARM中的关键部分 [英] Critical sections in ARM

查看:205
本文介绍了ARM中的关键部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AVR处理器系列的关键部分上有丰富的经验,您所要做的就是禁用中断(当然具有内存屏障),执行关键操作,然后重新启用中断:

I am experienced in implementing critical sections on the AVR family of processors, where all you do is disable interrupts (with a memory barrier of course), do the critical operation, and then reenable interrupts:

void my_critical_function()
{
   cli();  //Disable interrupts
   // Mission critical code here
   sei();  //Enable interrupts
}

现在我的问题是这个

这个简单的方法也适用于处理器的ARM体系结构吗?我听说过有关处理器在说明上进行前瞻性处理的事情,以及其他不可思议的问题,并且主要是想知道这些类型的事情是否可能对关键部分的实现造成问题.

Does this simple method apply to the ARM architecture of processor as well? I have heard things about the processor doing lookahead on the instructions, and other black magic, and was wondering primarily if these types of things could be problematic to this implementation of critical sections.

推荐答案

假设您使用的是Cortex-M处理器,请查看LDREXSTREX指令,它们可通过__STREXW()宏.它们可用于构建极其轻量级的互斥机制.

Assuming you're on a Cortex-M processor, take a look at the LDREX and STREX instructions, which are available in C via the __LDREXW() and __STREXW() macros provided by CMSIS (the Cortex Microcontroller Software Interface Standard). They can be used to build extremely lightweight mutual exclusion mechanisms.

基本上

data = __LDREXW(address)

的作用类似于data = *address,不同之处在于它在CPU中设置了独占访问标志".完成数据处理后,请使用

works like data = *address except that it sets an 'exclusive access flag' in the CPU. When you've finished manipulating your data, write it back using

success = __STREXW(address, data)

类似于*address = data,但只有在仍设置独占访问标志的情况下,写入操作才会成功.如果确实写入成功,那么还将清除该标志.成功返回0,失败返回1.如果STREX失败,则必须返回LDREX并重试.

which is like *address = data but will only succeed in writing if the exclusive access flag is still set. If it does succeed in writing then it also clears the flag. It returns 0 on success and 1 on failure. If the STREX fails, you have to go back to the LDREX and try again.

对于共享变量的简单互斥访问,不需要任何其他操作.例如:

For simple exclusive access to a shared variable, nothing else is required. For example:

do {
  data = LDREX(address);
  data++;
} while (STREXW(address, data));

关于此机制的有趣之处在于,它实际上是先到先得".如果此代码被中断并且中断使用LDREXSTREX,则STREX中断将成功执行,并且(低优先级)用户代码将必须重试.

The interesting thing about this mechanism is that it's effectively 'last come, first served'; if this code is interrupted and the interrupt uses LDREX and STREX, the STREX interrupt will succeed and the (lower-priority) user code will have to retry.

如果您使用的是操作系统,则可以使用相同的原语来构建适当的"信号灯和互斥锁(请参阅

If you're using an operating system, the same primitives can be used to build 'proper' semaphores and mutexes (see this application note, for example); but then again if you're using an OS you probably already have access to mutexes through its API!

这篇关于ARM中的关键部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆