protected override void Initialize() { base.Initialize(); // Subscribe to the solution events this.solutionListeners.Add(new SolutionListenerForProjectReferenceUpdate(this)); this.solutionListeners.Add(new SolutionListenerForProjectOpen(this)); this.solutionListeners.Add(new SolutionListenerForBuildDependencyUpdate(this)); this.solutionListeners.Add(new SolutionListenerForProjectEvents(this)); foreach (SolutionListener solutionListener in this.solutionListeners) { solutionListener.Init(); } try { // this block assumes that the ProjectPackage instances will all be initialized on the same thread, // but doesn't assume that only one ProjectPackage instance exists at a time if (Interlocked.Increment(ref _singleFileGeneratorNodeExtenderReferenceCount) == 1) { ObjectExtenders objectExtenders = (ObjectExtenders)GetService(typeof(ObjectExtenders)); _singleFileGeneratorNodeExtenderProvider = new SingleFileGeneratorNodeExtenderProvider(); string extenderCatId = typeof(FileNodeProperties).GUID.ToString("B"); string extenderName = SingleFileGeneratorNodeExtenderProvider.Name; string localizedName = extenderName; _singleFileGeneratorNodeExtenderCookie = objectExtenders.RegisterExtenderProvider(extenderCatId, extenderName, _singleFileGeneratorNodeExtenderProvider, localizedName); } } finally { _initialized = true; } }
protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress) { await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); base.InitializeAsync(cancellationToken, progress); try { // this block assumes that the ProjectPackage instances will all be initialized on the same thread, // but doesn't assume that only one ProjectPackage instance exists at a time if (Interlocked.Increment(ref _singleFileGeneratorNodeExtenderReferenceCount) == 1) { ObjectExtenders objectExtenders = (ObjectExtenders)GetService(typeof(ObjectExtenders)); _singleFileGeneratorNodeExtenderProvider = new SingleFileGeneratorNodeExtenderProvider(); string extenderCatId = typeof(FileNodeProperties).GUID.ToString("B"); string extenderName = SingleFileGeneratorNodeExtenderProvider.Name; string localizedName = extenderName; _singleFileGeneratorNodeExtenderCookie = objectExtenders.RegisterExtenderProvider(extenderCatId, extenderName, _singleFileGeneratorNodeExtenderProvider, localizedName); } } finally { _initialized = true; } }
public override void Add(T element) { int index = Interlocked.Increment(ref _index) - 1; int adjustedIndex = index; int arrayIndex = GetArrayIndex(index + 1); if (arrayIndex > 0) { adjustedIndex -= Counts[arrayIndex - 1]; } if (_array[arrayIndex] == null) { int arrayLength = Sizes[arrayIndex]; Interlocked.CompareExchange(ref _array[arrayIndex], new T[arrayLength], null); } _array[arrayIndex][adjustedIndex] = element; int count = _count; int fuzzyCount = Interlocked.Increment(ref _fuzzyCount); if (fuzzyCount == index + 1) { Interlocked.CompareExchange(ref _count, fuzzyCount, count); } ItemAddedEvent?.Invoke(element, index); }
public override AbstractEdgeMap <T> Put(int key, T value) { if (key >= minIndex && key <= maxIndex) { T existing = Interlocked.Exchange(ref arrayData[key - minIndex], value); if (existing == null && value != null) { Interlocked.Increment(ref size); } else { if (existing != null && value == null) { Interlocked.Decrement(ref size); } } } return(this); }
private void SetCallbacks() { for (int i = 0; i < _pkg.Connections.Count; i++) { int ind = i; _pkg.Connections[i].On(_pkg.Job.CallbackName, (string uid, string time) => { var receiveTimestamp = Util.Timestamp(); var sendTimestamp = Convert.ToInt64(time); //Util.Log($"diff time: {receiveTimestamp - sendTimestamp}"); Counters.CountLatency(sendTimestamp, receiveTimestamp); Interlocked.Increment(ref totalReceivedMsg); if (ind == 0) { Util.Log($"#### echocallback"); } }); } }
/// <summary> /// Executes a for loop in which iterations may run in parallel /// </summary> /// <param name="threadCount">The number of concurrent execution threads to run</param> /// <param name="fromInclusive">The loop will be started at this index</param> /// <param name="toExclusive">The loop will be terminated before this index is reached</param> /// <param name="body">Method body to run for each iteration of the loop</param> public static void For(int threadCount, int fromInclusive, int toExclusive, Action <int> body) { int counter = threadCount; AutoResetEvent threadFinishEvent = new AutoResetEvent(false); Exception exception = null; --fromInclusive; for (int i = 0; i < threadCount; i++) { ThreadPool.QueueUserWorkItem( delegate(object o) { int threadIndex = (int)o; while (exception == null) { int currentIndex = Interlocked.Increment(ref fromInclusive); if (currentIndex >= toExclusive) { break; } try { body(currentIndex); } catch (Exception ex) { exception = ex; break; } } if (Interlocked.Decrement(ref counter) == 0) { threadFinishEvent.Set(); } }, i ); } threadFinishEvent.WaitOne(); if (exception != null) { throw exception; } }
private SynchronizedBlock EnterLock() { this.IsLockTaken = true; SystemInterlocked.Increment(ref this.UseCount); if (this.Owner is null) { // If this operation is trying to acquire this lock while it is free, then inject a scheduling // point to give another enabled operation the chance to race and acquire this lock. this.Resource.Runtime.ScheduleNextOperation(SchedulingPointType.Acquire); } if (this.Owner != null) { var op = this.Resource.Runtime.GetExecutingOperation(); if (this.Owner == op) { // The owner is re-entering the lock. this.LockCountMap[op]++; return(this); } else { // Another op has the lock right now, so add the executing op // to the ready queue and block it. this.WaitQueue.Remove(op); if (!this.ReadyQueue.Contains(op)) { this.ReadyQueue.Add(op); } this.Resource.Wait(); this.LockCountMap.Add(op, 1); return(this); } } // The executing op acquired the lock and can proceed. this.Owner = this.Resource.Runtime.GetExecutingOperation(); this.LockCountMap.Add(this.Owner, 1); return(this); }
/// <summary> /// Executes a series of tasks in parallel /// </summary> /// <param name="threadCount">The number of concurrent execution threads to run</param> /// <param name="actions">A series of method bodies to execute</param> public static void Invoke(int threadCount, params Action[] actions) { int counter = threadCount; AutoResetEvent threadFinishEvent = new AutoResetEvent(false); int index = -1; Exception exception = null; for (int i = 0; i < threadCount; i++) { ThreadPool.QueueUserWorkItem( delegate(object o) { int threadIndex = (int)o; while (exception == null) { int currentIndex = Interlocked.Increment(ref index); if (currentIndex >= actions.Length) { break; } try { actions[currentIndex](); } catch (Exception ex) { exception = ex; break; } } if (Interlocked.Decrement(ref counter) == 0) { threadFinishEvent.Set(); } }, i ); } threadFinishEvent.WaitOne(); if (exception != null) { throw exception; } }
//protected void SetTimers() //{ // TimerPerConnection = new List<System.Timers.Timer>(_pkg.Job.Connections); // DelayPerConnection = new List<TimeSpan>(_pkg.Job.Connections); // for (int i = 0; i < _pkg.Connections.Count; i++) // { // var delay = StartTimeOffsetGenerator.Delay(TimeSpan.FromSeconds(_pkg.Job.Interval)); // DelayPerConnection.Add(delay); // TimerPerConnection.Add(new System.Timers.Timer()); // var ind = i; // var startTime = Util.Timestamp(); // TimerPerConnection[i].AutoReset = true; // TimerPerConnection[i].Elapsed += (sender, e) => // { // // set new interval // TimerPerConnection[ind].Stop(); // TimerPerConnection[ind].Interval = _pkg.Job.Interval * 1000; // TimerPerConnection[ind].Start(); // if (_pkg.SentMassage[ind] >= _pkg.Job.Duration * _pkg.Job.Interval) // { // TimerPerConnection[ind].Stop(); // return; // } // if (ind == 0) // { // Util.Log($"Sending Message"); // } // try // { // _pkg.Connections[ind].SendAsync("echo", $"{GuidEncoder.Encode(Guid.NewGuid())}", $"{Util.Timestamp()}").Wait(); // } // catch (Exception ex) // { // Console.WriteLine($"Failed to send massage: {ex} \n"); // } // Interlocked.Increment(ref totalSentMsg); // _pkg.SentMassage[ind]++; // Counters.IncreseSentMsg(); // }; // } //} private async Task StartSendingMessageAsync(HubConnection connection) { await Task.Delay(StartTimeOffsetGenerator.Delay(TimeSpan.FromSeconds(_pkg.Job.Interval))); using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(_pkg.Job.Duration))) { while (!cts.IsCancellationRequested) { try { await connection.SendAsync("echo", "id", $"{Util.Timestamp()}"); Interlocked.Increment(ref totalSentMsg); } catch { Interlocked.Increment(ref totalErrMsg); } await Task.Delay(TimeSpan.FromSeconds(_pkg.Job.Interval)); } } }
/// <summary> /// Attempt to add "value" to the table, hashed by an embedded string key. If a value having the same key already exists, /// then return the existing value in "newValue". Otherwise, return the newly added value in "newValue". /// /// If the hash table is full, return false. Otherwise, return true. /// </summary> public bool TryAdd(TValue value, out TValue newValue) { int newEntry, entryIndex; string key; int hashCode; // Assume "value" will be added and returned as "newValue" newValue = value; // Extract the key from the value. If it's null, then value is invalid and does not need to be added to table. key = _extractKey(value); if (key == null) { return(true); } // Compute hash code over entire length of key hashCode = ComputeHashCode(key, 0, key.Length); // Assume value is not yet in the hash table, and prepare to add it (if table is full, return false). // Use the entry index returned from Increment, which will never be zero, as zero conflicts with EndOfList. // Although this means that the first entry will never be used, it avoids the need to initialize all // starting buckets to the EndOfList value. newEntry = Interlocked.Increment(ref _numEntries); if (newEntry < 0 || newEntry >= _buckets.Length) { return(false); } _entries[newEntry].Value = value; _entries[newEntry].HashCode = hashCode; // Ensure that all writes to the entry can't be reordered past this barrier (or other threads might see new entry // in list before entry has been initialized!). Thread.MemoryBarrier(); // Loop until a matching entry is found, a new entry is added, or linked list is found to be full entryIndex = 0; while (!FindEntry(hashCode, key, 0, key.Length, ref entryIndex)) { // PUBLISH (buckets slot) // No matching entry found, so add the new entry to the end of the list ("entryIndex" is index of last entry) if (entryIndex == 0) { entryIndex = Interlocked.CompareExchange(ref _buckets[hashCode & (_buckets.Length - 1)], newEntry, EndOfList); } else { entryIndex = Interlocked.CompareExchange(ref _entries[entryIndex].Next, newEntry, EndOfList); } // Return true only if the CompareExchange succeeded (happens when replaced value is EndOfList). // Return false if the linked list turned out to be full because another thread is currently resizing // the hash table. In this case, entries[newEntry] is orphaned (not part of any linked list) and the // Add needs to be performed on the new hash table. Otherwise, keep looping, looking for new end of list. if (entryIndex <= EndOfList) { return(entryIndex == EndOfList); } } // Another thread already added the value while this thread was trying to add, so return that instance instead. // Note that entries[newEntry] will be orphaned (not part of any linked list) in this case newValue = _entries[entryIndex].Value; return(true); }