| 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 | int BaseVariableToManipulate = START_VALUE; |
| 29 | int ValueToExchange = SECOND_VALUE; |
| 30 | int TempValue; |
| 31 | int 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 | /* Compare START_VALUE with BaseVariableToManipulate, they're equal, |
| 43 | so exchange |
| 44 | */ |
| 45 | |
| 46 | TheReturn = InterlockedCompareExchange( |
| 47 | &BaseVariableToManipulate, /* Destination */ |
| 48 | ValueToExchange, /* Exchange value */ |
| 49 | START_VALUE); /* Compare value */ |
| 50 | |
| 51 | /* Exchanged, these should be equal now */ |
| 52 | if(BaseVariableToManipulate != ValueToExchange) |
| 53 | { |
| 54 | Fail("ERROR: A successful compare and exchange should have occurred, " |
| 55 | "making the variable have the value of %d, as opposed to the " |
| 56 | "current value of %d." , |
| 57 | ValueToExchange,BaseVariableToManipulate); |
| 58 | } |
| 59 | |
| 60 | /* Check to make sure it returns the original number which |
| 61 | 'BaseVariableToManipulate' was set to. |
| 62 | */ |
| 63 | if(TheReturn != START_VALUE) |
| 64 | { |
| 65 | Fail("ERROR: The return value after the first exchange should be the " |
| 66 | "former value of the variable, which was %d, but it is now %d." , |
| 67 | START_VALUE,TheReturn); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | ValueToExchange = THIRD_VALUE; /* Give this a new value */ |
| 73 | TempValue = BaseVariableToManipulate; /* Note value of Base */ |
| 74 | |
| 75 | /* |
| 76 | Do an exchange where 'BaseVariableToManipulate' doesn't |
| 77 | match -- therefore the exchange shouldn't happen. |
| 78 | So, it should end up the same as the 'TempValue' we saved. |
| 79 | */ |
| 80 | |
| 81 | InterlockedCompareExchange(&BaseVariableToManipulate, |
| 82 | ValueToExchange, |
| 83 | START_VALUE); |
| 84 | |
| 85 | if(BaseVariableToManipulate != TempValue) |
| 86 | { |
| 87 | Fail("ERROR: An attempted exchange should have failed due to " |
| 88 | "the compare failing. But, it seems to have succeeded. The " |
| 89 | "value should be %d but is %d in this case." , |
| 90 | TempValue,BaseVariableToManipulate); |
| 91 | } |
| 92 | |
| 93 | PAL_Terminate(); |
| 94 | return PASS; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |