Ejemplo n.º 1
0
        public void DoWork()
        {
            long SHORT_DELAY_MS = 300;

            sem.Release();

            //TODO can we do Assert.IsTrue here?
            sem.Attempt(SHORT_DELAY_MS);
            sem.Release();
            sem.Attempt(SHORT_DELAY_MS);
        }
Ejemplo n.º 2
0
        public void AttemptInSameThread()
        {
            Semaphore s = new Semaphore(1);

            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.AreEqual(1, s.Permits);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// <see cref="IPuttable.Offer"/>
 /// </summary>
 public virtual bool Offer(System.Object x, long msecs)
 {
     if (x == null)
     {
         throw new System.ArgumentException();
     }
     Utils.FailFastIfInterrupted();
     if (!putGuard_.Attempt(msecs))
     {
         return(false);
     }
     else
     {
         try
         {
             Insert(x);
             takeGuard_.Release();
             return(true);
         }
         catch (System.InvalidCastException ex)
         {
             putGuard_.Release();
             throw ex;
         }
     }
 }
Ejemplo n.º 4
0
        public void AttemptReleaseInDifferentThreads()
        {
            Semaphore            s       = new Semaphore(0);
            AttemptReleaseWorker worker1 = new AttemptReleaseWorker(s);
            Thread thread1 = new Thread(new ThreadStart(worker1.DoWork));

            thread1.Start();
            //TODO investigate...
            //Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Attempt(SHORT_DELAY_MS);
            s.Release();
            //Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Attempt(SHORT_DELAY_MS);
            s.Release();
            s.Release();
            thread1.Join();
        }
Ejemplo n.º 5
0
 public void AttemptWithNegativeMillisInSameThread()
 {
     Semaphore s = new Semaphore(2);
     Assert.AreEqual(2, s.Permits);
     s.Acquire();
     Assert.IsTrue(s.Attempt(-400));
     s.Release(2);
     Assert.AreEqual(2, s.Permits);            
 }
Ejemplo n.º 6
0
        public void AttemptWithNegativeMillisInSameThread()
        {
            Semaphore s = new Semaphore(2);

            Assert.AreEqual(2, s.Permits);
            s.Acquire();
            Assert.IsTrue(s.Attempt(-400));
            s.Release(2);
            Assert.AreEqual(2, s.Permits);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// <see cref="ITakable.Take"/>
 /// </summary>
 /// <param name="msecs"></param>
 /// <returns></returns>
 public virtual System.Object Poll(long msecs)
 {
     Utils.FailFastIfInterrupted();
     if (!takeGuard_.Attempt(msecs))
     {
         return(null);
     }
     else
     {
         try
         {
             System.Object x = Extract();
             putGuard_.Release();
             return(x);
         }
         catch (System.InvalidCastException ex)
         {
             takeGuard_.Release();
             throw ex;
         }
     }
 }
Ejemplo n.º 8
0
        public void AttemptInSameThread()
        {
            Semaphore s = new Semaphore(1);
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
            s.Release();
            Assert.AreEqual(1, s.Permits);

        }
Ejemplo n.º 9
0
 public void AttemptReleaseInDifferentThreads()
 {
     Semaphore s = new Semaphore(0); 
     AttemptReleaseWorker worker1 = new AttemptReleaseWorker(s);
     Thread thread1 = new Thread(new ThreadStart(worker1.DoWork));
     
     thread1.Start();
     //TODO investigate...
     //Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
     s.Attempt(SHORT_DELAY_MS);
     s.Release();
     //Assert.IsTrue(s.Attempt(SHORT_DELAY_MS));
     s.Attempt(SHORT_DELAY_MS);
     s.Release();
     s.Release();
     thread1.Join();
 }
        public void AnInvalidApplicationIsNotUpdatedNorRemovedButWhenRemovedStillRaisesTheRemovedEvent ()
        {
            Semaphore sync = new Semaphore (0);
            AddApplication();
            InitHandlerAndStartLocation (sync);
            File.Delete(serviceXml);
            Assert.IsFalse(sync.Attempt(1000), "some events propagated, expecting no one");

            // remove
            TestUtils.SafeDeleteDirectory(sampleDir);
            Thread.SpinWait(1);
            Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
            log.Debug("directory deleted");
            sync.Acquire ();
            log.Debug("sync acquired");
            Assert.IsFalse (Directory.Exists (sampleDir), "directory still exists: " + sampleDir);
            Assert.IsTrue (handler.applicationRemoved, "application not removed");
            Assert.AreEqual (0, location.Applications.Count);
        }
        public void AnInvalidApplicationIsNotUpdatedNorRemovedButItWillNotBeListed ()
        {
            Semaphore sync = new Semaphore (0);
            AddApplication();
            InitHandlerAndStartLocation (sync);
            File.Delete(serviceXml);
            Assert.IsFalse(sync.Attempt(1000), "some events propagated, expecting no one");
            Assert.IsFalse (handler.applicationUpdated, "application wrongly updated");
            Assert.IsFalse(handler.applicationRemoved, "application wrongly removed");
            Assert.AreEqual (0, location.Applications.Count, "application listed");

            location.Dispose();            
            location = new FileSystemDeployLocation (deployPath);
            Assert.AreEqual (0, location.Applications.Count, "invalid application listed");
        }
Ejemplo n.º 12
0
        /// <summary>
        /// The actual rendezvous logic for the given time
        /// </summary>
        protected internal virtual System.Object doRendezvous(System.Object x, bool timed, long msecs)
        {
            // rely on semaphore to throw interrupt on entry

            long startTime;

            if (timed)
            {
                startTime = Utils.CurrentTimeMillis;
                if (!entryGate_.Attempt(msecs))
                {
                    throw new TimeoutException(msecs);
                }
            }
            else
            {
                startTime = 0;
                entryGate_.Acquire();
            }

            lock (this)
            {
                System.Object y = null;

                int index = entries_++;
                slots_[index] = x;

                try
                {
                    // last one in runs function and releases
                    if (entries_ == parties_)
                    {
                        departures_ = entries_;
                        System.Threading.Monitor.PulseAll(this);

                        try
                        {
                            if (!broken_ && rendezvousFunction_ != null)
                            {
                                rendezvousFunction_.RendezvousFunction(slots_);
                            }
                        }
                        catch (System.SystemException)
                        {
                            broken_ = true;
                        }
                    }
                    else
                    {
                        while (!broken_ && departures_ < 1)
                        {
                            long timeLeft = 0;
                            if (timed)
                            {
                                timeLeft = msecs - (Utils.CurrentTimeMillis - startTime);
                                if (timeLeft <= 0)
                                {
                                    broken_     = true;
                                    departures_ = entries_;
                                    System.Threading.Monitor.PulseAll(this);
                                    throw new TimeoutException(msecs);
                                }
                            }

                            try
                            {
                                System.Threading.Monitor.Wait(this, TimeSpan.FromMilliseconds(timeLeft));
                            }
                            catch (System.Threading.ThreadInterruptedException ex)
                            {
                                if (broken_ || departures_ > 0)
                                {
                                    // interrupted after release
                                    Thread.CurrentThread.Interrupt();
                                    break;
                                }
                                else
                                {
                                    broken_     = true;
                                    departures_ = entries_;
                                    System.Threading.Monitor.PulseAll(this);
                                    throw ex;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    y = slots_[index];

                    // Last one out cleans up and allows next set of threads in
                    if (--departures_ <= 0)
                    {
                        for (int i = 0; i < slots_.Length; ++i)
                        {
                            slots_[i] = null;
                        }
                        entryGate_.Release(entries_);
                        entries_ = 0;
                    }
                }

                // continue if no IE/TO throw
                if (broken_)
                {
                    throw new BrokenBarrierException(index);
                }
                else
                {
                    return(y);
                }
            }
        }