Question

I have a struct called Ambigous, and inside the struct I have an array of pointers to other Ambigous.

I want to use OSAtomic.h library, to do CompareandSwaps.

However I am having trouble getting the array to play nice.

 OSAtomicCompareAndSwapPtrBarrier(void *__oldValue, void *__newValue, 
void *volatile *__theValue)

is the compare and swap function.

and inside my struct I have

 Ambigous * volatile* list;

and the call is

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, local->list[pos]);

When I try to cas by

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, (void * volatile *)local->list[pos]);

I get a bad EXE_BAD_ACCESS

So i guess what i am answering is how should i declare the array of volatile pointers?

Was it helpful?

Solution

Perhaps you want

bool res = OSAtomicCompareAndSwapPtrBarrier(
              current_node, new_node, local->list + pos);

Note that the CAS operation is basically

bool CAS(void* old, void* new_, void* volatile* val) {
   /*atomic*/ {
     if (old == *val) {
         *val = new_;
         return true;
     } else
         return false;
   }
}

If you pass a list[pos], the 3rd argument will be of type Ambigous*, and *val will be of type struct Ambigous which cannot be compared to a pointer.

OTHER TIPS

I think your problem isn't a type issue - you've misunderstood the semantics of OSAtomicCompareAndSwapPtrBarrier(). You need to give OSAtomicCompareAndSwapPtrBarrier a pointer to the memory location holding the pointer you want to update. In this case, that's the location of local->list[pos] - this can be written either as local->list + pos or perhaps more readably &(local->list[pos]).

Since you're dealing with C++, not plain C, so you will need a cast:

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, (void*volatile*)&(local->list[pos]));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top