Exemple #1
0
 public Promise(bool done)
 {
     _exception = null;
     _hashCode = base.GetHashCode();
     if (!done)
     {
         _waitHandle = new ManualResetEventSlim(false);
     }
 }
Exemple #2
0
 internal Work(Action action, bool exclusive, WorkContext context)
 {
     if (ReferenceEquals(context, null))
     {
         throw new ArgumentNullException("context");
     }
     else
     {
         _context = context;
         _action = action ?? ActionHelper.GetNoopAction();
         _exclusive = exclusive;
         _waitHandle = new ManualResetEventSlim(false);
     }
 }
Exemple #3
0
        private bool RegisterForAutoRemoveDeadItemsExtracted()
        {
            var          result = false;
            EventHandler eventHandler;

            if (ReferenceEquals(_eventHandler.Value, null))
            {
                eventHandler  = GarbageCollected;
                _eventHandler = new WeakNeedle <EventHandler>(eventHandler);
                result        = true;
            }
            else
            {
                eventHandler = _eventHandler.Value.Value;
                if (!_eventHandler.IsAlive)
                {
                    eventHandler        = GarbageCollected;
                    _eventHandler.Value = eventHandler;
                    result = true;
                }
            }
            GCMonitor.Collected += eventHandler;
            return(result);
        }
Exemple #4
0
        private bool RegisterForAutoRemoveDeadItemsExtracted()
        {
            // No risk of dead needles here
            bool         result = false;
            EventHandler eventHandler;

            if (ReferenceEquals(_eventHandler.Value, null))
            {
                eventHandler  = GarbageCollected;
                _eventHandler = new WeakNeedle <EventHandler>(eventHandler);
                result        = true;
            }
            else
            {
                if (_eventHandler.Value.TryGetValue(out eventHandler))
                {
                    eventHandler        = GarbageCollected;
                    _eventHandler.Value = eventHandler;
                    result = true;
                }
            }
            GCMonitor.Collected += eventHandler;
            return(result);
        }
 /// <summary>
 /// Creates a <see cref="TaskCompletionSource{TResult}"/> with
 /// the specified state and options.
 /// </summary>
 /// <param name="creationOptions">The options to use when creating the underlying
 /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>.</param>
 /// <param name="state">The state to use as the underlying
 /// <see cref="T:System.Threading.Tasks.Task{TResult}"/>'s AsyncState.</param>
 /// <exception cref="T:System.ArgumentOutOfRangeException">
 /// The <paramref name="creationOptions"/> represent options invalid for use
 /// with a <see cref="TaskCompletionSource{TResult}"/>.
 /// </exception>
 public TaskCompletionSource(object state, TaskCreationOptions creationOptions)
 {
     _task = new Task <TResult>(state, creationOptions);
 }
 /// <summary>
 /// Creates a <see cref="TaskCompletionSource{TResult}"/>.
 /// </summary>
 public TaskCompletionSource()
 {
     _task = new Task <TResult>();
 }
Exemple #7
0
 public Promise(Exception exception)
 {
     _exception = exception;
     _hashCode = exception.GetHashCode();
     _waitHandle = new ManualResetEventSlim(true);
 }
 public ObservableCollection(List <T> list)
     : base(new List <T>(list))
 {
     _entryCheck = new TrackingThreadLocal <int>(() => 0);
 }
 public ObservableCollection(IEnumerable <T> collection)
     : base(new List <T>(collection))
 {
     _entryCheck = new TrackingThreadLocal <int>(() => 0);
 }
 public ObservableCollection()
     : base(new List <T>())
 {
     _entryCheck      = new TrackingThreadLocal <int>(() => 0);
     _reentryBlockage = new ReentryBlockage(() => _entryCheck.Value.Value--);
 }
