/
Atomic.Int.cs
118 lines (109 loc) · 6.32 KB
/
Atomic.Int.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Runtime.CompilerServices;
namespace DotNext.Threading;
public static partial class Atomic
{
/// <summary>
/// Atomically updates the current value with the results of applying the given function
/// to the current and given values, returning the updated value.
/// </summary>
/// <remarks>
/// The function is applied with the current value as its first argument, and the given update as the second argument.
/// </remarks>
/// <typeparam name="TAccumulator">The type implementing accumulator.</typeparam>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="x">Accumulator operand.</param>
/// <param name="accumulator">A side-effect-free function of two arguments.</param>
/// <returns>The updated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint AccumulateAndGet<TAccumulator>(ref nint value, nint x, TAccumulator accumulator)
where TAccumulator : notnull, ISupplier<nint, nint, nint>
=> Accumulate<nint, TAccumulator, InterlockedOperations>(ref value, x, accumulator).NewValue;
/// <summary>
/// Atomically updates the current value with the results of applying the given function
/// to the current and given values, returning the updated value.
/// </summary>
/// <remarks>
/// The function is applied with the current value as its first argument, and the given update as the second argument.
/// </remarks>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="x">Accumulator operand.</param>
/// <param name="accumulator">A side-effect-free function of two arguments.</param>
/// <returns>The updated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint AccumulateAndGet(ref nint value, nint x, Func<nint, nint, nint> accumulator)
=> AccumulateAndGet<DelegatingSupplier<nint, nint, nint>>(ref value, x, accumulator);
/// <summary>
/// Atomically updates the current value with the results of applying the given function
/// to the current and given values, returning the original value.
/// </summary>
/// <remarks>
/// The function is applied with the current value as its first argument, and the given update as the second argument.
/// </remarks>
/// <typeparam name="TAccumulator">The type implementing accumulator.</typeparam>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="x">Accumulator operand.</param>
/// <param name="accumulator">A side-effect-free function of two arguments.</param>
/// <returns>The original value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint GetAndAccumulate<TAccumulator>(ref nint value, nint x, TAccumulator accumulator)
where TAccumulator : notnull, ISupplier<nint, nint, nint>
=> Accumulate<nint, TAccumulator, InterlockedOperations>(ref value, x, accumulator).OldValue;
/// <summary>
/// Atomically updates the current value with the results of applying the given function
/// to the current and given values, returning the original value.
/// </summary>
/// <remarks>
/// The function is applied with the current value as its first argument, and the given update as the second argument.
/// </remarks>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="x">Accumulator operand.</param>
/// <param name="accumulator">A side-effect-free function of two arguments.</param>
/// <returns>The original value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint GetAndAccumulate(ref nint value, nint x, Func<nint, nint, nint> accumulator)
=> GetAndAccumulate<DelegatingSupplier<nint, nint, nint>>(ref value, x, accumulator);
/// <summary>
/// Atomically updates the stored value with the results
/// of applying the given function, returning the updated value.
/// </summary>
/// <typeparam name="TUpdater">The type implementing updater.</typeparam>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="updater">A side-effect-free function.</param>
/// <returns>The updated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint UpdateAndGet<TUpdater>(ref nint value, TUpdater updater)
where TUpdater : notnull, ISupplier<nint, nint>
=> Update<nint, TUpdater, InterlockedOperations>(ref value, updater).NewValue;
/// <summary>
/// Atomically updates the stored value with the results
/// of applying the given function, returning the updated value.
/// </summary>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="updater">A side-effect-free function.</param>
/// <returns>The updated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint UpdateAndGet(ref nint value, Func<nint, nint> updater)
=> UpdateAndGet<DelegatingSupplier<nint, nint>>(ref value, updater);
/// <summary>
/// Atomically updates the stored value with the results
/// of applying the given function, returning the original value.
/// </summary>
/// <typeparam name="TUpdater">The type implementing updater.</typeparam>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="updater">A side-effect-free function.</param>
/// <returns>The original value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint GetAndUpdate<TUpdater>(ref nint value, TUpdater updater)
where TUpdater : notnull, ISupplier<nint, nint>
=> Update<nint, TUpdater, InterlockedOperations>(ref value, updater).OldValue;
/// <summary>
/// Atomically updates the stored value with the results
/// of applying the given function, returning the original value.
/// </summary>
/// <param name="value">Reference to a value to be modified.</param>
/// <param name="updater">A side-effect-free function.</param>
/// <returns>The original value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint GetAndUpdate(ref nint value, Func<nint, nint> updater)
=> GetAndUpdate<DelegatingSupplier<nint, nint>>(ref value, updater);
}