简体   繁体   English

将结构传递给内置的gcc以进行原子访问

[英]pass a structure to gcc built-ins for atomic accesses

I have a two structures just like below: 我有两个结构,如下所示:

template <class T>
struct pointer_t
{
    T *ptr;
    uintptr_t tag;
};


template <class T>
struct Node
{
    T data;
    pointer_t<Node<T> > next;
};

and now I want to pass for example pointer_t<Node<T> > newNext to __sync_bool_compare_and_swap() function. 现在我想将例如pointer_t<Node<T> > newNext__sync_bool_compare_and_swap()函数。 According to the prototype of the function I pass: 根据函数的原型,我通过:

__sync_bool_compare_and_swap((unsigned long*) &newTail.ptr->next, oldNext, newNext)

The problem is that if I don't cast newNext to unsigned long I'll get: 问题是,如果我不将newNextunsigned long我将得到:

error: ‘struct pointer_t<Node<int> >’ used where a ‘long unsigned int’ was expected
      if ( __sync_bool_compare_and_swap((unsigned long*) &newTail.ptr->next, newNext, newNext) )

if I cast it to unsigned long then: 如果我将其强制转换为unsigned long,那么:

if ( __sync_bool_compare_and_swap((unsigned long*) &newTail.ptr->next, (unsigned long) oldNext, (unsigned long) newNext) )

I'll get: 我去拿:

error: ‘struct pointer_t<Node<int> >’ used where a ‘long unsigned int’ was expected.

can someone explain me you can I use __sync_bool_compare_and_swap with these two structures? 有人可以向我解释一下,您可以对这两种结构使用__sync_bool_compare_and_swap吗?

thanks 谢谢

#include <iostream>
#include <bitset>
using namespace std;

template <class T>
struct pointer_t
{
    T *ptr;
    uintptr_t tag;
};


template <class T>
struct Node
{
    T data;
    pointer_t<Node<T> >* next;
};

int main()
{
    Node<int> *newTail = new Node<int>();
    pointer_t<Node<int> > *oldNext = newTail->next;


    Node<int> *newNext = new Node<int>();

    pointer_t<Node<int> >* newNextPtr = new pointer_t<Node<int> >();
    newNextPtr->ptr=newNext;

    if ( __sync_bool_compare_and_swap(&newTail->next, oldNext, newNextPtr)) {
        std::cout<<"Gotcha!\n";
      }
 }

Which doesn't really solve your pointer tagging problem. 这并不能真正解决您的指针标记问题。 If you want to achieve pointer tagging then, just steal a few unused bits from the pointer and set those bits to tag the pointer, then unset them to reset the pointer. 如果要实现指针标记,则只需从指针中窃取一些未使用的位,然后将这些位设置为标记指针,然后取消设置它们以重置指针。 Don't forget to untag the bits before dereferencing. 不要忘记在取消引用之前先对这些位进行标记。

#define get_markedness(p) (((ptr_int) (p)) & 3)
#define get_unmarked_reference(p) ((void *) (((ptr_int) (p)) & (~3)))
#define get_marked_reference(p,m) ((void *) (((ptr_int) (p)) | m))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM