private void Tick()
        {
            Debug.Print("N1: Entrando a Tick()...\n" +
                        "Fase de h: " + h.Fase + "\n");

            //aumentar ciclos
            h.Ciclos++;

            //tabla
            if (h.Fase == Hilillo.FaseDeHilillo.V)
            {
                lock (ColaHilillos)
                {
                    if (ColaHilillos.Count != 0)
                    {
                        h         = ColaHilillos.Dequeue();
                        h.Fase    = Hilillo.FaseDeHilillo.L;
                        h.Quantum = this.Quantum;
                        Terminado = false;
                    }
                    else
                    {
                        Terminado = true;
                    }
                }
            }
            else if (h.Fase == Hilillo.FaseDeHilillo.Exec)
            {
                h.Quantum--; //reducir quantum

                if (h.Quantum == 0)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h);
                        h         = ColaHilillos.Dequeue();
                        h.Quantum = this.Quantum;
                    }
                }

                h.Fase = Hilillo.FaseDeHilillo.L;
            }
            else if (h.Fase == Hilillo.FaseDeHilillo.Fin)
            {
                lock (HilillosFinalizados)
                {
                    HilillosFinalizados.Add(h);
                }
                lock (ColaHilillos)
                {
                    if (ColaHilillos.Count == 0)
                    {
                        h = Hilillo.HililloVacio;
                    }
                    else
                    {
                        h         = ColaHilillos.Dequeue();
                        h.Fase    = Hilillo.FaseDeHilillo.L;
                        h.Quantum = this.Quantum;
                    }
                }
            }

            //barrera
            Barrera.SignalAndWait();
        }
