home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xwphescr.zip / XWPH0208.ZIP / src / helpers / interlock.asm < prev    next >
Assembly Source File  |  2002-08-08  |  4KB  |  126 lines

  1.  
  2. ;/*
  3. ; * Interlocked apis
  4. ; *
  5. ; * Copyright 1999 Sander van Leeuwen
  6. ; * Copyright 1999 Patrick Haller
  7. ; *
  8. ; * Based on WINE code: win32\thread.c (990815)
  9. ; *
  10. ; * Copyright 1995 Martin von Loewis
  11. ; * Copyright 1997 Onno Hovers
  12. ; *
  13. ; * Project Odin Software License can be found in LICENSE.TXT
  14. ; *
  15. ; */
  16. .586P
  17.                NAME    interlocked
  18.  
  19. CODE32         SEGMENT DWORD PUBLIC USE32 'CODE'
  20.  
  21. ;* LONG DosInterlockedIncrement(PLONG);
  22. ;*
  23. ;* InterlockedIncrement adds 1 to a long variable and returns
  24. ;*  -  a negative number if the result < 0
  25. ;*  -  zero if the result == 0
  26. ;*  -  a positive number if the result > 0
  27. ;*
  28. ;* The returned number need not be equal to the result!!!!
  29.  
  30. public  DosInterlockedIncrement
  31. DosInterlockedIncrement proc near
  32.         ;we are allowed to trash edx
  33.         mov     edx, dword ptr [esp+4] ; LPLONG lpAddend
  34.         mov     eax, 1
  35.         lock    xadd dword ptr [edx], eax
  36.         inc     eax
  37.         ret     4
  38. DosInterlockedIncrement endp
  39.  
  40. ;* LONG DosInterlockedDecrement(PLONG);
  41. ;*
  42. ;* InterlockedIncrement adds 1 to a long variable and returns
  43. ;*  -  a negative number if the result < 0
  44. ;*  -  zero if the result == 0
  45. ;*  -  a positive number if the result > 0
  46. ;*
  47. ;* The returned number need not be equal to the result!!!!
  48.  
  49. public  DosInterlockedDecrement
  50. DosInterlockedDecrement proc near
  51.         ;we are allowed to trash edx
  52.         mov     edx, dword ptr [esp+4] ; LPLONG lpAddend
  53.         mov     eax, -1
  54.         lock    xadd dword ptr [edx], eax
  55.         dec     eax
  56.         ret     4
  57. DosInterlockedDecrement endp
  58.  
  59.  
  60. ; * LONG DosInterlockedExchange(PLONG, LONG);
  61. ; *
  62. ; * Atomically exchanges a pair of values.
  63. ; *
  64. ; * RETURNS
  65. ; * Prior value of value pointed to by Target
  66.  
  67. public  DosInterlockedExchange
  68. DosInterlockedExchange proc near
  69.         push    edx
  70.         mov     eax, [esp+12]           ; LONG value
  71.         mov     edx,[esp+8]             ; LPLONG target
  72.         lock    xchg eax, dword ptr [edx]
  73.         pop     edx
  74.         ret     8
  75. DosInterlockedExchange endp
  76.  
  77.  
  78. ;/************************************************************************
  79. ; * LONG DosInterlockedCompareExchange(PLONG dest, LONG xchg, LONG compare)
  80. ; *
  81. ; * Atomically compares Destination and Comperand, and if found equal exchanges
  82. ; * the value of Destination with Exchange
  83. ; *
  84. ; */
  85.  
  86. public DosInterlockedCompareExchange
  87. DosInterlockedCompareExchange proc near
  88.         push    ebp
  89.         mov     ebp, esp
  90.         push    edx
  91.         push    ebx
  92.  
  93.         mov     ebx, dword ptr [ebp+8]  ;PVOID *Destination, /* Address of 32-bit value to exchange */
  94.         mov     eax, [ebp+16]       ;PVOID Comperand      /* value to compare, 32 bits */
  95.         mov     edx, [ebp+12]       ;PVOID Exchange,      /* change value, 32 bits */
  96.         lock    cmpxchg dword ptr [ebx],edx
  97.  
  98.         pop     ebx
  99.         pop     edx
  100.         pop     ebp
  101.         ret     12
  102. DosInterlockedCompareExchange endp
  103.  
  104. ; * LONG DosInterlockedExchangeAdd(PLONG dest, LONG incr);
  105. ; *
  106. ; * Atomically adds Increment to Addend and returns the previous value of
  107. ; * Addend
  108. ; *
  109. ; * RETURNS
  110. ; * Prior value of value pointed to by cwAddendTarget
  111. ; */
  112.  
  113. public DosInterlockedExchangeAdd
  114. DosInterlockedExchangeAdd proc near
  115.         push    edx
  116.         mov     eax, dword ptr [esp+12] ;LONG Increment /* Value to add */
  117.         mov     edx, dword ptr [esp+8]  ;PLONG Addend, /* Address of 32-bit value to exchange */
  118.         lock    xadd dword ptr [edx], eax
  119.         pop     edx
  120.         ret     8
  121. DosInterlockedExchangeAdd endp
  122.  
  123. CODE32          ENDS
  124.  
  125.                 END
  126.