// Checks if the specified thread is waiting on a condition variable and interrupts it if it is.
        // This is kludge to get round c# interrupt not being able to interrupt WaitHandles!
        public static void Interrupt(Thread t)
        {
            ConditionVariable v = null;

            if (Waiters.TryGetValue(t, out v))
            {
                if (v != null)
                {
                    try {
                        v.wasInterrupted = true;
                        // This should throw an exception because we don't own the Mutex, but it doesn't?
                        if (v.currentWaitMutex != null)
                        {
                            v.currentWaitMutex.ReleaseMutex();
                        }
                        if (v.currentWaitSemaphore != null)
                        {
                            v.currentWaitSemaphore.Release(1);
                        }
                    } catch (Exception e) {
                        Log.w("ConditionVariable", "Exception interrupting thread:" + e.Message);
                    }
                }
            }
        }
        private void UnregisterWaiter()
        {
            ConditionVariable v = null;

            currentWaitMutex     = null;
            currentWaitSemaphore = null;
            Waiters.TryRemove(Thread.CurrentThread, out v);
        }
Exemple #3
0
 /**
  * Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity.
  *
  * @param capacity the capacity of this deque
  * @throws IllegalArgumentException if {@code capacity} is less than 1
  */
 public LinkedBlockingCollection(int capacity, T defaultValue = default(T))
 {
     if (capacity <= 0)
     {
         throw new System.ArgumentException("Capacity cannot be negative.", "capacity");
     }
     Count       = 0;
     MaxCapacity = capacity;
     Default     = defaultValue;
     AccessLock  = new Mutex();
     NotEmpty    = new ConditionVariable();
     NotFull     = new ConditionVariable();
 }
Exemple #4
0
 public TokenAllocator(int capacity, int defaultValue = -1)
 {
     if (capacity <= 0)
     {
         throw new System.ArgumentException("Capacity cannot be negative.", "capacity");
     }
     MaxCapacity   = capacity;
     Default       = defaultValue;
     AccessLock    = new Mutex();
     NotEmpty      = new ConditionVariable();
     NotFull       = new ConditionVariable();
     ContainerList = new int[capacity];
     Reset();
 }
Exemple #5
0
 public DequeBlockingCollection(int capacity, T defaultValue = default(T))
 {
     if (capacity <= 0)
     {
         throw new System.ArgumentException("Capacity cannot be negative.", "capacity");
     }
     Head          = 0;
     Tail          = 0;
     Count         = 0;
     MaxCapacity   = capacity;
     Default       = defaultValue;
     AccessLock    = new Mutex();
     NotEmpty      = new ConditionVariable();
     NotFull       = new ConditionVariable();
     ContainerList = new T[capacity];
 }
Exemple #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cv"></param>
        /// <param name="cvLock"></param>
        /// <param name="millisecondsTimeout"></param>
        /// <returns></returns>
        /// throws ThreadInterruptedException.
        private int Wait(ConditionVariable cv, Mutex cvLock, int millisecondsTimeout)
        {
            long start = DateTime.UtcNow.Ticks;

            if (cv.WaitCondition(cvLock, millisecondsTimeout))
            {
                if (millisecondsTimeout > 0)
                {
                    long diff = DateTime.UtcNow.Ticks - start;
                    millisecondsTimeout -= (int)(diff / 10000);
                    if (millisecondsTimeout < 0)
                    {
                        millisecondsTimeout = 0;
                    }
                }
            }
            return(millisecondsTimeout);
        }
Exemple #7
0
 public DequeBlockingCollection(DequeBlockingCollection <T> q)
 {
     q.AccessLock.WaitOne();
     try {
         Head          = q.Head;
         Tail          = q.Tail;
         Count         = q.Count;
         MaxCapacity   = q.MaxCapacity;
         Default       = q.Default;
         AccessLock    = new Mutex();
         NotEmpty      = new ConditionVariable();
         NotFull       = new ConditionVariable();
         ContainerList = new T[MaxCapacity];
         for (int k = 0; k < MaxCapacity; k++)
         {
             ContainerList[k] = q.ContainerList[k];
         }
     } finally {
         q.AccessLock.ReleaseMutex();
     }
 }
Exemple #8
0
        // This will keep setting Jint's exit flag to true causing it to exit.
        // Leaves it in a Paused state.
        public void BreakPoller()
        {
            //  ReturnStatement statement = new ReturnStatement((Expression)null);
            int count = 0;

            do
            {
                BreakEvent.WaitOne();
                while (IsRunning)
                {
                    if (count > 5)
                    {
                        count = 0;
                        Shell.Write("Attempting to interrupt shell...");
                        EngineThread.Interrupt();
                        ConditionVariable.Interrupt(EngineThread);
                    }
                    Shell.Write("Attempting (" + count++ + ") to pause shell...");
                    Visitor.ForceBreak();
                    Thread.Sleep(1000);
                }
            } while (!IsClosing);
        }
Exemple #9
0
 public static void Interrupt(Thread t)
 {
     if (t != null)
     {
         int count = 0;
         t.Interrupt();
         ConditionVariable.Interrupt(t);
         while (t.IsAlive)
         {
             try {
                 Thread.Sleep(100);
             } catch (Exception) {
             }
             ++count;
             if (count > 30)
             {
                 count = 0;
                 Log.w(TAG, "Interrupted Thread not stopping:" + t.Name);
             }
             t.Interrupt();
             ConditionVariable.Interrupt(t);
         }
     }
 }
Exemple #10
0
        ConditionVariable Ready; // Indicates when threads are enabled.

        public Gate(bool isOpen = false)
        {
            IsOpen     = isOpen;
            AccessLock = new Mutex();
            Ready      = new ConditionVariable();
        }