public void ConcurrentWriteAndAdviseTest() { const int threadsCount = 10; for (int i = 0; i < 200; i++) { using var def = new LifetimeDefinition(); var lifetime = def.Lifetime; var prop = new WriteOnceProperty <int>(); var value1 = new AtomicValue(); var count = 0; var tasks = Enumerable.Range(0, threadsCount).Select(j => Task.Factory.StartNew(() => { Interlocked.Increment(ref count); SpinWait.SpinUntil(() => Memory.VolatileRead(ref count) == threadsCount); // sync threads if (!prop.SetIfEmpty(j)) { return; } if (!value1.SetIfDefault(j)) { Assert.Fail("Value must be written once"); } }, lifetime)).ToArray(); var values = Enumerable.Range(0, i).Select(j => { var localValue = new AtomicValue(); prop.Advise(lifetime, v => { if (!localValue.SetIfDefault(v)) { Assert.Fail("Handled must not be called twice"); } }); return(localValue); }).ToArray(); Assert.IsTrue(Task.WaitAll(tasks, TimeSpan.FromMinutes(1)), "Task.WaitAll(tasks, TimeSpan.FromMinutes(1))"); value1.AssertNonDefault(); if (values.Length != 0) { var value = values.Select(x => x.Value).Distinct().Single(); Assert.AreEqual(value, value1.Value); } Assert.AreEqual(value1.Value, prop.Value); prop.fireInternal(10000); } }
private void ThrowIfUnsupportedAtomicOperation(AtomicValue atomic) { if ((atomic.ArithmeticBasicValueType == ArithmeticBasicValueType.Int64 || atomic.ArithmeticBasicValueType == ArithmeticBasicValueType.UInt64 || atomic.ArithmeticBasicValueType == ArithmeticBasicValueType.Float64) && !TypeGenerator.Capabilities.Int64_Atomics) { throw CLCapabilityContext.GetNotSupportedInt64_AtomicsException(); } }
public MessageBusInteractor(AtomicValue <LockSystem> lockSystem, ILoggerFactory loggerFactory, IObserver <Statement> statementObserver, IObservable <Message> messageObservable) { this.lockSystem = lockSystem; this.logger = loggerFactory.CreateLogger <MessageBusInteractor>(); this.statementObserver = statementObserver; messageObservable.Subscribe(msg => Receive(msg.replyTo, msg.correlationId, msg.text)); }
public DataController( AtomicValue <LockSystem> lockSystem, ILogger <DataController> logger, IObserver <Statement> statementObserver, Id idGenerator) { this.lockSystem = lockSystem; this.logger = logger; this.statementObserver = statementObserver; this.idGenerator = idGenerator; }
public static T MatchWith <T>( this AtomicValue self, Func <Bool, T> a1, Func <EnumValue, T> a2, Func <ReferenceValue, T> a3, Func <String, T> a4, Func <Scalar, T> a6, Func <Vector, T> a5, Func <GlobalReferenceValue, T> a7, Func <Uno.UX.Markup.Size, T> a8, Func <Size2, T> a9, Func <Selector, T> a10) { var t1 = self as Bool; if (t1 != null) { return(a1(t1)); } var t2 = self as EnumValue; if (t2 != null) { return(a2(t2)); } var t3 = self as ReferenceValue; if (t3 != null) { return(a3(t3)); } var t4 = self as String; if (t4 != null) { return(a4(t4)); } var t6 = self as Scalar; if (t6 != null) { return(a6(t6)); } var t7 = self as GlobalReferenceValue; if (t7 != null) { return(a7(t7)); } var t8 = self as Uno.UX.Markup.Size; if (t8 != null) { return(a8(t8)); } var t9 = self as Size2; if (t9 != null) { return(a9(t9)); } var t10 = self as Selector; if (t10 != null) { return(a10(t10)); } return(a5(new Vector(self))); }
/// <summary> /// Compares the current value with the expected value. /// If it matches the value will be updated to <paramref name="newValue" />. /// </summary> /// <param name="expected">The currently expected value.</param> /// <param name="newValue">The new value.</param> /// <returns>Returns a value of true if the current value matches the expected value.</returns> public Boolean CompareAndSet(T expected, T newValue) { lock (this) { if (0 != AtomicValue.CompareTo(expected)) { return(false); } AtomicValue = newValue; return(true); } }
public static Expression GetExpression(this AtomicValue value, Context ctx) { return(value.MatchWith( (Bool v) => new BooleanLiteral(v.Value), (EnumValue v) => new EnumLiteral(v.GetEnumValueName()), (ReferenceValue v) => v.Value.GetExpression(ctx), (String v) => new StringLiteral(v.Value), (Scalar v) => v.GetExpression(), (UXIL.Vector v) => v.GetExpression(), (GlobalReferenceValue grv) => grv.GetExpression(ctx), (Uno.UX.Markup.Size v) => v.GetExpression(), (Size2 v) => v.GetExpression(), (Selector v) => v.GetExpression())); }
private static void SetupAndIncrement(ref AtomicValue<PerformanceCounter> counter, string counterName, long? value = null, bool rawValue = false) { if (Diagnostics.CassandraPerformanceCountersEnabled) { SetupCounter(ref counter, CassandraCountersCategory, counterName); if (value != null) if (rawValue) counter.Value.RawValue = (long) value; else counter.Value.IncrementBy((long) value); else counter.Value.Increment(); } }
/// <summary> /// Create a new cache with a fixed size entry table and @Lock table. /// </summary> /// <param name="tSize">number of entries in the entry hash table.</param> /// <param name="lockCount"> /// number of entries in the <see cref="LockTarget"/> table. This is the maximum /// concurrency rate for creation of new objects through /// <see cref="load(PackFile, long)"/> invocations. /// </param> internal OffsetCache(int tSize, int lockCount) { if (tSize < 1) { throw new ArgumentException("tSize must be >= 1"); } if (lockCount < 1) { throw new ArgumentException("lockCount must be >= 1"); } queue = new Queue(); _tableSize = tSize; _clock = new AtomicValue <long>(1); _table = new AtomicReferenceArray <Entry <V> >(_tableSize); _locks = new LockTarget[lockCount]; for (int i = 0; i < _locks.Length; i++) { _locks[i] = new LockTarget(); } _evictLock = new AutoResetEvent(true); var eb = (int)(_tableSize * .1); if (64 < eb) { eb = 64; } else if (eb < 4) { eb = 4; } if (_tableSize < eb) { eb = _tableSize; } _evictBatch = eb; }
private WindowCache(WindowCacheConfig cfg) : base(TableSize(cfg), LockCount(cfg)) { _maxFiles = cfg.PackedGitOpenFiles; _maxBytes = cfg.PackedGitLimit; _memoryMap = cfg.PackedGitMMAP; _windowSizeShift = Bits(cfg.PackedGitWindowSize); _windowSize = 1 << _windowSizeShift; _openFiles = new AtomicValue <int>(0); _openBytes = new AtomicValue <long>(0); if (_maxFiles < 1) { throw new ArgumentException("Open files must be >= 1"); } if (_maxBytes < _windowSize) { throw new ArgumentException("Window size must be < limit"); } }
internal BoxedValueNode(FileSourceInfo source, string name, IDataType dataType, AtomicValue value, InstanceType type, IEnumerable <RawProperty> rawProperties = null) : base(source, name, dataType, type, rawProperties) { Value = value; }
public AtomicValueSource(AtomicValue value) { Value = value; }
private static void SetupCounter(ref AtomicValue<PerformanceCounter> counter, string counterCategory, string counterName) { if (CategoryReady.TryTake()) SetupCategory(); if (counter.Value == null) lock (counter) if (counter.Value == null) counter.Value = new PerformanceCounter(counterCategory, counterName, false); }