Exemple #11
0
 /// <summary>
 /// An internal constructor used by the factory methods on task and its descendent(s).
 /// This variant does not capture the ExecutionContext; it is up to the caller to do that.
 /// </summary>
 /// <param name="action">An action to execute.</param>
 /// <param name="state">Optional state to pass to the action.</param>
 /// <param name="parent">Parent of Task.</param>
 /// <param name="cancellationToken">A CancellationToken for the task.</param>
 /// <param name="scheduler">A task scheduler under which the task will run.</param>
 /// <param name="creationOptions">Options to control its execution.</param>
 /// <param name="internalOptions">Internal options to control its execution</param>
 internal Task(Delegate action, object state, Task parent, CancellationToken cancellationToken, TaskCreationOptions creationOptions, InternalTaskOptions internalOptions, TaskScheduler scheduler)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     if (ReferenceEquals(scheduler, null))
     {
         throw new ArgumentNullException("scheduler");
     }
     Contract.EndContractBlock();
     // This is readonly, and so must be set in the constructor
     // Keep a link to your parent if: (A) You are attached, or (B) you are self-replicating.
     if
     (
         ((creationOptions & TaskCreationOptions.AttachedToParent) != 0) ||
         ((internalOptions & InternalTaskOptions.SelfReplicating) != 0)
     )
     {
         _parent = parent;
     }
     _id     = Interlocked.Increment(ref _lastId) - 1;
     _status = (int)TaskStatus.Created;
     if
     (
         _parent != null &&
         ((creationOptions & TaskCreationOptions.AttachedToParent) != 0) &&
         ((_parent.CreationOptions & TaskCreationOptions.DenyChildAttach) == 0)
     )
     {
         _parent.AddNewChild();
     }
     Action = action;
     State  = state;
     ExecutingTaskScheduler = scheduler;
     _waitHandle            = new ManualResetEventSlim(false);
     if ((creationOptions &
          ~(TaskCreationOptions.AttachedToParent |
            TaskCreationOptions.LongRunning |
            TaskCreationOptions.DenyChildAttach |
            TaskCreationOptions.HideScheduler |
            TaskCreationOptions.PreferFairness |
            TaskCreationOptions.RunContinuationsAsynchronously)) != 0)
     {
         throw new ArgumentOutOfRangeException("creationOptions");
     }
     // Throw exception if the user specifies both LongRunning and SelfReplicating
     if (((creationOptions & TaskCreationOptions.LongRunning) != 0) &&
         ((internalOptions & InternalTaskOptions.SelfReplicating) != 0))
     {
         throw new InvalidOperationException("An attempt was made to create a LongRunning SelfReplicating task.");
     }
     if ((internalOptions & InternalTaskOptions.ContinuationTask) != 0)
     {
         // For continuation tasks or TaskCompletionSource.Tasks, begin life in the
         // WaitingForActivation state rather than the Created state.
         _status = (int)TaskStatus.WaitingForActivation;
     }
     _creationOptions = creationOptions;
     _internalOptions = internalOptions;
     // if we have a non-null cancellationToken, allocate the contingent properties to save it
     // we need to do this as the very last thing in the construction path, because the CT registration could modify m_stateFlags
     if (cancellationToken.CanBeCanceled)
     {
         AssignCancellationToken(cancellationToken, null, null);
     }
 }
Exemple #12
0
 public TaskCompletionSource(object?state, TaskCreationOptions creationOptions)
 {
     _task = new StructNeedle <Task>(new Task(creationOptions, state));
 }
Exemple #13
0
 /// <summary>
 /// An internal constructor used by the factory methods on task and its descendent(s).
 /// This variant does not capture the ExecutionContext; it is up to the caller to do that.
 /// </summary>
 /// <param name="action">An action to execute.</param>
 /// <param name="state">Optional state to pass to the action.</param>
 /// <param name="parent">Parent of Task.</param>
 /// <param name="cancellationToken">A CancellationToken for the task.</param>
 /// <param name="scheduler">A task scheduler under which the task will run.</param>
 /// <param name="creationOptions">Options to control its execution.</param>
 /// <param name="internalOptions">Internal options to control its execution</param>
 internal Task(Delegate action, object state, Task parent, CancellationToken cancellationToken, TaskCreationOptions creationOptions, InternalTaskOptions internalOptions, TaskScheduler scheduler)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     if (ReferenceEquals(scheduler, null))
     {
         throw new ArgumentNullException("scheduler");
     }
     Contract.EndContractBlock();
     // This is readonly, and so must be set in the constructor
     // Keep a link to your parent if: (A) You are attached, or (B) you are self-replicating.
     if
         (
         ((creationOptions & TaskCreationOptions.AttachedToParent) != 0)
         || ((internalOptions & InternalTaskOptions.SelfReplicating) != 0)
         )
     {
         _parent = parent;
     }
     _id = Interlocked.Increment(ref _lastId) - 1;
     _status = (int)TaskStatus.Created;
     if
     (
         _parent != null
         && ((creationOptions & TaskCreationOptions.AttachedToParent) != 0)
         && ((_parent.CreationOptions & TaskCreationOptions.DenyChildAttach) == 0)
     )
     {
         _parent.AddNewChild();
     }
     Action = action;
     State = state;
     Scheduler = scheduler;
     _waitHandle = new ManualResetEventSlim(false);
     if ((creationOptions &
             ~(TaskCreationOptions.AttachedToParent |
               TaskCreationOptions.LongRunning |
               TaskCreationOptions.DenyChildAttach |
               TaskCreationOptions.HideScheduler |
               TaskCreationOptions.PreferFairness |
               TaskCreationOptions.RunContinuationsAsynchronously)) != 0)
     {
         throw new ArgumentOutOfRangeException("creationOptions");
     }
     // Throw exception if the user specifies both LongRunning and SelfReplicating
     if (((creationOptions & TaskCreationOptions.LongRunning) != 0) &&
         ((internalOptions & InternalTaskOptions.SelfReplicating) != 0))
     {
         throw new InvalidOperationException("An attempt was made to create a LongRunning SelfReplicating task.");
     }
     if ((internalOptions & InternalTaskOptions.ContinuationTask) != 0)
     {
         // For continuation tasks or TaskCompletionSource.Tasks, begin life in the
         // WaitingForActivation state rather than the Created state.
         _status = (int)TaskStatus.WaitingForActivation;
     }
     _creationOptions = creationOptions;
     _internalOptions = internalOptions;
     // if we have a non-null cancellationToken, allocate the contingent properties to save it
     // we need to do this as the very last thing in the construction path, because the CT registration could modify m_stateFlags
     if (cancellationToken.CanBeCanceled)
     {
         AssignCancellationToken(cancellationToken, null, null);
     }
 }