Exemple #1
0
        /// <summary>
        /// Attempts to acquire a proscribed quantity of a resource in this resource pool. If the
        /// resource has already been reserved under this resourceRequest, it simply acquires that
        /// resource. If no resource has been reserved, then the best available resource will be
        /// reserved, and then acquired.
        /// </summary>
        /// <param name="resourceRequest">The resource request under which the reservation is to take
        /// place, and describing the resources desired.</param>
        /// <param name="blockAwaitingAcquisition">If true, blocks until resource is acquired.</param>
        /// <returns>true if the acquisition was successful.</returns>
        public bool Acquire(IResourceRequest resourceRequest, bool blockAwaitingAcquisition)
        {
            if (s_diagnostics)
            {
                _Debug.WriteLine(Name + " servicing request to acquire (" + (blockAwaitingAcquisition?"with":"without") + " block) " + resourceRequest.QuantityDesired + " units of " + resourceRequest);
            }

            if (resourceRequest.RequiredResource != null)
            {
                _Debug.Assert(false, "Explicit targeting of resources not yet implemented.");
                // TODO: Explicit targeting of resources and keying of requests not yet implemented.
            }

            if (blockAwaitingAcquisition)
            {
                bool acquired = AcquireWithWait(resourceRequest);
                return(acquired);
            }

            bool ableToReserve = ReserveBestResource(resourceRequest);

            if (ableToReserve)
            {
                lock (resourceRequest.ResourceObtained) {
                    IResource rsc = resourceRequest.ResourceObtained;
                    rsc.Acquire(resourceRequest);
                    resourceRequest.ResourceObtainedFrom = this;
                    ResourceAcquired?.Invoke(resourceRequest, resourceRequest.ResourceObtained);
                }
            }
            return(ableToReserve);
        }
Exemple #2
0
        public void SetInServiceTracked()
        {
            IResource  resource = Resource.Create(Names);
            Simulation sim      = new Simulation();

            resource.OutOfService = resource.Count;
            Task[] tasks = new Task[resource.Count + 1];
            for (int i = 0; i < resource.Count; i++)
            {
                Task nop = new NoOperation(sim);
                tasks[i] = resource.Acquire(nop);
                nop.WaitOnTask(tasks[i]);
            }
            tasks[tasks.Length - 1] = new Process(sim, SetInService, resource);
            sim.Run(tasks);
            Assert.AreEqual(Names.Length, resource.InUse);
        }
Exemple #3
0
        public void SetOutOfServiceAnonymous()
        {
            IResource  resource = Resource.Create(7);
            Simulation sim      = new Simulation();

            resource.OutOfService = 2;
            Task[] tasks = new Task[resource.Count];
            for (int i = 0; i < resource.Count; i++)
            {
                Task nop = new NoOperation(sim);
                tasks[i] = resource.Acquire(nop);
                nop.WaitOnTask(tasks[i]);
            }
            sim.Run(tasks);
            Assert.AreEqual(resource.Count - resource.OutOfService,
                            resource.InUse);
        }
Exemple #4
0
        private IEnumerator <Task> AcquireOneInterrupted(Process p, object data)
        {
            IResource r = (IResource)data;
            Task      t = new InterruptTask(p.Simulation);

            p.WaitOnTask(t, t.Priority + 10);
            Assert.AreEqual(1, t.BlockCount);

            yield return(r.Acquire(p));

            // The InterruptTask should interrupt us here.
            Assert.IsTrue(p.Interrupted);
            yield return(p.Delay(3));

            // After a delay, the interrupt flag should be cleared.
            Assert.IsTrue(!p.Interrupted);
            yield break;
        }
Exemple #5
0
 public ResourceGuard(IResource resource)
 {
     _resource = resource;
     _resource.Acquire();
 }