LocksWin32.h

00001 //Thread Locking Constructs
00002 //Luke Lenhart, 2007
00003 //See /docs/License.txt for details on how this code may be used.
00004 
00005 #pragma once
00006 
00007 namespace MPMA
00008 {
00009     //Atomically increments an integer
00010     inline void AtomicIntInc(volatile uint *pint)
00011     {
00012         __asm
00013         {
00014             mov eax, pint;
00015             lock inc dword ptr [eax];
00016         }
00017     }
00018 
00019     //Atomically decrements an integer
00020     inline void AtomicIntDec(volatile uint *pint)
00021     {
00022         __asm
00023         {
00024             mov eax, pint;
00025             lock dec dword ptr [eax];
00026         }
00027     }
00028     
00029     //Atomically adds one integer to another and returns the value of the original
00030     inline int AtomicIntAdd(volatile int *pint, int addValue)
00031     {
00032         int rval;
00033         __asm
00034         {
00035             mov eax, addValue;
00036             mov ebx, pint;
00037             lock xadd dword ptr [ebx], eax;
00038             mov rval, eax;
00039         }
00040         return rval;
00041     }
00042 
00043     //Compares expectedValue with the value at pInt, and if they are the same, sets pInt to newValue and returns true with outResultValue set to newValue.  If they are different then pInt is unaffected, and returns false with outResultValue set to the value that was found at pInt.
00044     inline bool AtomicCompareExchange(volatile uint *pInt, uint expectedValue, uint newValue, volatile uint &outResultValue)
00045     {
00046         uint rval;
00047         uint changed=0;
00048         __asm
00049         {
00050             mov eax, expectedValue;
00051             mov ebx, pInt;
00052             mov ecx, newValue;
00053             lock cmpxchg [ebx], ecx;
00054             jnz AtomCmpExch_Diff;
00055             mov eax,ecx;
00056             inc changed;
00057             AtomCmpExch_Diff:
00058             mov rval, eax;
00059         }
00060         outResultValue=rval;
00061         return changed!=0;;
00062     }
00063 
00064 }; //namespace Platform

Generated on Sat Aug 9 15:05:05 2008 for MPMA Framework by  doxygen 1.5.6