TestInterlockedAccess.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <stdio.h>
  2. #include <winpr/crt.h>
  3. #include <winpr/windows.h>
  4. #include <winpr/interlocked.h>
  5. int TestInterlockedAccess(int argc, char* argv[])
  6. {
  7. int index;
  8. LONG* Addend;
  9. LONG* Target;
  10. LONG oldValue;
  11. LONG* Destination;
  12. LONGLONG oldValue64;
  13. LONGLONG* Destination64;
  14. /* InterlockedIncrement */
  15. /*allocates memory on a specified alignment boundary*/
  16. Addend = _aligned_malloc(sizeof(LONG), sizeof(LONG));
  17. if (!Addend)
  18. {
  19. printf("Failed to allocate memory\n");
  20. return -1;
  21. }
  22. *Addend = 0;
  23. for (index = 0; index < 10; index++)
  24. InterlockedIncrement(Addend);
  25. if (*Addend != 10)
  26. {
  27. printf("InterlockedIncrement failure: Actual: %" PRId32 ", Expected: 10\n", *Addend);
  28. return -1;
  29. }
  30. /* InterlockedDecrement */
  31. for (index = 0; index < 10; index++)
  32. InterlockedDecrement(Addend);
  33. if (*Addend != 0)
  34. {
  35. printf("InterlockedDecrement failure: Actual: %" PRId32 ", Expected: 0\n", *Addend);
  36. return -1;
  37. }
  38. /* InterlockedExchange */
  39. Target = _aligned_malloc(sizeof(LONG), sizeof(LONG));
  40. if (!Target)
  41. {
  42. printf("Failed to allocate memory\n");
  43. return -1;
  44. }
  45. *Target = 0xAA;
  46. oldValue = InterlockedExchange(Target, 0xFF);
  47. if (oldValue != 0xAA)
  48. {
  49. printf("InterlockedExchange failure: Actual: 0x%08" PRIX32 ", Expected: 0xAA\n", oldValue);
  50. return -1;
  51. }
  52. if (*Target != 0xFF)
  53. {
  54. printf("InterlockedExchange failure: Actual: 0x%08" PRIX32 ", Expected: 0xFF\n", *Target);
  55. return -1;
  56. }
  57. /* InterlockedExchangeAdd */
  58. *Addend = 25;
  59. oldValue = InterlockedExchangeAdd(Addend, 100);
  60. if (oldValue != 25)
  61. {
  62. printf("InterlockedExchangeAdd failure: Actual: %" PRId32 ", Expected: 25\n", oldValue);
  63. return -1;
  64. }
  65. if (*Addend != 125)
  66. {
  67. printf("InterlockedExchangeAdd failure: Actual: %" PRId32 ", Expected: 125\n", *Addend);
  68. return -1;
  69. }
  70. /* InterlockedCompareExchange (*Destination == Comparand) */
  71. Destination = _aligned_malloc(sizeof(LONG), sizeof(LONG));
  72. if (!Destination)
  73. {
  74. printf("Failed to allocate memory\n");
  75. return -1;
  76. }
  77. *Destination = (LONG)0xAABBCCDDL;
  78. oldValue = InterlockedCompareExchange(Destination, (LONG)0xCCDDEEFFL, (LONG)0xAABBCCDDL);
  79. if (oldValue != (LONG)0xAABBCCDDL)
  80. {
  81. printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
  82. ", Expected: 0xAABBCCDD\n",
  83. oldValue);
  84. return -1;
  85. }
  86. if ((*Destination) != (LONG)0xCCDDEEFFL)
  87. {
  88. printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
  89. ", Expected: 0xCCDDEEFF\n",
  90. *Destination);
  91. return -1;
  92. }
  93. /* InterlockedCompareExchange (*Destination != Comparand) */
  94. *Destination = (LONG)0xAABBCCDDL;
  95. oldValue = InterlockedCompareExchange(Destination, 0xCCDDEEFFL, 0x66778899);
  96. if (oldValue != (LONG)0xAABBCCDDL)
  97. {
  98. printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
  99. ", Expected: 0xAABBCCDD\n",
  100. oldValue);
  101. return -1;
  102. }
  103. if ((*Destination) != (LONG)0xAABBCCDDL)
  104. {
  105. printf("InterlockedCompareExchange failure: Actual: 0x%08" PRIX32
  106. ", Expected: 0xAABBCCDD\n",
  107. *Destination);
  108. return -1;
  109. }
  110. /* InterlockedCompareExchange64 (*Destination == Comparand) */
  111. Destination64 = _aligned_malloc(sizeof(LONGLONG), sizeof(LONGLONG));
  112. if (!Destination64)
  113. {
  114. printf("Failed to allocate memory\n");
  115. return -1;
  116. }
  117. *Destination64 = 0x66778899AABBCCDD;
  118. oldValue64 =
  119. InterlockedCompareExchange64(Destination64, 0x8899AABBCCDDEEFF, 0x66778899AABBCCDD);
  120. if (oldValue64 != 0x66778899AABBCCDD)
  121. {
  122. printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
  123. ", Expected: 0x66778899AABBCCDD\n",
  124. oldValue64);
  125. return -1;
  126. }
  127. if ((*Destination64) != (LONGLONG)0x8899AABBCCDDEEFFLL)
  128. {
  129. printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
  130. ", Expected: 0x8899AABBCCDDEEFF\n",
  131. *Destination64);
  132. return -1;
  133. }
  134. /* InterlockedCompareExchange64 (*Destination != Comparand) */
  135. *Destination64 = 0x66778899AABBCCDDLL;
  136. oldValue64 = InterlockedCompareExchange64(Destination64, 0x8899AABBCCDDEEFFLL, 12345);
  137. if (oldValue64 != 0x66778899AABBCCDDLL)
  138. {
  139. printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
  140. ", Expected: 0x66778899AABBCCDD\n",
  141. oldValue64);
  142. return -1;
  143. }
  144. if (*Destination64 != 0x66778899AABBCCDDLL)
  145. {
  146. printf("InterlockedCompareExchange failure: Actual: 0x%016" PRIX64
  147. ", Expected: 0x66778899AABBCCDD\n",
  148. *Destination64);
  149. return -1;
  150. }
  151. _aligned_free(Addend);
  152. _aligned_free(Target);
  153. _aligned_free(Destination);
  154. _aligned_free(Destination64);
  155. return 0;
  156. }