-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite of the AtomicReference #1615
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,14 @@ | |
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System.Threading; | ||
using Akka.IO; | ||
using Akka.TestKit; | ||
using Akka.Util; | ||
using Xunit; | ||
|
||
namespace Akka.Tests.IO | ||
{ | ||
|
||
public class SimpleDnsCacheSpec | ||
{ | ||
private class SimpleDnsCacheTestDouble : SimpleDnsCache | ||
|
@@ -79,5 +80,96 @@ public void Cache_should_be_updated_with_the_latest_resolved() | |
cache.Put(cacheEntryTwo, ttl); | ||
cache.Cached("test.local").ShouldBe(cacheEntryTwo); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has used the old version with a value type, therefore I have copied the old version into this class. |
||
|
||
//This version was replaced with a new version which is restricted to reference types, | ||
//therefore we use the old version here. | ||
private class AtomicReference<T> | ||
{ | ||
/// <summary> | ||
/// Sets the initial value of this <see cref="AtomicReference{T}"/> to <paramref name="originalValue"/>. | ||
/// </summary> | ||
public AtomicReference(T originalValue) | ||
{ | ||
atomicValue = originalValue; | ||
} | ||
|
||
/// <summary> | ||
/// Default constructor | ||
/// </summary> | ||
public AtomicReference() | ||
{ | ||
atomicValue = default(T); | ||
} | ||
|
||
// ReSharper disable once InconsistentNaming | ||
protected T atomicValue; | ||
|
||
/// <summary> | ||
/// The current value of this <see cref="AtomicReference{T}"/> | ||
/// </summary> | ||
public T Value | ||
{ | ||
get | ||
{ | ||
Interlocked.MemoryBarrier(); | ||
return atomicValue; | ||
} | ||
set | ||
{ | ||
Interlocked.MemoryBarrier(); | ||
atomicValue = value; | ||
Interlocked.MemoryBarrier(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// If <see cref="Value"/> equals <paramref name="expected"/>, then set the Value to | ||
/// <paramref name="newValue"/>. | ||
/// </summary> | ||
/// <returns><c>true</c> if <paramref name="newValue"/> was set</returns> | ||
public bool CompareAndSet(T expected, T newValue) | ||
{ | ||
//special handling for null values | ||
if (Value == null) | ||
{ | ||
if (expected == null) | ||
{ | ||
Value = newValue; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
if (Value.Equals(expected)) | ||
{ | ||
Value = newValue; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
#region Conversion operators | ||
|
||
/// <summary> | ||
/// Implicit conversion operator = automatically casts the <see cref="AtomicReference{T}"/> to an instance of <typeparamref name="T"/>. | ||
/// </summary> | ||
public static implicit operator T(AtomicReference<T> aRef) | ||
{ | ||
return aRef.Value; | ||
} | ||
|
||
/// <summary> | ||
/// Implicit conversion operator = allows us to cast any type directly into a <see cref="AtomicReference{T}"/> instance. | ||
/// </summary> | ||
/// <param name="newValue"></param> | ||
/// <returns></returns> | ||
public static implicit operator AtomicReference<T>(T newValue) | ||
{ | ||
return new AtomicReference<T>(newValue); | ||
} | ||
|
||
#endregion | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -403,15 +403,15 @@ public override void Stop(IActorRef actor) | |
class TerminationCallbacks | ||
{ | ||
private Task _terminationTask; | ||
private AtomicReference<Task> _atomicRef; | ||
private readonly AtomicReference<Task> _atomicRef; | ||
|
||
public TerminationCallbacks(Task upStreamTerminated) | ||
{ | ||
_atomicRef = new AtomicReference<Task>(new Task(() => {})); | ||
|
||
upStreamTerminated.ContinueWith(_ => | ||
{ | ||
_terminationTask = Interlocked.Exchange(ref _atomicRef, new AtomicReference<Task>(null)).Value; | ||
_terminationTask = _atomicRef.GetAndSet(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the original reason for the discussion, in the old version it wasn't possible to implement GetAndSet because the generic version of Interlocked.Exchange is restricted to reference types. |
||
_terminationTask.Start(); | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class has used the old version with a value type, therefore I have copied the old version into this class.