Files
spark/src/client/spark/core/Utility/RefValueContainer.cs
T

36 lines
998 B
C#

namespace mbts.utils {
/// <summary>
/// Facilities multi-threaded read/write operations to a particular <typeparamref name="T"/> reference.
/// </summary>
/// <typeparam name="T">The reference type to be read/written.</typeparam>
class RefValueContainer<T> where T : class {
readonly object _lock = new object();
T _value;
bool _is_dirty;
/// <summary>
/// Returns the current value if it has not been read already, otherwise returns null.
/// </summary>
public T read_new_value() {
lock (_lock) {
if (_is_dirty) {
_is_dirty = false;
return _value;
}
return null;
}
}
public void write_value(T value) {
lock (_lock) {
_is_dirty = true;
_value = value;
}
}
public void mark_dirty() {
lock (_lock)
_is_dirty = true;
}
}
}