| 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: InterlockedExchange() 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 | |
| 24 | int __cdecl main(int argc, char *argv[]) { |
| 25 | |
| 26 | int TheValue = START_VALUE; |
| 27 | int NewValue = 5; |
| 28 | int TheReturn; |
| 29 | |
| 30 | /* |
| 31 | * Initialize the PAL and return FAILURE if this fails |
| 32 | */ |
| 33 | |
| 34 | if(0 != (PAL_Initialize(argc, argv))) |
| 35 | { |
| 36 | return FAIL; |
| 37 | } |
| 38 | |
| 39 | TheReturn = InterlockedExchange(&TheValue,NewValue); |
| 40 | |
| 41 | /* Compare the exchanged value with the value we exchanged it with. Should |
| 42 | be the same. |
| 43 | */ |
| 44 | if(TheValue != NewValue) |
| 45 | { |
| 46 | Fail("ERROR: The value which was exchanged should now be %d, but " |
| 47 | "instead it is %d." ,NewValue,TheValue); |
| 48 | } |
| 49 | |
| 50 | /* Check to make sure it returns the origional number which 'TheValue' was |
| 51 | set to. |
| 52 | */ |
| 53 | |
| 54 | if(TheReturn != START_VALUE) |
| 55 | { |
| 56 | Fail("ERROR: The value returned should be the value before the " |
| 57 | "exchange happened, which was %d, but %d was returned." , |
| 58 | START_VALUE,TheReturn); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | PAL_Terminate(); |
| 63 | return PASS; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |