Esempio n. 1
0
        public void Start()
        {
#if !DISABLE_SECURITY
            // propagate informations from the original thread to the new thread
#if NET_2_0
            if (!ExecutionContext.IsFlowSuppressed())
            {
                ec_to_set = ExecutionContext.Capture();
            }
#else
            // before 2.0 this was only used for security (mostly CAS) so we
            // do this only if the security manager is active
            if (SecurityManager.SecurityEnabled)
            {
                ec_to_set = ExecutionContext.Capture();
            }
#endif
            if (CurrentThread._principal != null)
            {
                _principal = CurrentThread._principal;
            }
#endif

            // Thread_internal creates and starts the new thread,
#if NET_2_1 && !MONOTOUCH && !MICRO_LIB
            if (Thread_internal((ThreadStart)StartSafe) == (IntPtr)0)
#else
            if (Thread_internal(threadstart) == (IntPtr)0)
#endif
            { throw new SystemException("Thread creation failed."); }
        }
        public void Undo()
        {
            if (_thread == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseAFCMultiple"));
            }
            if (Thread.CurrentThread != _thread)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseAFCOtherThread"));
            }

            // An async flow control cannot be undone when a different execution context is applied. The desktop framework
            // mutates the execution context when its state changes, and only changes the instance when an execution context
            // is applied (for instance, through ExecutionContext.Run). The framework prevents a suppressed-flow execution
            // context from being applied by returning null from ExecutionContext.Capture, so the only type of execution
            // context that can be applied is one whose flow is not suppressed. After suppressing flow and changing an async
            // local's value, the desktop framework verifies that a different execution context has not been applied by
            // checking the execution context instance against the one saved from when flow was suppressed. In .NET Core,
            // since the execution context instance will change after changing the async local's value, it verifies that a
            // different execution context has not been applied, by instead ensuring that the current execution context's
            // flow is suppressed.
            if (!ExecutionContext.IsFlowSuppressed())
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_AsyncFlowCtrlCtxMismatch"));
            }
            Contract.EndContractBlock();

            _thread = null;
            ExecutionContext.RestoreFlow();
        }
Esempio n. 3
0
        public static bool QueueUserWorkItem(WaitCallback callBack, object state)
        {
            if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
            {
                return(Microsoft.ThreadPool.QueueUserWorkItem(callBack, state));
            }
            else
            {
                if (callBack == null)
                {
                    throw new ArgumentNullException("callBack");
                }

                if (callBack.IsTransparentProxy())
                {
                    IAsyncResult ar = callBack.BeginInvoke(state, null, null);
                    if (ar == null)
                    {
                        return(false);
                    }
                }
                else
                {
                    AsyncResult ares = new AsyncResult(callBack, state, !ExecutionContext.IsFlowSuppressed());
                    pool_queue(ares);
                }
                return(true);
            }
        }
Esempio n. 4
0
        public static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object state)
        {
            // no stack propagation here (that's why it's unsafe and requires extra security permissions)
            if (!callBack.IsTransparentProxy())
            {
                if (!callBack.HasSingleTarget)
                {
                    throw new Exception("The delegate must have only one target");
                }

                AsyncResult ares = new AsyncResult(callBack, state, false);
                pool_queue(ares);
                return(true);
            }
            try {
                if (!ExecutionContext.IsFlowSuppressed())
                {
                    ExecutionContext.SuppressFlow();                      // on current thread only
                }
                IAsyncResult ar = callBack.BeginInvoke(state, null, null);
                if (ar == null)
                {
                    return(false);
                }
            } finally {
                if (ExecutionContext.IsFlowSuppressed())
                {
                    ExecutionContext.RestoreFlow();
                }
            }
            return(true);
        }
Esempio n. 5
0
 internal _ThreadPoolWaitOrTimerCallback(WaitOrTimerCallback waitOrTimerCallback, object state, bool compressStack, ref StackCrawlMark stackMark)
 {
     this._waitOrTimerCallback = waitOrTimerCallback;
     this._state = state;
     if (compressStack && !ExecutionContext.IsFlowSuppressed())
     {
         this._executionContext = ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
     }
 }
 internal QueueUserWorkItemCallback(WaitCallback waitCallback, object stateObj, bool compressStack, ref StackCrawlMark stackMark)
 {
     this.callback = waitCallback;
     this.state    = stateObj;
     if (compressStack && !ExecutionContext.IsFlowSuppressed())
     {
         this.context = ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
     }
 }
 internal _TimerCallback(TimerCallback timerCallback, object state, ref StackCrawlMark stackMark)
 {
     this._timerCallback = timerCallback;
     this._state         = state;
     if (!ExecutionContext.IsFlowSuppressed())
     {
         this._executionContext = ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.OptimizeDefaultCase | ExecutionContext.CaptureOptions.IgnoreSyncCtx);
     }
 }
Esempio n. 8
0
 internal _TimerCallback(TimerCallback timerCallback, object state, ref StackCrawlMark stackMark)
 {
     this._timerCallback = timerCallback;
     this._state         = state;
     if (!ExecutionContext.IsFlowSuppressed())
     {
         this._executionContext = ExecutionContext.Capture(ref stackMark);
         ExecutionContext.ClearSyncContext(this._executionContext);
     }
 }
Esempio n. 9
0
 internal _ThreadPoolWaitCallback(WaitCallback waitCallback, object state, bool compressStack, ref StackCrawlMark stackMark)
 {
     this._waitCallback = waitCallback;
     this._state        = state;
     if (compressStack && !ExecutionContext.IsFlowSuppressed())
     {
         this._executionContext = ExecutionContext.Capture(ref stackMark);
         ExecutionContext.ClearSyncContext(this._executionContext);
     }
 }
Esempio n. 10
0
 internal _ThreadPoolWaitOrTimerCallback(WaitOrTimerCallback waitOrTimerCallback, Object state, bool compressStack, ref StackCrawlMark stackMark)
 {
     _waitOrTimerCallback = waitOrTimerCallback;
     _state = state;
     if (compressStack && !ExecutionContext.IsFlowSuppressed())
     {
         // capture the exection context
         _executionContext = ExecutionContext.Capture(ref stackMark);
         ExecutionContext.ClearSyncContext(_executionContext);
     }
 }
Esempio n. 11
0
        public static AsyncFlowControl SuppressFlow()
        {
            if (ExecutionContext.IsFlowSuppressed())
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotSupressFlowMultipleTimes"));
            }
            AsyncFlowControl asyncFlowControl = new AsyncFlowControl();

            asyncFlowControl.Setup();
            return(asyncFlowControl);
        }
Esempio n. 12
0
        public void Start()
        {
            // propagate informations from the original thread to the new thread
            if (!ExecutionContext.IsFlowSuppressed())
            {
                ec_to_set = ExecutionContext.Capture();
            }
            Internal._serialized_principal = CurrentThread.Internal._serialized_principal;

            // Thread_internal creates and starts the new thread,
            if (Thread_internal((ThreadStart)StartInternal) == (IntPtr)0)
            {
                throw new SystemException("Thread creation failed.");
            }
        }
 internal TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
 {
     this.m_timerCallback = timerCallback;
     this.m_state         = state;
     this.m_dueTime       = uint.MaxValue;
     this.m_period        = uint.MaxValue;
     if (!ExecutionContext.IsFlowSuppressed())
     {
         this.m_executionContext = ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
     }
     if (dueTime != 4294967295U)
     {
         this.Change(dueTime, period);
     }
 }
Esempio n. 14
0
 public Barrier(int participantCount, Action <Barrier> postPhaseAction)
 {
     if ((participantCount < 0) || (participantCount > 0x7fff))
     {
         throw new ArgumentOutOfRangeException("participantCount", participantCount, SR.GetString("Barrier_ctor_ArgumentOutOfRange"));
     }
     this.m_currentTotalCount = participantCount;
     this.m_postPhaseAction   = postPhaseAction;
     this.m_oddEvent          = new ManualResetEventSlim(true);
     this.m_evenEvent         = new ManualResetEventSlim(false);
     if ((postPhaseAction != null) && !ExecutionContext.IsFlowSuppressed())
     {
         this.m_ownerThreadContext = ExecutionContext.Capture();
     }
     this.m_actionCallerID = 0;
 }
