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// Interlocked operations
5//
6
7#ifndef __GCENV_INTERLOCKED_H__
8#define __GCENV_INTERLOCKED_H__
9
10// Interlocked operations
11class Interlocked
12{
13private:
14
15#ifndef _MSC_VER
16 static void ArmInterlockedOperationBarrier();
17#endif // !_MSC_VER
18
19public:
20
21 // Increment the value of the specified 32-bit variable as an atomic operation.
22 // Parameters:
23 // addend - variable to be incremented
24 // Return:
25 // The resulting incremented value
26 template<typename T>
27 static T Increment(T volatile *addend);
28
29 // Decrement the value of the specified 32-bit variable as an atomic operation.
30 // Parameters:
31 // addend - variable to be decremented
32 // Return:
33 // The resulting decremented value
34 template<typename T>
35 static T Decrement(T volatile *addend);
36
37 // Perform an atomic AND operation on the specified values values
38 // Parameters:
39 // destination - the first operand and the destination
40 // value - second operand
41 template<typename T>
42 static void And(T volatile *destination, T value);
43
44 // Perform an atomic OR operation on the specified values values
45 // Parameters:
46 // destination - the first operand and the destination
47 // value - second operand
48 template<typename T>
49 static void Or(T volatile *destination, T value);
50
51 // Set a 32-bit variable to the specified value as an atomic operation.
52 // Parameters:
53 // destination - value to be exchanged
54 // value - value to set the destination to
55 // Return:
56 // The previous value of the destination
57 template<typename T>
58 static T Exchange(T volatile *destination, T value);
59
60 // Set a pointer variable to the specified value as an atomic operation.
61 // Parameters:
62 // destination - value to be exchanged
63 // value - value to set the destination to
64 // Return:
65 // The previous value of the destination
66 template <typename T>
67 static T ExchangePointer(T volatile * destination, T value);
68
69 template <typename T>
70 static T ExchangePointer(T volatile * destination, std::nullptr_t value);
71
72 // Perform an atomic addition of two 32-bit values and return the original value of the addend.
73 // Parameters:
74 // addend - variable to be added to
75 // value - value to add
76 // Return:
77 // The previous value of the addend
78 template<typename T>
79 static T ExchangeAdd(T volatile *addend, T value);
80
81 // Performs an atomic compare-and-exchange operation on the specified values.
82 // Parameters:
83 // destination - value to be exchanged
84 // exchange - value to set the destination to
85 // comparand - value to compare the destination to before setting it to the exchange.
86 // The destination is set only if the destination is equal to the comparand.
87 // Return:
88 // The original value of the destination
89 template<typename T>
90 static T CompareExchange(T volatile *destination, T exchange, T comparand);
91
92 // Performs an atomic compare-and-exchange operation on the specified pointers.
93 // Parameters:
94 // destination - value to be exchanged
95 // exchange - value to set the destination to
96 // comparand - value to compare the destination to before setting it to the exchange.
97 // The destination is set only if the destination is equal to the comparand.
98 // Return:
99 // The original value of the destination
100 template <typename T>
101 static T CompareExchangePointer(T volatile *destination, T exchange, T comparand);
102
103 template <typename T>
104 static T CompareExchangePointer(T volatile *destination, T exchange, std::nullptr_t comparand);
105};
106
107#endif // __GCENV_INTERLOCKED_H__
108