00001 
00002 
00003 
00004 
00005 #pragma once
00006 
00007 namespace MPMA
00008 {
00009     
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     
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     
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     
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 };