Esempio n. 15
0
        public static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object state)
        {
            // no stack propagation here (that's why it's unsafe and requires extra security permissions)
            IAsyncResult ar = null;

            try {
                if (!ExecutionContext.IsFlowSuppressed())
                {
                    ExecutionContext.SuppressFlow();                      // on current thread only
                }
                ar = callBack.BeginInvoke(state, null, null);
            }
            finally {
                if (ExecutionContext.IsFlowSuppressed())
                {
                    ExecutionContext.RestoreFlow();
                }
            }
            return(ar != null);
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Barrier"/> class.
        /// </summary>
        /// <param name="participantCount">The number of participating threads.</param>
        /// <param name="postPhaseAction">The <see cref="T:System.Action`1"/> to be executed after each
        /// phase.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="participantCount"/> is less than 0
        /// or greater than <see cref="T:System.Int32.MaxValue"/>.</exception>
        /// <remarks>
        /// The <paramref name="postPhaseAction"/> delegate will be executed after
        /// all participants have arrived at the barrier in one phase.  The participants
        /// will not be released to the next phase until the postPhaseAction delegate
        /// has completed execution.
        /// </remarks>
        public Barrier(int participantCount, Action <Barrier> postPhaseAction)
        {
            // the count must be non negative value
            if (participantCount < 0 || participantCount > Max_Participants)
            {
                throw new ArgumentOutOfRangeException("participantCount", participantCount, "The participantCount argument must be non-negative and less than or equal to 32767.");
            }
            Thread.VolatileWrite(ref _currentTotalCount, participantCount);
            _postPhaseAction = postPhaseAction;

            //Lazily initialize the events
            _oddEvent  = new ManualResetEventSlim(true);
            _evenEvent = new ManualResetEventSlim(false);

            // Capture the context if the post phase action is not null
            if (postPhaseAction != null && !ExecutionContext.IsFlowSuppressed())
            {
                _ownerThreadContext = ExecutionContext.Capture();
            }

            _actionCallerId = 0;
        }
Esempio n. 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Barrier"/> class.
        /// </summary>
        /// <param name="participantCount">The number of participating threads.</param>
        /// <param name="postPhaseAction">The <see cref="T:System.Action`1"/> to be executed after each
        /// phase.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException"> <paramref name="participantCount"/> is less than 0
        /// or greater than <see cref="T:System.Int32.MaxValue"/>.</exception>
        /// <remarks>
        /// The <paramref name="postPhaseAction"/> delegate will be executed after
        /// all participants have arrived at the barrier in one phase.  The participants
        /// will not be released to the next phase until the postPhaseAction delegate
        /// has completed execution.
        /// </remarks>
        public Barrier(int participantCount, Action <Barrier> postPhaseAction)
        {
            // the count must be non negative value
            if (participantCount < 0 || participantCount > MAX_PARTICIPANTS)
            {
                throw new ArgumentOutOfRangeException("participantCount", participantCount, "Barrier_ctor_ArgumentOutOfRange");
            }
            m_currentTotalCount = (int)participantCount;
            m_postPhaseAction   = postPhaseAction;

            //Lazily initialize the events
            m_oddEvent  = new ManualResetEventSlim(true);
            m_evenEvent = new ManualResetEventSlim(false);

            // Capture the context if the post phase action is not null
            if (postPhaseAction != null && !ExecutionContext.IsFlowSuppressed())
            {
                m_ownerThreadContext = ExecutionContext.Capture();
            }

            m_actionCallerID = 0;
        }
Esempio n. 18
0
        public static bool UnsafeQueueUserWorkItem(WaitCallback callBack, object state)
        {
            if (Microsoft.ThreadPool.UseMicrosoftThreadPool)
            {
                return(Microsoft.ThreadPool.UnsafeQueueUserWorkItem(callBack, state));
            }
            else
            {
                if (callBack == null)
                {
                    throw new ArgumentNullException("callBack");
                }

                // no stack propagation here (that's why it's unsafe and requires extra security permissions)
                if (!callBack.IsTransparentProxy())
                {
                    AsyncResult ares = new AsyncResult(callBack, state, false);
                    pool_queue(ares);
                    return(true);
                }
                try {
                    if (!ExecutionContext.IsFlowSuppressed())
                    {
                        ExecutionContext.SuppressFlow();                          // on current thread only
                    }
                    IAsyncResult ar = callBack.BeginInvoke(state, null, null);
                    if (ar == null)
                    {
                        return(false);
                    }
                } finally {
                    if (ExecutionContext.IsFlowSuppressed())
                    {
                        ExecutionContext.RestoreFlow();
                    }
                }
                return(true);
            }
        }
Esempio n. 19
0
        internal TimerQueueTimer(TimerCallback timerCallback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
        {
            m_timerCallback = timerCallback;
            m_state         = state;
            m_dueTime       = Timeout.UnsignedInfinite;
            m_period        = Timeout.UnsignedInfinite;

            if (!ExecutionContext.IsFlowSuppressed())
            {
                m_executionContext = ExecutionContext.Capture(
                    ref stackMark,
                    ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
            }

            //
            // After the following statement, the timer may fire.  No more manipulation of timer state outside of
            // the lock is permitted beyond this point!
            //
            if (dueTime != Timeout.UnsignedInfinite)
            {
                Change(dueTime, period);
            }
        }