Esempio n. 2
0
        private void Tick()
        {
            Debug.Print("N0: Entrando a Tick()...\n" +
                        "Fase de h[0]: " + h[0].Fase + "\n" +
                        "Fase de h[1]: " + h[1].Fase + "\n");

            //aumentar ciclos
            h[0].Ciclos++;
            h[1].Ciclos++;

            //reducir quantums
            if (h[0].Fase == Hilillo.FaseDeHilillo.Exec)
            {
                h[0].Quantum--;
            }
            if (h[1].Fase == Hilillo.FaseDeHilillo.Exec)
            {
                h[1].Quantum--;
            }

            //tabla
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.V && h[0].Fase == Hilillo.FaseDeHilillo.L));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.L && h[0].Fase == Hilillo.FaseDeHilillo.V));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.L && h[0].Fase == Hilillo.FaseDeHilillo.L));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.FI && h[0].Fase == Hilillo.FaseDeHilillo.FI));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.IR && h[0].Fase == Hilillo.FaseDeHilillo.IR));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.FD && h[0].Fase == Hilillo.FaseDeHilillo.L));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.FD && h[0].Fase == Hilillo.FaseDeHilillo.FD));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum > 0 && h[0].Fase == Hilillo.FaseDeHilillo.L));
            Debug.Assert(!(h[1].Quantum == 0 && h[0].Fase == Hilillo.FaseDeHilillo.L));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.Fin && h[0].Fase == Hilillo.FaseDeHilillo.L));
            Debug.Assert(!(h[1].Fase == Hilillo.FaseDeHilillo.Fin && h[0].Fase == Hilillo.FaseDeHilillo.Fin));
            if ((h[0].Fase == Hilillo.FaseDeHilillo.FI ||
                 h[0].Fase == Hilillo.FaseDeHilillo.FD) &&
                h[1].Fase == Hilillo.FaseDeHilillo.V)
            { // H0: V && H1: FI|FD
                lock (ColaHilillos)
                {
                    if (ColaHilillos.Count != 0)
                    {
                        h[1]         = ColaHilillos.Dequeue();
                        h[1].Fase    = Hilillo.FaseDeHilillo.L;
                        h[1].Quantum = this.Quantum;
                    }
                }
            }
            else if (h[0].Fase == Hilillo.FaseDeHilillo.Exec)
            { // H0: Exec - Mayor riesgo de fracasar
                if (h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum > 0)
                {
                    h[1].Fase = Hilillo.FaseDeHilillo.L;
                }
                else if (h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum == 0)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h[1]);
                    }
                    h[1] = Hilillo.HililloVacio;
                }
                else if (h[1].Fase == Hilillo.FaseDeHilillo.Fin)
                {
                    lock (HilillosFinalizados)
                    {
                        HilillosFinalizados.Add(h[1]);
                    }
                    h[1] = Hilillo.HililloVacio;
                }

                //solo cuando q==0
                if (h[0].Quantum == 0)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h[0]);
                        h[0] = ColaHilillos.Dequeue();
                    }
                    h[0].Quantum = this.Quantum;
                }

                //común
                h[0].Fase = Hilillo.FaseDeHilillo.L;
            }
            else if (h[0].Fase == Hilillo.FaseDeHilillo.Fin)
            { // H0: Fin - Riesgo de fracasar
                lock (HilillosFinalizados)
                {
                    HilillosFinalizados.Add(h[0]);
                }

                if (h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum > 0)
                {
                    h[1].Fase = Hilillo.FaseDeHilillo.L;
                }
                else if (h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum == 0)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h[1]);
                        h[0]         = ColaHilillos.Dequeue();
                        h[0].Fase    = Hilillo.FaseDeHilillo.L;
                        h[0].Quantum = this.Quantum;
                    }
                    h[1] = Hilillo.HililloVacio;
                    goto salida;
                }

                //común
                lock (ColaHilillos)
                {
                    if (ColaHilillos.Count == 0)
                    {
                        h[0] = Hilillo.HililloVacio;
                    }
                    else
                    {
                        h[0]         = ColaHilillos.Dequeue();
                        h[0].Fase    = Hilillo.FaseDeHilillo.L;
                        h[0].Quantum = this.Quantum;
                    }
                }
            }
            else if (h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum > 0)
            { //H1: Exec, q > 0 && H0: V|L|FI|IR|FD
                h[1].Fase = Hilillo.FaseDeHilillo.L;
            }
            else if (h[1].Fase == Hilillo.FaseDeHilillo.Exec && h[1].Quantum == 0)
            { //H1: Exec, q == 0 && H0: V|L|FI|IR|FD
                if (h[0].Fase == Hilillo.FaseDeHilillo.V)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h[1]);
                        h[1]         = Hilillo.HililloVacio;
                        h[0]         = ColaHilillos.Dequeue();
                        h[0].Fase    = Hilillo.FaseDeHilillo.L;
                        h[0].Quantum = this.Quantum;
                    }
                }
                else if (h[0].Fase == Hilillo.FaseDeHilillo.L ||
                         h[0].Fase == Hilillo.FaseDeHilillo.IR)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h[1]);
                    }
                    h[1] = Hilillo.HililloVacio;
                }
                else if (h[0].Fase == Hilillo.FaseDeHilillo.FI ||
                         h[0].Fase == Hilillo.FaseDeHilillo.FD)
                {
                    lock (ColaHilillos)
                    {
                        ColaHilillos.Enqueue(h[1]);
                        h[1] = ColaHilillos.Dequeue();
                    }
                    h[1].Fase    = Hilillo.FaseDeHilillo.L;
                    h[1].Quantum = this.Quantum;
                }
            }
            else if (h[1].Fase == Hilillo.FaseDeHilillo.Fin)
            { //H1: Fin && H0: V|L|FI|IR|FD
                lock (HilillosFinalizados)
                {
                    HilillosFinalizados.Add(h[1]);
                }

                if (h[0].Fase == Hilillo.FaseDeHilillo.V)
                {
                    lock (ColaHilillos)
                    {
                        if (ColaHilillos.Count == 0)
                        {
                            h[0] = Hilillo.HililloVacio;
                        }
                        else
                        {
                            h[0]         = ColaHilillos.Dequeue();
                            h[0].Fase    = Hilillo.FaseDeHilillo.L;
                            h[0].Quantum = this.Quantum;
                        }
                    }
                    h[1] = Hilillo.HililloVacio;
                }
                if (h[0].Fase == Hilillo.FaseDeHilillo.IR)
                {
                    h[1] = Hilillo.HililloVacio;
                }
                else if (h[0].Fase == Hilillo.FaseDeHilillo.FI ||
                         h[0].Fase == Hilillo.FaseDeHilillo.FD)
                {
                    lock (ColaHilillos)
                    {
                        if (ColaHilillos.Count == 0)
                        {
                            h[1] = Hilillo.HililloVacio;
                        }
                        else
                        {
                            h[1]         = ColaHilillos.Dequeue();
                            h[1].Fase    = Hilillo.FaseDeHilillo.L;
                            h[1].Quantum = this.Quantum;
                        }
                    }
                }
            }
            else if (h[0].Fase == Hilillo.FaseDeHilillo.V && h[1].Fase == Hilillo.FaseDeHilillo.V)
            {
                Terminado = true;
            }

salida:

            //barrera
            Barrera.SignalAndWait();
        }