Ejemplo n.º 1
0
        private static RegisteredWaitHandle RegisterWaitForSingleObject(  // throws RegisterWaitException
            WaitHandle waitObject,
            WaitOrTimerCallback callBack,
            object?state,
            uint millisecondsTimeOutInterval,
            bool executeOnlyOnce,    // NOTE: we do not allow other options that allow the callback to be queued as an APC
            bool compressStack
            )
        {
            RegisteredWaitHandle registeredWaitHandle = new RegisteredWaitHandle();

            if (callBack != null)
            {
                _ThreadPoolWaitOrTimerCallback callBackHelper = new _ThreadPoolWaitOrTimerCallback(callBack, state, compressStack);
                state = (object)callBackHelper;
                // call SetWaitObject before native call so that waitObject won't be closed before threadpoolmgr registration
                // this could occur if callback were to fire before SetWaitObject does its addref
                registeredWaitHandle.SetWaitObject(waitObject);
                IntPtr nativeRegisteredWaitHandle = RegisterWaitForSingleObjectNative(waitObject,
                                                                                      state,
                                                                                      millisecondsTimeOutInterval,
                                                                                      executeOnlyOnce,
                                                                                      registeredWaitHandle);
                registeredWaitHandle.SetHandle(nativeRegisteredWaitHandle);
            }
            else
            {
                throw new ArgumentNullException(nameof(WaitOrTimerCallback));
            }
            return(registeredWaitHandle);
        }
Ejemplo n.º 2
0
        internal RegisteredWaitHandle(SafeWaitHandle waitHandle, _ThreadPoolWaitOrTimerCallback callbackHelper,
                                      uint millisecondsTimeout, bool repeating)
        {
            _lock = new Lock();

            // Protect the handle from closing while we are waiting on it (VSWhidbey 285642)
            waitHandle.DangerousAddRef();
            _waitHandle = waitHandle;

            _callbackHelper      = callbackHelper;
            _millisecondsTimeout = millisecondsTimeout;
            _repeating           = repeating;

            // Allocate _gcHandle and _tpWait as the last step and make sure they are never leaked
            _gcHandle = GCHandle.Alloc(this);

            _tpWait = Interop.mincore.CreateThreadpoolWait(
                AddrofIntrinsics.AddrOf <Interop.mincore.WaitCallback>(RegisteredWaitCallback), (IntPtr)_gcHandle, IntPtr.Zero);

            if (_tpWait == IntPtr.Zero)
            {
                _gcHandle.Free();
                throw new OutOfMemoryException();
            }
        }
Ejemplo n.º 3
0
 internal RegisteredWaitHandle(WaitHandle waitHandle, _ThreadPoolWaitOrTimerCallback callbackHelper,
                               int millisecondsTimeout, bool repeating)
 {
     Handle            = waitHandle;
     Callback          = callbackHelper;
     TimeoutDurationMs = millisecondsTimeout;
     Repeating         = repeating;
     RestartTimeout(Environment.TickCount);
 }
 internal RegisteredWaitHandle(WaitHandle waitHandle, _ThreadPoolWaitOrTimerCallback callbackHelper,
                               int millisecondsTimeout, bool repeating)
 {
     Handle            = waitHandle.SafeWaitHandle;
     Callback          = callbackHelper;
     TimeoutDurationMs = millisecondsTimeout;
     Repeating         = repeating;
     if (!IsInfiniteTimeout)
     {
         RestartTimeout();
     }
 }
Ejemplo n.º 5
0
 // call back helper
 internal static void PerformWaitOrTimerCallback(_ThreadPoolWaitOrTimerCallback helper, bool timedOut)
 {
     Debug.Assert(helper != null, "Null state passed to PerformWaitOrTimerCallback!");
     // call directly if it is an unsafe call OR EC flow is suppressed
     if (helper._executionContext == null)
     {
         WaitOrTimerCallback callback = helper._waitOrTimerCallback;
         callback(helper._state, timedOut);
     }
     else
     {
         ExecutionContext.Run(helper._executionContext, timedOut ? _ccbt : _ccbf, helper);
     }
 }
        internal static void PerformWaitOrTimerCallback(object state, bool timedOut)
        {
            _ThreadPoolWaitOrTimerCallback callback = (_ThreadPoolWaitOrTimerCallback)state;

            if (callback._executionContext == null)
            {
                callback._waitOrTimerCallback(callback._state, timedOut);
            }
            else if (timedOut)
            {
                ExecutionContext.Run(callback._executionContext.CreateCopy(), _ccbt, callback);
            }
            else
            {
                ExecutionContext.Run(callback._executionContext.CreateCopy(), _ccbf, callback);
            }
        }
