title: Why are the implementations of the spin lock different between Windows XP and Windows 7?
body: <p>I know the spinlock is exported by hal.dll in Windows, so I reverse engineered the code for the spin lock.  The results are below.</p>
  
  <p>Windows XP's decompiled spinlock.</p>
  
  <pre><code>unsigned __int32 __thiscall KfAcquireSpinLock(signed __int32 *this)
  {
    unsigned __int32 result; // eax@1
  
    result = __readfsdword(36);
    __writefsdword(36, 2u);
    while ( _interlockedbittestandset(this, 0) )
    {
      while ( *this &amp; 1 )
        _mm_pause();
    }
    return result;
  }
  </code></pre>
  
  <p>Windows 7's decompiled spinlock.</p>
  
  <pre><code>unsigned __int32 __fastcall KeAcquireSpinLockRaiseToSynch(signed __int32 *a1)
  {
    unsigned __int32 result; // eax@1
    int v2; // edx@4
    unsigned __int32 v3; // ST0C_4@7
    signed __int32 *v4; // ST08_4@7
    int v5; // ST04_4@7
  
    result = __readfsdword(36);
    __writefsbyte(36, 0x1Bu);
    while ( _interlockedbittestandset(a1, 0) )
    {
      v2 = 0;
      do
      {
        ++v2;
       if ( !(v2 &amp; dword_8002D1B0) )
        {
          if ( dword_8002D19C &amp; 0x40 )
          {
            v3 = result;
            v4 = a1;
            v5 = v2;
            dword_8002D1B4(v2);
            v2 = v5;
            a1 = v4;
            result = v3;
          }
        }
        _mm_pause();
      }
      while ( *a1 &amp; 1 );
   }
    return result;
  }
  </code></pre>
  
  <p>Why is the code different between the versions?  In particular, I don't see how the code added in the Windows 7 version improves the spinlock's performance in virtualization.</p>