protected internal virtual void doWait(QueuedSemaphore sem)
 {
     lock (this)
     {
         if (!sem.Recheck(this))
         {
             try
             {
                 while (waiting)
                 {
                     //TODO: ? System.Threading.Monitor.Wait(this);
                     System.Threading.Monitor.Wait(sem);
                 }
             }
             catch (System.Threading.ThreadInterruptedException ex)
             {
                 if (waiting)
                 {
                     // no notification
                     waiting = false; // invalidate for the signaller
                     throw ex;
                 }
                 else
                 {
                     // thread was interrupted after it was notified
                     Thread.CurrentThread.Interrupt();
                     return;
                 }
             }
         }
     }
 }
                protected internal virtual bool doTimedWait(QueuedSemaphore sem, long msecs)
                {
                    lock (this)
                    {
                        if (sem.Recheck(this) || !waiting)
                        {
                            return(true);
                        }
                        else if (msecs <= 0)
                        {
                            waiting = false;
                            return(false);
                        }
                        else
                        {
                            long waitTime = msecs;
                            long start    = Utils.CurrentTimeMillis;

                            try
                            {
                                for (; ;)
                                {
                                    //TODO: ? System.Threading.Monitor.Wait(this, TimeSpan.FromMilliseconds(waitTime));
                                    System.Threading.Monitor.Wait(sem, TimeSpan.FromMilliseconds(waitTime));
                                    if (!waiting)
                                    {
                                        // definitely signalled
                                        return(true);
                                    }
                                    else
                                    {
                                        waitTime = msecs - (Utils.CurrentTimeMillis - start);
                                        if (waitTime <= 0)
                                        {
                                            //  timed out
                                            waiting = false;
                                            return(false);
                                        }
                                    }
                                }
                            }
                            catch (System.Threading.ThreadInterruptedException ex)
                            {
                                if (waiting)
                                {
                                    // no notification
                                    waiting = false; // invalidate for the signaller
                                    throw ex;
                                }
                                else
                                {
                                    // thread was interrupted after it was notified
                                    Thread.CurrentThread.Interrupt();
                                    return(true);
                                }
                            }
                        }
                    }
                }
 protected internal virtual bool signal(QueuedSemaphore sem)
 {
     lock (this)
     {
         bool signalled = waiting;
         if (signalled)
         {
             waiting = false;
             // TODO: ? System.Threading.Monitor.Pulse(this);
             System.Threading.Monitor.Pulse(sem);
         }
         return(signalled);
     }
 }
 protected internal virtual bool signal(QueuedSemaphore sem)
 {
     lock (this)
     {
         bool signalled = waiting;
         if (signalled)
         {
             waiting = false;
             // TODO: ? System.Threading.Monitor.Pulse(this);
             System.Threading.Monitor.Pulse(sem);
         }
         return signalled;
     }
 }
 protected internal virtual void doWait(QueuedSemaphore sem)
 {
     lock (this)
     {
         if (!sem.Recheck(this))
         {
             try
             {
                 while (waiting)
                 {
                     //TODO: ? System.Threading.Monitor.Wait(this);
                     System.Threading.Monitor.Wait(sem);
                 }
             }
             catch (System.Threading.ThreadInterruptedException ex)
             {
                 if (waiting)
                 {
                     // no notification
                     waiting = false; // invalidate for the signaller
                     throw ex;
                 }
                 else
                 {
                     // thread was interrupted after it was notified
                     Thread.CurrentThread.Interrupt();
                     return;
                 }
             }
         }
     }
 }
                protected internal virtual bool doTimedWait(QueuedSemaphore sem, long msecs)
                {
                    lock (this)
                    {
                        if (sem.Recheck(this) || !waiting)
                            return true;
                        else if (msecs <= 0)
                        {
                            waiting = false;
                            return false;
                        }
                        else
                        {
                            long waitTime = msecs;
                            long start = Utils.CurrentTimeMillis;

                            try
                            {
                                for (; ; )
                                {
                                    //TODO: ? System.Threading.Monitor.Wait(this, TimeSpan.FromMilliseconds(waitTime));
                                    System.Threading.Monitor.Wait(sem, TimeSpan.FromMilliseconds(waitTime));
                                    if (!waiting)
                                        // definitely signalled
                                        return true;
                                    else
                                    {
                                        waitTime = msecs - (Utils.CurrentTimeMillis - start);
                                        if (waitTime <= 0)
                                        {
                                            //  timed out
                                            waiting = false;
                                            return false;
                                        }
                                    }
                                }
                            }
                            catch (System.Threading.ThreadInterruptedException ex)
                            {
                                if (waiting)
                                {
                                    // no notification
                                    waiting = false; // invalidate for the signaller
                                    throw ex;
                                }
                                else
                                {
                                    // thread was interrupted after it was notified
                                    Thread.CurrentThread.Interrupt();
                                    return true;
                                }
                            }
                        }
                    }
                }