Ejemplo n.º 7
0
        private static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce, ref StackCrawlMark stackMark, bool compressStack)
        {
            if (RemotingServices.IsTransparentProxy(waitObject))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WaitOnTransparentProxy"));
            }
            RegisteredWaitHandle registeredWaitHandle = new RegisteredWaitHandle();

            if (callBack != null)
            {
                _ThreadPoolWaitOrTimerCallback threadPoolWaitOrTimerCallback = new _ThreadPoolWaitOrTimerCallback(callBack, state, compressStack, ref stackMark);
                state = threadPoolWaitOrTimerCallback;
                registeredWaitHandle.SetWaitObject(waitObject);
                IntPtr handle = ThreadPool.RegisterWaitForSingleObjectNative(waitObject, state, millisecondsTimeOutInterval, executeOnlyOnce, registeredWaitHandle, ref stackMark, compressStack);
                registeredWaitHandle.SetHandle(handle);
                return(registeredWaitHandle);
            }
            throw new ArgumentNullException("WaitOrTimerCallback");
        }
Ejemplo n.º 8
0
        internal static void PerformWaitOrTimerCallback(object state, bool timedOut)
        {
            _ThreadPoolWaitOrTimerCallback threadPoolWaitOrTimerCallback = (_ThreadPoolWaitOrTimerCallback)state;

            if (threadPoolWaitOrTimerCallback._executionContext == null)
            {
                WaitOrTimerCallback waitOrTimerCallback = threadPoolWaitOrTimerCallback._waitOrTimerCallback;
                waitOrTimerCallback(threadPoolWaitOrTimerCallback._state, timedOut);
                return;
            }
            using (ExecutionContext executionContext = threadPoolWaitOrTimerCallback._executionContext.CreateCopy())
            {
                if (timedOut)
                {
                    ExecutionContext.Run(executionContext, _ThreadPoolWaitOrTimerCallback._ccbt, threadPoolWaitOrTimerCallback, true);
                }
                else
                {
                    ExecutionContext.Run(executionContext, _ThreadPoolWaitOrTimerCallback._ccbf, threadPoolWaitOrTimerCallback, true);
                }
            }
        }
Ejemplo n.º 9
0
        private static RegisteredWaitHandle RegisterWaitForSingleObject(  // throws RegisterWaitException
            WaitHandle waitObject,
            WaitOrTimerCallback callBack,
            Object state,
            uint millisecondsTimeOutInterval,
            bool executeOnlyOnce,                  // NOTE: we do not allow other options that allow the callback to be queued as an APC
            ref StackCrawlMark stackMark,
            bool compressStack
            )
        {
            if (RemotingServices.IsTransparentProxy(waitObject))
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WaitOnTransparentProxy"));
            }
            RegisteredWaitHandle registeredWaitHandle = new RegisteredWaitHandle();

            if (callBack != null)
            {
                _ThreadPoolWaitOrTimerCallback callBackHelper = new _ThreadPoolWaitOrTimerCallback(callBack, state, compressStack, ref stackMark);
                state = (Object)callBackHelper;
                // call SetWaitObject before native call so that waitObject won't be closed before threadpoolmgr registration
                // this could occur if callback were to fire before SetWaitObject does its addref
                registeredWaitHandle.SetWaitObject(waitObject);
                IntPtr nativeRegisteredWaitHandle = RegisterWaitForSingleObjectNative(waitObject,
                                                                                      state,
                                                                                      millisecondsTimeOutInterval,
                                                                                      executeOnlyOnce,
                                                                                      registeredWaitHandle,
                                                                                      ref stackMark,
                                                                                      compressStack);
                registeredWaitHandle.SetHandle(nativeRegisteredWaitHandle);
            }
            else
            {
                throw new ArgumentNullException("WaitOrTimerCallback");
            }
            return(registeredWaitHandle);
        }
Ejemplo n.º 10
0
        internal static void PerformWaitOrTimerCallback(object state, bool timedOut)
        {
            _ThreadPoolWaitOrTimerCallback waitOrTimerCallback = (_ThreadPoolWaitOrTimerCallback)state;

            if (waitOrTimerCallback._executionContext == null)
            {
                waitOrTimerCallback._waitOrTimerCallback(waitOrTimerCallback._state, timedOut);
            }
            else
            {
                using (ExecutionContext copy = waitOrTimerCallback._executionContext.CreateCopy())
                {
                    if (timedOut)
                    {
                        ExecutionContext.Run(copy, _ThreadPoolWaitOrTimerCallback._ccbt, (object)waitOrTimerCallback, true);
                    }
                    else
                    {
                        ExecutionContext.Run(copy, _ThreadPoolWaitOrTimerCallback._ccbf, (object)waitOrTimerCallback, true);
                    }
                }
            }
        }
Ejemplo n.º 11
0
        // call back helper
        static internal void PerformWaitOrTimerCallback(Object state, bool timedOut)
        {
            _ThreadPoolWaitOrTimerCallback helper = (_ThreadPoolWaitOrTimerCallback)state;

            BCLDebug.Assert(helper != null, "Null state passed to PerformWaitOrTimerCallback!");
            // call directly if it is an unsafe call OR EC flow is suppressed
            if (helper._executionContext == null)
            {
                WaitOrTimerCallback callback = helper._waitOrTimerCallback;
                callback(helper._state, timedOut);
            }
            else
            {
                if (timedOut)
                {
                    ExecutionContext.Run(helper._executionContext.CreateCopy(), _ccbt, helper);
                }
                else
                {
                    ExecutionContext.Run(helper._executionContext.CreateCopy(), _ccbf, helper);
                }
            }
        }
