summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/miscellaneous/InterLockedExchangeAdd/test1/test.cpp
blob: ca9e5bb5bfae6bc763c96227ae2ef9b7db7c8dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
** Source : test.c
**
** Purpose: InterlockedExchangeAdd() function
**
**
**=========================================================*/

/*
** The InterlockedExchangeAdd function performs an atomic addition of Value 
** to the value pointed to by Addend. 
** The result is stored in the address specified by Addend. 
** The initial value of the variable pointed to by Addend is returned as the function value.
*/
#include <palsuite.h>

int __cdecl main(int argc, char *argv[]) 
{
    
    LONG TheReturn;
  
    LONG *ptrValue = NULL;
    /*
     * Initialize the PAL and return FAILURE if this fails
     */

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }



#if defined(BIT64) && defined(PLATFORM_UNIX)
    ptrValue = (LONG *) malloc(sizeof(LONG));

    if(ptrValue == NULL)
    {
		Fail("Error:%d:Malloc failed for ptrValue\n", GetLastError()); 
	}

	*ptrValue = (LONG) 0;

    TheReturn = InterlockedExchangeAdd( ptrValue, (LONG) 5);
    

    /* Added, it should be 5  now */
    if(*ptrValue != 5) 
    {
        
      Trace("ERROR: After an add operation, the value should be 5, "
             "but it is really %d.", *ptrValue);
      free(ptrValue);
      Fail("");
    }
  
    /* Check to make sure the function returns the original value (5 in this case) */
    if(TheReturn != 0) 
    {
        Trace("ERROR: The function should have returned the new value of %d "
             "but instead returned %d.", *ptrValue, TheReturn);    
        free(ptrValue);
		Fail("");
    }

    TheReturn = InterlockedExchangeAdd( ptrValue, (LONG) 1);


    /* Added twice, it should be 6  now */
    if(*ptrValue != 6) 
    {
        
      Trace("ERROR: After having two add operations, the value should be 6, "
             "but it is really %d.", *ptrValue);
      free(ptrValue);
      Fail("");
    }
  
    /* Check to make sure the function returns the original value (5 in this case) */
    if(TheReturn != 5) 
    {
        Trace("ERROR: The function should have returned the new value of %d "
             "but instead returned %d.", *ptrValue, TheReturn);    
        free(ptrValue);
		Fail("");
    }
    
    free(ptrValue);
#endif
    PAL_Terminate();
    return PASS; 
}