| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================ |
| 6 | ** |
| 7 | ** Source : test.c |
| 8 | ** |
| 9 | ** Purpose: InterlockedExchangeAdd() function |
| 10 | ** |
| 11 | ** |
| 12 | **=========================================================*/ |
| 13 | |
| 14 | /* |
| 15 | ** The InterlockedExchangeAdd function performs an atomic addition of Value |
| 16 | ** to the value pointed to by Addend. |
| 17 | ** The result is stored in the address specified by Addend. |
| 18 | ** The initial value of the variable pointed to by Addend is returned as the function value. |
| 19 | */ |
| 20 | #include <palsuite.h> |
| 21 | |
| 22 | int __cdecl main(int argc, char *argv[]) |
| 23 | { |
| 24 | |
| 25 | LONG TheReturn; |
| 26 | |
| 27 | LONG *ptrValue = NULL; |
| 28 | /* |
| 29 | * Initialize the PAL and return FAILURE if this fails |
| 30 | */ |
| 31 | |
| 32 | if(0 != (PAL_Initialize(argc, argv))) |
| 33 | { |
| 34 | return FAIL; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | #if defined(BIT64) |
| 40 | ptrValue = (LONG *) malloc(sizeof(LONG)); |
| 41 | |
| 42 | if(ptrValue == NULL) |
| 43 | { |
| 44 | Fail("Error:%d:Malloc failed for ptrValue\n" , GetLastError()); |
| 45 | } |
| 46 | |
| 47 | *ptrValue = (LONG) 0; |
| 48 | |
| 49 | TheReturn = InterlockedExchangeAdd( ptrValue, (LONG) 5); |
| 50 | |
| 51 | |
| 52 | /* Added, it should be 5 now */ |
| 53 | if(*ptrValue != 5) |
| 54 | { |
| 55 | |
| 56 | Trace("ERROR: After an add operation, the value should be 5, " |
| 57 | "but it is really %d." , *ptrValue); |
| 58 | free(ptrValue); |
| 59 | Fail("" ); |
| 60 | } |
| 61 | |
| 62 | /* Check to make sure the function returns the original value (5 in this case) */ |
| 63 | if(TheReturn != 0) |
| 64 | { |
| 65 | Trace("ERROR: The function should have returned the new value of %d " |
| 66 | "but instead returned %d." , *ptrValue, TheReturn); |
| 67 | free(ptrValue); |
| 68 | Fail("" ); |
| 69 | } |
| 70 | |
| 71 | TheReturn = InterlockedExchangeAdd( ptrValue, (LONG) 1); |
| 72 | |
| 73 | |
| 74 | /* Added twice, it should be 6 now */ |
| 75 | if(*ptrValue != 6) |
| 76 | { |
| 77 | |
| 78 | Trace("ERROR: After having two add operations, the value should be 6, " |
| 79 | "but it is really %d." , *ptrValue); |
| 80 | free(ptrValue); |
| 81 | Fail("" ); |
| 82 | } |
| 83 | |
| 84 | /* Check to make sure the function returns the original value (5 in this case) */ |
| 85 | if(TheReturn != 5) |
| 86 | { |
| 87 | Trace("ERROR: The function should have returned the new value of %d " |
| 88 | "but instead returned %d." , *ptrValue, TheReturn); |
| 89 | free(ptrValue); |
| 90 | Fail("" ); |
| 91 | } |
| 92 | |
| 93 | free(ptrValue); |
| 94 | #endif |
| 95 | PAL_Terminate(); |
| 96 | return PASS; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |