/
ImmutableInterlocked.cs
627 lines (555 loc) · 33.2 KB
/
ImmutableInterlocked.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Collections.Immutable
{
/// <summary>
/// Contains interlocked exchange mechanisms for immutable collections.
/// </summary>
public static class ImmutableInterlocked
{
/// <summary>
/// Mutates a value in-place with optimistic locking transaction semantics
/// via a specified transformation function.
/// The transformation is retried as many times as necessary to win the optimistic locking race.
/// </summary>
/// <typeparam name="T">The type of data.</typeparam>
/// <param name="location">
/// The variable or field to be changed, which may be accessed by multiple threads.
/// </param>
/// <param name="transformer">
/// A function that mutates the value. This function should be side-effect free,
/// as it may run multiple times when races occur with other threads.</param>
/// <returns>
/// <c>true</c> if the location's value is changed by applying the result of the
/// <paramref name="transformer"/> function;
/// <c>false</c> if the location's value remained the same because the last
/// invocation of <paramref name="transformer"/> returned the existing value.
/// </returns>
public static bool Update<T>(ref T location, Func<T, T> transformer) where T : class?
{
Requires.NotNull(transformer, nameof(transformer));
bool successful;
T oldValue = Volatile.Read(ref location);
do
{
T newValue = transformer(oldValue);
if (ReferenceEquals(oldValue, newValue))
{
// No change was actually required.
return false;
}
T interlockedResult = Interlocked.CompareExchange(ref location, newValue, oldValue);
successful = ReferenceEquals(oldValue, interlockedResult);
oldValue = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
return true;
}
/// <summary>
/// Mutates a value in-place with optimistic locking transaction semantics
/// via a specified transformation function.
/// The transformation is retried as many times as necessary to win the optimistic locking race.
/// </summary>
/// <typeparam name="T">The type of data.</typeparam>
/// <typeparam name="TArg">The type of argument passed to the <paramref name="transformer"/>.</typeparam>
/// <param name="location">
/// The variable or field to be changed, which may be accessed by multiple threads.
/// </param>
/// <param name="transformer">
/// A function that mutates the value. This function should be side-effect free,
/// as it may run multiple times when races occur with other threads.</param>
/// <param name="transformerArgument">The argument to pass to <paramref name="transformer"/>.</param>
/// <returns>
/// <c>true</c> if the location's value is changed by applying the result of the
/// <paramref name="transformer"/> function;
/// <c>false</c> if the location's value remained the same because the last
/// invocation of <paramref name="transformer"/> returned the existing value.
/// </returns>
public static bool Update<T, TArg>(ref T location, Func<T, TArg, T> transformer, TArg transformerArgument) where T : class?
{
Requires.NotNull(transformer, nameof(transformer));
bool successful;
T oldValue = Volatile.Read(ref location);
do
{
T newValue = transformer(oldValue, transformerArgument);
if (ReferenceEquals(oldValue, newValue))
{
// No change was actually required.
return false;
}
T interlockedResult = Interlocked.CompareExchange(ref location, newValue, oldValue);
successful = ReferenceEquals(oldValue, interlockedResult);
oldValue = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
return true;
}
/// <summary>
/// Mutates an immutable array in-place with optimistic locking transaction semantics
/// via a specified transformation function.
/// The transformation is retried as many times as necessary to win the optimistic locking race.
/// </summary>
/// <typeparam name="T">The type of data in the immutable array.</typeparam>
/// <param name="location">
/// The immutable array to be changed.
/// </param>
/// <param name="transformer">
/// A function that produces the new array from the old. This function should be side-effect free,
/// as it may run multiple times when races occur with other threads.</param>
/// <returns>
/// <c>true</c> if the location's value is changed by applying the result of the
/// <paramref name="transformer"/> function;
/// <c>false</c> if the location's value remained the same because the last
/// invocation of <paramref name="transformer"/> returned the existing value.
/// </returns>
public static bool Update<T>(ref ImmutableArray<T> location, Func<ImmutableArray<T>, ImmutableArray<T>> transformer)
{
Requires.NotNull(transformer, nameof(transformer));
bool successful;
T[]? oldArray = Volatile.Read(ref Unsafe.AsRef(in location.array));
do
{
ImmutableArray<T> newImmutableArray = transformer(new ImmutableArray<T>(oldArray));
if (ReferenceEquals(oldArray, newImmutableArray.array))
{
// No change was actually required.
return false;
}
T[]? interlockedResult = Interlocked.CompareExchange(ref Unsafe.AsRef(in location.array), newImmutableArray.array, oldArray);
successful = ReferenceEquals(oldArray, interlockedResult);
oldArray = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
return true;
}
/// <summary>
/// Mutates an immutable array in-place with optimistic locking transaction semantics
/// via a specified transformation function.
/// The transformation is retried as many times as necessary to win the optimistic locking race.
/// </summary>
/// <typeparam name="T">The type of data in the immutable array.</typeparam>
/// <typeparam name="TArg">The type of argument passed to the <paramref name="transformer"/>.</typeparam>
/// <param name="location">
/// The immutable array to be changed.
/// </param>
/// <param name="transformer">
/// A function that produces the new array from the old. This function should be side-effect free,
/// as it may run multiple times when races occur with other threads.</param>
/// <param name="transformerArgument">The argument to pass to <paramref name="transformer"/>.</param>
/// <returns>
/// <c>true</c> if the location's value is changed by applying the result of the
/// <paramref name="transformer"/> function;
/// <c>false</c> if the location's value remained the same because the last
/// invocation of <paramref name="transformer"/> returned the existing value.
/// </returns>
public static bool Update<T, TArg>(ref ImmutableArray<T> location, Func<ImmutableArray<T>, TArg, ImmutableArray<T>> transformer, TArg transformerArgument)
{
Requires.NotNull(transformer, nameof(transformer));
bool successful;
T[]? oldArray = Volatile.Read(ref Unsafe.AsRef(in location.array));
do
{
ImmutableArray<T> newImmutableArray = transformer(new ImmutableArray<T>(oldArray), transformerArgument);
if (ReferenceEquals(oldArray, newImmutableArray.array))
{
// No change was actually required.
return false;
}
T[]? interlockedResult = Interlocked.CompareExchange(ref Unsafe.AsRef(in location.array), newImmutableArray.array, oldArray);
successful = ReferenceEquals(oldArray, interlockedResult);
oldArray = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
return true;
}
#region ImmutableArray<T> members
/// <summary>
/// Assigns a field or variable containing an immutable array to the specified value and returns the previous value.
/// </summary>
/// <typeparam name="T">The type of element stored by the array.</typeparam>
/// <param name="location">The field or local variable to change.</param>
/// <param name="value">The new value to assign.</param>
/// <returns>The prior value at the specified <paramref name="location"/>.</returns>
public static ImmutableArray<T> InterlockedExchange<T>(ref ImmutableArray<T> location, ImmutableArray<T> value)
{
return new ImmutableArray<T>(Interlocked.Exchange(ref Unsafe.AsRef(in location.array), value.array));
}
/// <summary>
/// Assigns a field or variable containing an immutable array to the specified value
/// if it is currently equal to another specified value. Returns the previous value.
/// </summary>
/// <typeparam name="T">The type of element stored by the array.</typeparam>
/// <param name="location">The field or local variable to change.</param>
/// <param name="value">The new value to assign.</param>
/// <param name="comparand">The value to check equality for before assigning.</param>
/// <returns>The prior value at the specified <paramref name="location"/>.</returns>
public static ImmutableArray<T> InterlockedCompareExchange<T>(ref ImmutableArray<T> location, ImmutableArray<T> value, ImmutableArray<T> comparand)
{
return new ImmutableArray<T>(Interlocked.CompareExchange(ref Unsafe.AsRef(in location.array), value.array, comparand.array));
}
/// <summary>
/// Assigns a field or variable containing an immutable array to the specified value
/// if it is has not yet been initialized.
/// </summary>
/// <typeparam name="T">The type of element stored by the array.</typeparam>
/// <param name="location">The field or local variable to change.</param>
/// <param name="value">The new value to assign.</param>
/// <returns>True if the field was assigned the specified value; <c>false</c> if it was previously initialized.</returns>
public static bool InterlockedInitialize<T>(ref ImmutableArray<T> location, ImmutableArray<T> value)
{
return InterlockedCompareExchange(ref location, value, default(ImmutableArray<T>)).IsDefault;
}
#endregion
#region ImmutableDictionary<TKey, TValue> members
/// <summary>
/// Obtains the value for the specified key from a dictionary, or adds a new value to the dictionary where the key did not previously exist.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <typeparam name="TArg">The type of argument supplied to the value factory.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key for the value to retrieve or add.</param>
/// <param name="valueFactory">The function to execute to obtain the value to insert into the dictionary if the key is not found.</param>
/// <param name="factoryArgument">The argument to pass to the value factory.</param>
/// <returns>The value obtained from the dictionary or <paramref name="valueFactory"/> if it was not present.</returns>
public static TValue GetOrAdd<TKey, TValue, TArg>(ref ImmutableDictionary<TKey, TValue> location, TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument) where TKey : notnull
{
Requires.NotNull(valueFactory, nameof(valueFactory));
ImmutableDictionary<TKey, TValue> map = Volatile.Read(ref location);
Requires.NotNull(map, nameof(location));
TValue value;
if (map.TryGetValue(key, out value!))
{
return value;
}
value = valueFactory(key, factoryArgument);
return GetOrAdd(ref location, key, value);
}
/// <summary>
/// Obtains the value for the specified key from a dictionary, or adds a new value to the dictionary where the key did not previously exist.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key for the value to retrieve or add.</param>
/// <param name="valueFactory">
/// The function to execute to obtain the value to insert into the dictionary if the key is not found.
/// This delegate will not be invoked more than once.
/// </param>
/// <returns>The value obtained from the dictionary or <paramref name="valueFactory"/> if it was not present.</returns>
public static TValue GetOrAdd<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, Func<TKey, TValue> valueFactory) where TKey : notnull
{
Requires.NotNull(valueFactory, nameof(valueFactory));
ImmutableDictionary<TKey, TValue> map = Volatile.Read(ref location);
Requires.NotNull(map, nameof(location));
TValue value;
if (map.TryGetValue(key, out value!))
{
return value;
}
value = valueFactory(key);
return GetOrAdd(ref location, key, value);
}
/// <summary>
/// Obtains the value for the specified key from a dictionary, or adds a new value to the dictionary where the key did not previously exist.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key for the value to retrieve or add.</param>
/// <param name="value">The value to add to the dictionary if one is not already present.</param>
/// <returns>The value obtained from the dictionary or <paramref name="value"/> if it was not present.</returns>
public static TValue GetOrAdd<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, TValue value) where TKey : notnull
{
ImmutableDictionary<TKey, TValue> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
TValue oldValue;
if (priorCollection.TryGetValue(key, out oldValue!))
{
return oldValue;
}
ImmutableDictionary<TKey, TValue> updatedCollection = priorCollection.Add(key, value);
ImmutableDictionary<TKey, TValue> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
// We won the race-condition and have updated the collection.
// Return the value that is in the collection (as of the Interlocked operation).
return value;
}
/// <summary>
/// Obtains the value from a dictionary after having added it or updated an existing entry.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key for the value to add or update.</param>
/// <param name="addValueFactory">The function that receives the key and returns a new value to add to the dictionary when no value previously exists.</param>
/// <param name="updateValueFactory">The function that receives the key and prior value and returns the new value with which to update the dictionary.</param>
/// <returns>The added or updated value.</returns>
public static TValue AddOrUpdate<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory) where TKey : notnull
{
Requires.NotNull(addValueFactory, nameof(addValueFactory));
Requires.NotNull(updateValueFactory, nameof(updateValueFactory));
TValue newValue;
ImmutableDictionary<TKey, TValue> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
TValue oldValue;
if (priorCollection.TryGetValue(key, out oldValue!))
{
newValue = updateValueFactory(key, oldValue);
}
else
{
newValue = addValueFactory(key);
}
ImmutableDictionary<TKey, TValue> updatedCollection = priorCollection.SetItem(key, newValue);
if (object.ReferenceEquals(priorCollection, updatedCollection))
{
return oldValue;
}
ImmutableDictionary<TKey, TValue> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
// We won the race-condition and have updated the collection.
// Return the value that is in the collection (as of the Interlocked operation).
return newValue;
}
/// <summary>
/// Obtains the value from a dictionary after having added it or updated an existing entry.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key for the value to add or update.</param>
/// <param name="addValue">The value to use if no previous value exists.</param>
/// <param name="updateValueFactory">The function that receives the key and prior value and returns the new value with which to update the dictionary.</param>
/// <returns>The added or updated value.</returns>
public static TValue AddOrUpdate<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory) where TKey : notnull
{
Requires.NotNull(updateValueFactory, nameof(updateValueFactory));
TValue newValue;
ImmutableDictionary<TKey, TValue> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
TValue oldValue;
if (priorCollection.TryGetValue(key, out oldValue!))
{
newValue = updateValueFactory(key, oldValue);
}
else
{
newValue = addValue;
}
ImmutableDictionary<TKey, TValue> updatedCollection = priorCollection.SetItem(key, newValue);
if (object.ReferenceEquals(priorCollection, updatedCollection))
{
return oldValue;
}
ImmutableDictionary<TKey, TValue> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
}
while (!successful);
// We won the race-condition and have updated the collection.
// Return the value that is in the collection (as of the Interlocked operation).
return newValue;
}
/// <summary>
/// Adds the specified key and value to the dictionary if no colliding key already exists in the dictionary.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key to add, if is not already defined in the dictionary.</param>
/// <param name="value">The value to add.</param>
/// <returns><c>true</c> if the key was not previously set in the dictionary and the value was set; <c>false</c> otherwise.</returns>
public static bool TryAdd<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, TValue value) where TKey : notnull
{
ImmutableDictionary<TKey, TValue> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
if (priorCollection.ContainsKey(key))
{
return false;
}
ImmutableDictionary<TKey, TValue> updatedCollection = priorCollection.Add(key, value);
ImmutableDictionary<TKey, TValue> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
return true;
}
/// <summary>
/// Sets the specified key to the given value if the key already is set to a specific value.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key to update.</param>
/// <param name="newValue">The new value to set.</param>
/// <param name="comparisonValue">The value that must already be set in the dictionary in order for the update to succeed.</param>
/// <returns><c>true</c> if the key and comparison value were present in the dictionary and the update was made; <c>false</c> otherwise.</returns>
public static bool TryUpdate<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, TValue newValue, TValue comparisonValue) where TKey : notnull
{
EqualityComparer<TValue> valueComparer = EqualityComparer<TValue>.Default;
ImmutableDictionary<TKey, TValue> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
TValue priorValue;
if (!priorCollection.TryGetValue(key, out priorValue!) || !valueComparer.Equals(priorValue, comparisonValue))
{
// The key isn't in the dictionary, or its current value doesn't match what the caller expected.
return false;
}
ImmutableDictionary<TKey, TValue> updatedCollection = priorCollection.SetItem(key, newValue);
ImmutableDictionary<TKey, TValue> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
return true;
}
/// <summary>
/// Removes an entry from the dictionary with the specified key if it is defined and returns its value.
/// </summary>
/// <typeparam name="TKey">The type of key stored by the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value stored by the dictionary.</typeparam>
/// <param name="location">The variable or field to atomically update if the specified <paramref name="key"/> is not in the dictionary.</param>
/// <param name="key">The key to remove.</param>
/// <param name="value">Receives the value from the pre-existing entry, if one exists.</param>
/// <returns><c>true</c> if the key was found and removed; <c>false</c> otherwise.</returns>
public static bool TryRemove<TKey, TValue>(ref ImmutableDictionary<TKey, TValue> location, TKey key, [MaybeNullWhen(false)] out TValue value) where TKey : notnull
{
ImmutableDictionary<TKey, TValue> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
if (!priorCollection.TryGetValue(key, out value!))
{
return false;
}
ImmutableDictionary<TKey, TValue> updatedCollection = priorCollection.Remove(key);
ImmutableDictionary<TKey, TValue> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
return true;
}
#endregion
#region ImmutableStack<T> members
/// <summary>
/// Pops the top element off a stack and returns it to the caller, if the stack is not empty.
/// </summary>
/// <typeparam name="T">The type of elements stored in the stack.</typeparam>
/// <param name="location">The variable or field to atomically update.</param>
/// <param name="value">The value popped from the stack, if it was non-empty.</param>
/// <returns><c>true</c> if an element was removed from the stack; <c>false</c> otherwise.</returns>
public static bool TryPop<T>(ref ImmutableStack<T> location, [MaybeNullWhen(false)] out T value)
{
ImmutableStack<T> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
if (priorCollection.IsEmpty)
{
value = default;
return false;
}
ImmutableStack<T> updatedCollection = priorCollection.Pop(out value);
ImmutableStack<T> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
return true;
}
/// <summary>
/// Pushes a new element onto a stack.
/// </summary>
/// <typeparam name="T">The type of elements stored in the stack.</typeparam>
/// <param name="location">The variable or field to atomically update.</param>
/// <param name="value">The value to push.</param>
public static void Push<T>(ref ImmutableStack<T> location, T value)
{
ImmutableStack<T> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
ImmutableStack<T> updatedCollection = priorCollection.Push(value);
ImmutableStack<T> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
}
#endregion
#region ImmutableQueue<T> members
/// <summary>
/// Atomically removes the element at the head of a queue and returns it to the caller, if the queue is not empty.
/// </summary>
/// <typeparam name="T">The type of element stored in the queue.</typeparam>
/// <param name="location">The variable or field to atomically update.</param>
/// <param name="value">Receives the value from the head of the queue, if the queue is non-empty.</param>
/// <returns><c>true</c> if the queue was not empty and the head element was removed; <c>false</c> otherwise.</returns>
public static bool TryDequeue<T>(ref ImmutableQueue<T> location, [MaybeNullWhen(false)] out T value)
{
ImmutableQueue<T> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
if (priorCollection.IsEmpty)
{
value = default;
return false;
}
ImmutableQueue<T> updatedCollection = priorCollection.Dequeue(out value);
ImmutableQueue<T> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
return true;
}
/// <summary>
/// Atomically enqueues an element to the tail of a queue.
/// </summary>
/// <typeparam name="T">The type of element stored in the queue.</typeparam>
/// <param name="location">The variable or field to atomically update.</param>
/// <param name="value">The value to enqueue.</param>
public static void Enqueue<T>(ref ImmutableQueue<T> location, T value)
{
ImmutableQueue<T> priorCollection = Volatile.Read(ref location);
bool successful;
do
{
Requires.NotNull(priorCollection, nameof(location));
ImmutableQueue<T> updatedCollection = priorCollection.Enqueue(value);
ImmutableQueue<T> interlockedResult = Interlocked.CompareExchange(ref location, updatedCollection, priorCollection);
successful = object.ReferenceEquals(priorCollection, interlockedResult);
priorCollection = interlockedResult; // we already have a volatile read that we can reuse for the next loop
} while (!successful);
}
#endregion
}
}