r6:Function:abstraction cas

From liblfds.org
Jump to navigation Jump to search

Source Files

/src/abstraction/abstraction_cas.c
/inc/liblfds.h

Prototype

INLINE atom_t abstraction_cas( volatile atom_t *destination, atom_t exchange, atom_t compare )

Parameters

volatile atom_t *destination

Address of the destination.

atom_t exchange

The value of the placed into destination if destination and compare are equal.

atom_t compare

The value to compare to destination; the value pointed to by compare must be over-written, after the compare, with the original value of destination.

Return Value

Returns the original value of *destination.

Notes

Be aware the prototype for abstraction_cas is distinctly different to the prototype for abstraction_dcas. The reason for this is that abstraction_cas works on pointer length values which can accordingly be directly passed into the function; abstraction_dcas is working on double pointer length values, which have to be passed in as a pointer to an array.

Examples

64-bit and 32-bit Windows (user-mode and kernel-mode) on any CPU

#if (defined _WIN32 && defined _MSC_VER)

  /* TRD : 64 bit and 32 bit Windows (user-mode or kernel) on any CPU with the Microsoft C compiler

           _WIN32    indicates 64-bit or 32-bit Windows
           _MSC_VER  indicates Microsoft C compiler
  */

  INLINE atom_t abstraction_cas( volatile atom_t *destination, atom_t exchange, atom_t compare )
  {
    return( (atom_t) _InterlockedCompareExchangePointer((void * volatile *) destination, (void *) exchange, (void *) compare) );
  }

#endif

_InterlockedCompareExchangePointer is a compiler instrinsic and so is available on all platforms using the MS C compiler. However, the documentation states that; "On the x86 architecture, _InterlockedCompareExchangePointer is a macro that calls _InterlockedCompareExchange." In fact, this macro is missing. To compensate, it is defined in liblfds.h, in the abstraction layer for x86 on user-mode Windows. (Kernel-mode compiles without any problems).

See Also