MITHREAD::INTERLOCKEDINT32 Class Reference

Interlocking in multi-core systems is used to ensure that the per-core cache agrees with all of the other per-core caches and main memory. More...

#include <mi32/interlockedint32.h>

List of all members.

Public Member Functions

 INTERLOCKEDINT32 (INT32 value)
 INTERLOCKEDINT32 (const INTERLOCKEDINT32 &rhs)
 INTERLOCKEDINT32 ()
 ~INTERLOCKEDINT32 ()
INT32 CompareAndExchange (INT32 NewValue, INT32 TestValue)
 operator INT32 () const
INTERLOCKEDINT32operator&= (INT32 with)
INT32 operator++ (int)
INTERLOCKEDINT32operator++ ()
INTERLOCKEDINT32operator+= (INT32 with)
INT32 operator-- (int)
INTERLOCKEDINT32operator-- ()
INTERLOCKEDINT32operator-= (INT32 with)
INTERLOCKEDINT32operator= (const INTERLOCKEDINT32 &rhs)
INTERLOCKEDINT32operator^= (INT32 with)
INTERLOCKEDINT32operator|= (INT32 with)

Detailed Description

Interlocking in multi-core systems is used to ensure that the per-core cache agrees with all of the other per-core caches and main memory.

On multi-core systems, there is a race condition with a shared variable that if the variable is in the core cache and not in the other core caches, then another thread will get the wrong value. This class wraps the UNIX and Windows INT32 interlocked API's to enforce the synchronization of INT32's. Most of the basic integer operators that would modify the INT32 value is available in this class. Since this synchronization is done in user mode and not by the OS, it is significantly faster than using mutexes and other synchronization operations.

   ERRVALUE MICACHE::Select (
      INT32 ItemID,
      ITEMINFO& iteminfo,
      SELECTFLAGS flags,
      void* UserDataPtr
      ) {
      ...
      if (m_CacheAccessCount < 0) return (SetErrPosnC(EInvalidCacheResize));     // This thread is attempting to access when another thread is resizing
      m_CacheAccessCount ++;     // After incrementing, all threads will see the same value for m_CacheAccessCount
      ...
      return (0);
      }


   void MICACHE::ITEMINFO::~ITEMINFO (
      ) {
      ...
      m_CacheAccessCount --;     // After decrementing, all threads will see the same value for m_CacheAccessCount
      ...
      }


   ERRVALUE MICACHEPRIV::Resize (
      INT32 NumCache,
      INT32 NumHash
      ) {
      ...
      // If m_CacheAccessCount > 0, then the return value will != 0 and the error will be sent.
      // That means that someone other thread is accessing the cache.  If m_CacheAccessCount == 0, 
      // then m_CacheAccessCount will be assigned -1, which the MICACHE::Select() code above is checking
      // for and returns an error if the cache is begin resized when another thread is accessing the cache.
      if (m_CacheAccessCount.CompareAndExchange(-1, 0) != 0) return (SetErrPosnC(EInvalidCacheResize));     // Some other thread is attempting to resize when other threads are accessing the cache

      ...
      // Allow the cache to be accessable again to the other threads.
      m_CacheAccessCount = 0;    // After assignment, all threads will see the same value for m_CacheAccessCount
      ...
      return (0);
      }

Constructor & Destructor Documentation

MITHREAD::INTERLOCKEDINT32::INTERLOCKEDINT32 (  ) 

Default ctor, initialize to zero.

MITHREAD::INTERLOCKEDINT32::INTERLOCKEDINT32 ( const INTERLOCKEDINT32 rhs  ) 

Copy ctor, init to zero then do an Interlocked Exchange.

MITHREAD::INTERLOCKEDINT32::INTERLOCKEDINT32 ( INT32  value  ) 

INT32 to this ctor, init to zero then do an Interlocked Exchange.

MITHREAD::INTERLOCKEDINT32::~INTERLOCKEDINT32 (  ) 

Default dtor.


Member Function Documentation

INT32 MITHREAD::INTERLOCKEDINT32::CompareAndExchange ( INT32  NewValue,
INT32  TestValue 
)

Performs the following operation atomically INT32 retval = 'this'; if ('this' == TestValue) 'this' = NewValue; return (retval);.

Returns:
Value of 'this' BEFORE the call.
MITHREAD::INTERLOCKEDINT32::operator INT32 (  )  const [inline]

Cast operator to get the INT32 value stored The compile condition is because Solaris 9 does not make available its atomic functions public and to attempt to maintain the fastest possible code for the other platforms.

Returns:
Signed integer value of 'this'
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator&= ( INT32  with  ) 

AND operator.

Returns:
'this'.
INT32 MITHREAD::INTERLOCKEDINT32::operator++ ( int   ) 

Post-increment operator, do an Interlocked Increment, return the value from the intrinsic function.

Returns:
Previous value stored in 'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator++ (  ) 

Pre-increment operator, do an Interlocked Increment, return this.

Returns:
'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator+= ( INT32  with  ) 

Addition operator, do an Interlocked Add.

Returns:
'this'.
INT32 MITHREAD::INTERLOCKEDINT32::operator-- ( int   ) 

Post-decrement operator, do an Interlocked Decrement, return the value from the intrinsic function.

Returns:
Previous value stored in 'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator-- (  ) 

Pre-decrement operator, do an Interlocked Decrement.

Returns:
'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator-= ( INT32  with  ) 

Subtraction operator, do an Interlocked Add.

Returns:
'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator= ( const INTERLOCKEDINT32 rhs  ) 

Assignment operator, do an Interlocked Exchange.

Returns:
'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator^= ( INT32  with  ) 

XOR operator.

Returns:
'this'.
INTERLOCKEDINT32& MITHREAD::INTERLOCKEDINT32::operator|= ( INT32  with  ) 

OR operator.

Returns:
'this'.

The documentation for this class was generated from the following file:

Generated on Sun Oct 7 21:36:38 2012 for TNTsdk 2012 by  doxygen 1.6.1