internal CancellationTokenRegistration InternalRegister(Action<object> callback, object stateForCallback, SynchronizationContext targetSyncContext, ExecutionContext executionContext) { this.ThrowIfDisposed(); if (!this.IsCancellationRequested) { int index = Thread.CurrentThread.ManagedThreadId % s_nLists; CancellationCallbackInfo element = new CancellationCallbackInfo(callback, stateForCallback, targetSyncContext, executionContext, this); if (this.m_registeredCallbacksLists == null) { SparselyPopulatedArray<CancellationCallbackInfo>[] arrayArray = new SparselyPopulatedArray<CancellationCallbackInfo>[s_nLists]; Interlocked.CompareExchange<SparselyPopulatedArray<CancellationCallbackInfo>[]>(ref this.m_registeredCallbacksLists, arrayArray, null); } if (this.m_registeredCallbacksLists[index] == null) { SparselyPopulatedArray<CancellationCallbackInfo> array = new SparselyPopulatedArray<CancellationCallbackInfo>(4); Interlocked.CompareExchange<SparselyPopulatedArray<CancellationCallbackInfo>>(ref this.m_registeredCallbacksLists[index], array, null); } SparselyPopulatedArrayAddInfo<CancellationCallbackInfo> registrationInfo = this.m_registeredCallbacksLists[index].Add(element); CancellationTokenRegistration registration = new CancellationTokenRegistration(this, element, registrationInfo); if (!this.IsCancellationRequested) { return registration; } if (!registration.TryDeregister()) { this.WaitForCallbackToComplete(element); return new CancellationTokenRegistration(); } } callback(stateForCallback); return new CancellationTokenRegistration(); }
public void Dispose() { if (!this.m_disposed) { if (this.m_linkingRegistrations != null) { foreach (CancellationTokenRegistration registration in this.m_linkingRegistrations) { registration.Dispose(); } this.m_linkingRegistrations = null; } this.m_registeredCallbacksLists = null; if (this.m_kernelEvent != null) { this.m_kernelEvent.Close(); this.m_kernelEvent = null; } this.m_disposed = true; } }
internal CancellationTokenRegistration InternalRegister(Action<object> callback, object stateForCallback, SynchronizationContext targetSyncContext, ExecutionContext executionContext) { this.ThrowIfDisposed(); if (!this.IsCancellationRequested) { int num = Thread.CurrentThread.ManagedThreadId % CancellationTokenSource.s_nLists; CancellationCallbackInfo cancellationCallbackInfo = new CancellationCallbackInfo(callback, stateForCallback, targetSyncContext, executionContext, this); SparselyPopulatedArray<CancellationCallbackInfo>[] array = this.m_registeredCallbacksLists; if (array == null) { SparselyPopulatedArray<CancellationCallbackInfo>[] array2 = new SparselyPopulatedArray<CancellationCallbackInfo>[CancellationTokenSource.s_nLists]; array = Interlocked.CompareExchange<SparselyPopulatedArray<CancellationCallbackInfo>[]>(ref this.m_registeredCallbacksLists, array2, null); if (array == null) { array = array2; } } SparselyPopulatedArray<CancellationCallbackInfo> sparselyPopulatedArray = Volatile.Read<SparselyPopulatedArray<CancellationCallbackInfo>>(ref array[num]); if (sparselyPopulatedArray == null) { SparselyPopulatedArray<CancellationCallbackInfo> value = new SparselyPopulatedArray<CancellationCallbackInfo>(4); Interlocked.CompareExchange<SparselyPopulatedArray<CancellationCallbackInfo>>(ref array[num], value, null); sparselyPopulatedArray = array[num]; } SparselyPopulatedArrayAddInfo<CancellationCallbackInfo> registrationInfo = sparselyPopulatedArray.Add(cancellationCallbackInfo); CancellationTokenRegistration result = new CancellationTokenRegistration(cancellationCallbackInfo, registrationInfo); if (!this.IsCancellationRequested) { return result; } if (!result.TryDeregister()) { return result; } } callback(stateForCallback); return default(CancellationTokenRegistration); }
protected virtual void Dispose(bool disposing) { if (disposing) { if (this.m_disposed) { return; } if (this.m_timer != null) { this.m_timer.Dispose(); } CancellationTokenRegistration[] linkingRegistrations = this.m_linkingRegistrations; if (linkingRegistrations != null) { this.m_linkingRegistrations = null; for (int i = 0; i < linkingRegistrations.Length; i++) { linkingRegistrations[i].Dispose(); } } this.m_registeredCallbacksLists = null; if (this.m_kernelEvent != null) { this.m_kernelEvent.Close(); this.m_kernelEvent = null; } this.m_disposed = true; } }
/// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (disposing && !_disposed) { if (_timer != null) _timer.Dispose(); var linkingRegistrations = _linkingRegistrations; if (linkingRegistrations != null) { _linkingRegistrations = null; for (int i = 0; i < linkingRegistrations.Length; i++) linkingRegistrations[i].Dispose(); } _registeredCallbacksLists = null; if (_kernelEvent != null) { _kernelEvent.Close(); _kernelEvent = null; } _disposed = true; } }
internal CancellationTokenRegistration InternalRegister(Action<object> callback, object stateForCallback, SynchronizationContext targetSyncContext, ExecutionContext executionContext) { ThrowIfDisposed(); if (!IsCancellationRequested) { var index = Thread.CurrentThread.ManagedThreadId % _nLists; var element = new CancellationCallbackInfo(callback, stateForCallback, targetSyncContext, executionContext, this); var registeredCallbacksLists = _registeredCallbacksLists; if (registeredCallbacksLists == null) { var arrayArray2 = new SparselyPopulatedArray<CancellationCallbackInfo>[_nLists]; registeredCallbacksLists = Interlocked.CompareExchange<SparselyPopulatedArray<CancellationCallbackInfo>[]>(ref _registeredCallbacksLists, arrayArray2, null); if (registeredCallbacksLists == null) registeredCallbacksLists = arrayArray2; } var array = Volatile.Read<SparselyPopulatedArray<CancellationCallbackInfo>>(ref registeredCallbacksLists[index]); if (array == null) { var array2 = new SparselyPopulatedArray<CancellationCallbackInfo>(4); Interlocked.CompareExchange<SparselyPopulatedArray<CancellationCallbackInfo>>(ref registeredCallbacksLists[index], array2, null); array = registeredCallbacksLists[index]; } var registrationInfo = array.Add(element); var registration = new CancellationTokenRegistration(element, registrationInfo); if (!IsCancellationRequested) return registration; if (!registration.TryDeregister()) return registration; } callback(stateForCallback); return new CancellationTokenRegistration(); }