Ejemplo n.º 12
0
        private static RegisteredWaitHandle RegisterWaitForSingleObject(
            WaitHandle waitObject,
            WaitOrTimerCallback callBack,
            Object state,
            uint millisecondsTimeOutInterval,
            bool executeOnlyOnce,
            bool flowExecutionContext)
        {
            if (waitObject == null)
            {
                throw new ArgumentNullException(nameof(waitObject));
            }

            if (callBack == null)
            {
                throw new ArgumentNullException(nameof(callBack));
            }

            var callbackHelper       = new _ThreadPoolWaitOrTimerCallback(callBack, state, flowExecutionContext);
            var registeredWaitHandle = new RegisteredWaitHandle(waitObject.SafeWaitHandle, callbackHelper, millisecondsTimeOutInterval, !executeOnlyOnce);

            registeredWaitHandle.RestartWait();
            return(registeredWaitHandle);
        }
 private static RegisteredWaitHandle RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval, bool executeOnlyOnce, ref StackCrawlMark stackMark, bool compressStack)
 {
     if (RemotingServices.IsTransparentProxy(waitObject))
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WaitOnTransparentProxy"));
     }
     RegisteredWaitHandle registeredWaitHandle = new RegisteredWaitHandle();
     if (callBack == null)
     {
         throw new ArgumentNullException("WaitOrTimerCallback");
     }
     _ThreadPoolWaitOrTimerCallback callback = new _ThreadPoolWaitOrTimerCallback(callBack, state, compressStack, ref stackMark);
     state = callback;
     registeredWaitHandle.SetWaitObject(waitObject);
     IntPtr handle = RegisterWaitForSingleObjectNative(waitObject, state, millisecondsTimeOutInterval, executeOnlyOnce, registeredWaitHandle, ref stackMark, compressStack);
     registeredWaitHandle.SetHandle(handle);
     return registeredWaitHandle;
 }
Ejemplo n.º 14
0
 internal RegisteredWaitHandle(WaitHandle waitHandle, _ThreadPoolWaitOrTimerCallback callbackHelper,
                               int millisecondsTimeout, bool repeating)
 {
 }
Ejemplo n.º 15
0
        private static void WaitOrTimerCallback_Context(Object state, bool timedOut)
        {
            _ThreadPoolWaitOrTimerCallback helper = (_ThreadPoolWaitOrTimerCallback)state;

            helper._waitOrTimerCallback(helper._state, timedOut);
        }
Ejemplo n.º 16
0
        private static RegisteredWaitHandle RegisterWaitForSingleObject(  // throws RegisterWaitException
             WaitHandle             waitObject,
             WaitOrTimerCallback    callBack,
             Object                 state,
             uint               millisecondsTimeOutInterval,
             bool               executeOnlyOnce,   // NOTE: we do not allow other options that allow the callback to be queued as an APC
             ref StackCrawlMark stackMark,
             bool               compressStack
             )
        {
            if (RemotingServices.IsTransparentProxy(waitObject))
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_WaitOnTransparentProxy"));
            RegisteredWaitHandle registeredWaitHandle = new RegisteredWaitHandle();

            if (callBack != null)
            {
                _ThreadPoolWaitOrTimerCallback callBackHelper = new _ThreadPoolWaitOrTimerCallback(callBack, state, compressStack, ref stackMark);
                state = (Object)callBackHelper;
                // call SetWaitObject before native call so that waitObject won't be closed before threadpoolmgr registration
                // this could occur if callback were to fire before SetWaitObject does its addref
                registeredWaitHandle.SetWaitObject(waitObject);
                IntPtr nativeRegisteredWaitHandle = RegisterWaitForSingleObjectNative(waitObject,
                                                                               state, 
                                                                               millisecondsTimeOutInterval,
                                                                               executeOnlyOnce,
                                                                               registeredWaitHandle,
                                                                               ref stackMark,
                                                                               compressStack);
                registeredWaitHandle.SetHandle(nativeRegisteredWaitHandle);
            }
            else
            {
                throw new ArgumentNullException("WaitOrTimerCallback");
            }
            return registeredWaitHandle;
        }
Ejemplo n.º 17
0
        // Token: 0x06003CD0 RID: 15568 RVA: 0x000E2C04 File Offset: 0x000E0E04
        private static void WaitOrTimerCallback_Context(object state, bool timedOut)
        {
            _ThreadPoolWaitOrTimerCallback threadPoolWaitOrTimerCallback = (_ThreadPoolWaitOrTimerCallback)state;

            threadPoolWaitOrTimerCallback._waitOrTimerCallback(threadPoolWaitOrTimerCallback._state, timedOut);
        }