相当于LWARX和STWCX的x86 [英] x86 equivalent for LWARX and STWCX

查看:83
本文介绍了相当于LWARX和STWCX的x86的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找等效的LWARX和STWCX(在PowerPC处理器上可以找到)或在x86平台上实现类似功能的方法.另外,在哪里找到此类事情的最佳位置(例如,好的文章/网站/用于锁定/无需等待编程的论坛).


修改
我认为我可能需要提供更多详细信息,因为我只是在寻找CAS(比较和交换)操作.我正在尝试的是实现一个具有智能指针的无锁引用计数系统,该指针可以由多个线程访问和更改.我基本上需要一种在x86处理器上实现以下功能的方法.

int* IncrementAndRetrieve(int **ptr)
{
  int val;
  int *pval;
  do
  {
    // fetch the pointer to the value
    pval = *ptr;

    // if its NULL, then just return NULL, the smart pointer
    // will then become NULL as well
    if(pval == NULL)
      return NULL;

    // Grab the reference count
    val = lwarx(pval);

    // make sure the pointer we grabbed the value from
    // is still the same one referred to by  'ptr'
    if(pval != *ptr)
      continue;

    // Increment the reference count via 'stwcx' if any other threads
    // have done anything that could potentially break then it should
    // fail and try again
  } while(!stwcx(pval, val + 1));
  return pval;
}

我确实需要相当准确地模仿LWARX和STWCX的东西来实现这一目标(我无法找到一种方法来使用CompareExchange,交换或添加我到目前为止为x86找到的功能). /p>

谢谢

解决方案

正如Michael所说,您可能正在寻找的是cmpxchg指令.

重要的是要指出,尽管实现PPC的方法称为加载链接/有条件存储(LL/SC),而x86架构则使用比较并交换(CAS). LL/SC比CAS具有更强的语义,因为条件地址上的值的任何更改都将导致存储失败,即使其他更改将值替换为负载条件相同的值也是如此.另一方面,CAS在这种情况下会成功.这就是所谓的ABA问题(有关更多信息,请参见CAS链接).

如果在x86体系结构上需要更强的语义,则可以使用x86s双宽度比较和交换(DWCAS)指令cmpxchg8b或x86_64下的cmpxchg16b对其进行近似.这使您可以一次原子交换两个连续的自然大小"的单词,而不仅仅是通常的一个.基本思想是两个单词中的一个包含感兴趣的值,而另一个则包含始终递增的变异计数".尽管从技术上讲这还不能解决问题,但突变计数器在两次尝试之间发生换行的可能性非常低,以至于可以替代大多数用途.

I'm looking for an equivalent of LWARX and STWCX (as found on the PowerPC processors) or a way to implement similar functionality on the x86 platform. Also, where would be the best place to find out about such things (i.e. good articles/web sites/forums for lock/wait-free programing).


Edit
I think I might need to give more details as it is being assumed that I'm just looking for a CAS (compare and swap) operation. What I'm trying to do is implement a lock-free reference counting system with smart pointers that can be accessed and changed by multiple threads. I basically need a way to implement the following function on an x86 processor.

int* IncrementAndRetrieve(int **ptr)
{
  int val;
  int *pval;
  do
  {
    // fetch the pointer to the value
    pval = *ptr;

    // if its NULL, then just return NULL, the smart pointer
    // will then become NULL as well
    if(pval == NULL)
      return NULL;

    // Grab the reference count
    val = lwarx(pval);

    // make sure the pointer we grabbed the value from
    // is still the same one referred to by  'ptr'
    if(pval != *ptr)
      continue;

    // Increment the reference count via 'stwcx' if any other threads
    // have done anything that could potentially break then it should
    // fail and try again
  } while(!stwcx(pval, val + 1));
  return pval;
}

I really need something that mimics LWARX and STWCX fairly accurately to pull this off (I can't figure out a way to do this with the CompareExchange, swap or add functions I've so far found for the x86).

Thanks

解决方案

As Michael mentioned, what you're probably looking for is the cmpxchg instruction.

It's important to point out though that the PPC method of accomplishing this is known as Load Link / Store Conditional (LL/SC), while the x86 architecture uses Compare And Swap (CAS). LL/SC has stronger semantics than CAS in that any change to the value at the conditioned address will cause the store to fail, even if the other change replaces the value with the same value that the load was conditioned on. CAS, on the other hand, would succeed in this case. This is known as the ABA problem (see the CAS link for more info).

If you need the stronger semantics on the x86 architecture, you can approximate it by using the x86s double-width compare-and-swap (DWCAS) instruction cmpxchg8b, or cmpxchg16b under x86_64. This allows you to atomically swap two consecutive 'natural sized' words at once, instead of just the usual one. The basic idea is one of the two words contains the value of interest, and the other one contains an always incrementing 'mutation count'. Although this does not technically eliminate the problem, the likelihood of the mutation counter to wrap between attempts is so low that it's a reasonable substitute for most purposes.

这篇关于相当于LWARX和STWCX的x86的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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