| 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: Test for InterlockedCompareExchange() function |
| 10 | ** |
| 11 | ** |
| 12 | **=========================================================*/ |
| 13 | |
| 14 | /* This test is FINISHED. Note: The biggest feature of this function is that |
| 15 | it locks the value before it increments it -- in order to make it so only |
| 16 | one thread can access it. But, I really don't have a great test to make |
| 17 | sure it's thread safe. Any ideas? |
| 18 | */ |
| 19 | |
| 20 | #include <palsuite.h> |
| 21 | |
| 22 | #define START_VALUE 0 |
| 23 | #define SECOND_VALUE 5 |
| 24 | #define THIRD_VALUE 10 |
| 25 | |
| 26 | int __cdecl main(int argc, char *argv[]) { |
| 27 | |
| 28 | LONGLONG BaseVariableToManipulate = START_VALUE; |
| 29 | LONGLONG ValueToExchange = SECOND_VALUE; |
| 30 | LONGLONG TempValue; |
| 31 | LONGLONG TheReturn; |
| 32 | |
| 33 | /* |
| 34 | * Initialize the PAL and return FAILURE if this fails |
| 35 | */ |
| 36 | |
| 37 | if(0 != (PAL_Initialize(argc, argv))) |
| 38 | { |
| 39 | return FAIL; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | ** Run only on 64 bit platforms |
| 44 | */ |
| 45 | #if defined(BIT64) |
| 46 | /* Compare START_VALUE with BaseVariableToManipulate, they're equal, |
| 47 | so exchange |
| 48 | */ |
| 49 | |
| 50 | TheReturn = InterlockedCompareExchange64( |
| 51 | &BaseVariableToManipulate, /* Destination */ |
| 52 | ValueToExchange, /* Exchange value */ |
| 53 | START_VALUE); /* Compare value */ |
| 54 | |
| 55 | /* Exchanged, these should be equal now */ |
| 56 | if(BaseVariableToManipulate != ValueToExchange) |
| 57 | { |
| 58 | Fail("ERROR: A successful compare and exchange should have occurred, " |
| 59 | "making the variable have the value of %ll, as opposed to the " |
| 60 | "current value of %ll." , |
| 61 | ValueToExchange,BaseVariableToManipulate); |
| 62 | } |
| 63 | |
| 64 | /* Check to make sure it returns the original number which |
| 65 | 'BaseVariableToManipulate' was set to. |
| 66 | */ |
| 67 | if(TheReturn != START_VALUE) |
| 68 | { |
| 69 | Fail("ERROR: The return value after the first exchange should be the " |
| 70 | "former value of the variable, which was %ll, but it is now %ll." , |
| 71 | START_VALUE,TheReturn); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | ValueToExchange = THIRD_VALUE; /* Give this a new value */ |
| 78 | TempValue = BaseVariableToManipulate; /* Note value of Base */ |
| 79 | |
| 80 | /* |
| 81 | Do an exchange where 'BaseVariableToManipulate' doesn't |
| 82 | match -- therefore the exchange shouldn't happen. |
| 83 | So, it should end up the same as the 'TempValue' we saved. |
| 84 | */ |
| 85 | |
| 86 | InterlockedCompareExchange64(&BaseVariableToManipulate, |
| 87 | ValueToExchange, |
| 88 | START_VALUE); |
| 89 | |
| 90 | if(BaseVariableToManipulate != TempValue) |
| 91 | { |
| 92 | Fail("ERROR: An attempted exchange should have failed due to " |
| 93 | "the compare failing. But, it seems to have succeeded. The " |
| 94 | "value should be %ll but is %ll in this case." , |
| 95 | TempValue,BaseVariableToManipulate); |
| 96 | } |
| 97 | |
| 98 | #endif //if defined(BIT64) |
| 99 | PAL_Terminate(); |
| 100 | return PASS; |
| 101 | } |
| 102 | |