/// <inheritdoc/> public override bool Enqueue(ref TItem element, HighResolutionTimeSpan timer) { var result = base.Enqueue(ref element, timer); this.OnEnqueueCalled(); return(result); }
public void FromSeconds_ShouldReturnTimeSpanThatRepresentsSpecifiedNumberOfSeconds_WithSingleTickPrecision(double seconds) { // Arrange // Act var actual = HighResolutionTimeSpan.FromSeconds(seconds); // Assert Assert.That(actual.TotalSeconds, Is.EqualTo(seconds).Within(0.0000001)); }
/// <summary> /// Adds the caller's value-type element to the queue if and when the queue has available space /// </summary> /// <param name="element">A large-sized value-type passed by reference that's expensive to pass directly by value</param> /// <param name="timeout"> /// An optional TimeSpan representing the amount of time /// to spend trying to enqueue the element while space remains unavailable. /// If no timeout value is supplied, a default of 10-seconds will be used /// </param> /// <returns>If enqueueing succeeds, the call returns true, otherwise false</returns> public bool Enqueue(ref TItem element, TimeSpan timeout) { Arguments.NotNull(element, nameof(element)); Arguments.IsPositiveAndNotZero(timeout, nameof(timeout)); //----------------------------------------------------------------- // Set a termination time point to prevent from looping forever //----------------------------------------------------------------- var timer = new HighResolutionTimeSpan(timeout); return(this.Enqueue(ref element, timer)); }
public virtual bool Enqueue(ref TItem element, HighResolutionTimeSpan timer) { Arguments.NotNull(element, nameof(element)); // cache this reference so that it can't change on us for (var queueCopy = this.Queue; ;) { this.ThrowIfDisposed(); //------------------------------------------------------------- // If there's available space in the queue, try to enqueue // the user element by taking ownership of an queue slot //------------------------------------------------------------- var head_index = this.QueueHead; // fetch these values in this order var tail_index = this.QueueTail; //------------------------------------------------------------- // For Enqueues, the tail index must be positive, but the // head index can be negative, which we will work around //------------------------------------------------------------- head_index = ToIndexValue(head_index); if (tail_index >= 0) { if (tail_index >= int.MaxValue) { throw new OverflowException(OverflowedMessage); } var used_count = tail_index - head_index; if (used_count < this.Size) { var queue_index = tail_index % this.Size; var lock_value = ToLockValue(tail_index); // By complimenting the tail index value, we effectively take a lock var expected_index = Interlocked.CompareExchange(ref this.queueTail, lock_value, tail_index); if (expected_index == tail_index) { try { queueCopy ![queue_index].Index = tail_index; // set the index for sanity checks
public IEnumerable <SystemExecutionTime> GetSystemsExecutionTime() { if (!_performanceStatisticsStorage.Frames.Any()) { return(Enumerable.Empty <SystemExecutionTime>()); } var avgFrameTimeInSeconds = _performanceStatisticsStorage.Frames.Average(f => f.Time.TotalSeconds); return(_performanceStatisticsStorage.SystemsFrames .Select(systemFrames => { var systemAvgFrameTimeInSeconds = systemFrames.Value.Average(f => f.Time.TotalSeconds); var systemAvgFrameTime = HighResolutionTimeSpan.FromSeconds(systemAvgFrameTimeInSeconds); return new SystemExecutionTime( systemFrames.Key, systemAvgFrameTime, systemAvgFrameTimeInSeconds / avgFrameTimeInSeconds); })); }