public WaitPanelThread(Point origin, int delay, bool positiveAxis, bool horizontal, Panel panel, Plane plane, Semaphore semaphoreThis, Semaphore semaphoreNext, Semaphore semaphoreTerminal, Buffer bufferPrevious, Buffer bufferNext, Buffer bufferTerminal, int length, int terminalID)
 {
     this.origin = origin;
     this.delay = delay;
     this.positiveAxis = positiveAxis;
     this.panel = panel;
     this.plane = plane;
     this.panel.Paint += new PaintEventHandler(this.panel_Paint);
     if (horizontal)
     {
         this.xDelta = positiveAxis ? +10 : -10;
         this.yDelta = 0;
     }
     else
     {
         this.xDelta = 0;
         this.yDelta = positiveAxis ? +10 : -10;
     }
     this.semaphoreThis = semaphoreThis;
     this.semaphoreNext = semaphoreNext;
     this.semaphoreTerminal = semaphoreTerminal;
     this.bufferPrevious = bufferPrevious;
     this.bufferNext = bufferNext;
     this.bufferTerminal = bufferTerminal;
     this.length = length;
     this.terminalID = terminalID;
 }
        public void Start()
        {
            Thread.Sleep(delay);
            for (int k = 1; k <= 100; k++)
            {
                semaphoreThis.Signal();

                bufferPrevious.Read(ref this.plane);

                this.zeroPlane();

                if (this.plane.getDestination() == 0 && terminalID == 0)
                {
                    for (int i = 1; i <= this.length + 8; i++)
                    {
                        this.movePlane(xDelta, yDelta);
                        panel.Invalidate();
                        Thread.Sleep(delay - (int)(i * 1.8));
                    }
                    this.plane = null;
                    panel.Invalidate();
                }
                else
                {
                    if (terminalID == 0)
                    {
                        for (int i = 1; i <= this.length; i++)
                        {
                            this.movePlane(xDelta, yDelta);
                            panel.Invalidate();
                            Thread.Sleep(15 + (int)(i * 2.0));
                        }
                    }
                    else
                    {
                        for (int i = 1; i < length; i++)
                        {
                            panel.Invalidate();
                            this.movePlane(xDelta, yDelta);
                            Thread.Sleep(delay);
                        }
                    }
                    if (this.plane.getDestination() == terminalID)
                    {
                        semaphoreTerminal.Wait();
                        bufferTerminal.Write(this.plane);
                        this.plane = null;
                        panel.Invalidate();
                    }
                    else
                    {
                        semaphoreNext.Wait();
                        bufferNext.Write(this.plane);
                        this.plane = null;
                        panel.Invalidate();
                    }
                }
            }
        }
Пример #3
0
 public void Write(Plane plane)
 {
     lock (this)
     {
         // Check whether the buffer is full.
         if (!empty)
             Monitor.Wait(this);
         empty = false;
         this.plane = plane;
         Monitor.Pulse(this);
     }
 }
Пример #4
0
 public void Read(ref Plane plane)
 {
     lock (this)
     {
         // Check whether the buffer is empty.
         if (empty)
             Monitor.Wait(this);
         empty = true;
         plane = this.plane;
         Monitor.Pulse(this);
     }
 }
 public ButtonPanelThread(Point origin, Plane plane, int delay, bool positiveAxis, bool horizontal, bool isArrival, Panel panel, Semaphore semaphoreThis, Semaphore semaphoreNext, Buffer bufferIn, Buffer bufferOut, Button btn, Button takeoff, Button terminal1, Button terminal2, Button terminal3, int length)
 {
     this.origin = origin;
     this.delay = delay;
     this.positiveAxis = positiveAxis;
     this.panel = panel;
     this.plane = plane;
     this.panel.Paint += new PaintEventHandler(this.panel_Paint);
     if (horizontal)
     {
         this.xDelta = positiveAxis ? +10 : -10;
         this.yDelta = 0;
     }
     else
     {
         this.xDelta = 0;
         this.yDelta = positiveAxis ? +10 : -10;
     }
     this.semaphoreThis = semaphoreThis;
     this.semaphoreNext = semaphoreNext;
     this.bufferIn = bufferIn;
     this.bufferOut = bufferOut;
     this.btn = btn;
     this.isArrival = isArrival;
     if (isArrival)
     {
         this.takeoff = takeoff;
         this.terminal1 = terminal1;
         this.terminal2 = terminal2;
         this.terminal3 = terminal3;
         this.takeoff.Click += new System.EventHandler(this.takeoff_Click);
         this.terminal1.Click += new System.EventHandler(this.terminal1_Click);
         this.terminal2.Click += new System.EventHandler(this.terminal2_Click);
         this.terminal3.Click += new System.EventHandler(this.terminal3_Click);
     }
     this.takeoffLock = true;
     this.terminal1Lock = true;
     this.terminal2Lock = true;
     this.terminal3Lock = true;
     this.btn.Click += new System.EventHandler(this.btn_Click);
     this.length = length;
 }
        public void Start()
        {
            Thread.Sleep(delay);
            for (int k = 1; k <= 100; k++)
            {
                semaphoreThis.Signal();
                if (this.plane != null)
                {
                    semaphoreThis.Wait();
                }
                if (this.plane == null)
                {
                    bufferIn.Read(ref this.plane);
                    this.movePlaneToBottom();
                    panel.Invalidate();
                    for (int i = 1; i < length; i++)
                    {
                        this.movePlane(xDelta, -yDelta);
                        Thread.Sleep(delay);
                        panel.Invalidate();
                    }
                    this.plane.setDestination(0);

                    locked = true;
                    this.btn.BackColor = locked ? Color.DarkRed : Color.IndianRed;
                    lock (this)
                    {
                        if (!locked)
                            Monitor.Pulse(this);
                    }
                }
                this.zeroPlane();
                panel.Invalidate();
                lock (this)
                {
                    while (locked)
                    {
                        Monitor.Wait(this);
                    }
                }
                for (int i = 1; i < length; i++)
                {
                    this.movePlane(xDelta, yDelta);
                    Thread.Sleep(delay);
                    panel.Invalidate();
                }
                semaphoreNext.Wait();
                bufferOut.Write(this.plane);
                this.plane = null;
                panel.Invalidate();
                if (isArrival)
                {
                    this.locked = true;
                    this.btn.BackColor = locked ? Color.DarkRed : Color.IndianRed;
                    this.plane = new Plane(origin, this.randomColorGenerator(), 0);
                    panel.Invalidate();
                }
            }
        }
        public Form1()
        {
            InitializeComponent();
            planes = new Plane[4];

            planes[0] = new Plane(new Point(0, 0), Color.Red, 0);
            planes[1] = new Plane(new Point(0, 0), Color.Blue, 0);
            planes[2] = new Plane(new Point(0, 0), Color.Green, 0);
            planes[3] = new Plane(new Point(0, 0), Color.Magenta, 1);

            semaphoreTerminal1 = new Semaphore();
            semaphoreTerminal2 = new Semaphore();
            semaphoreTerminal3 = new Semaphore();
            semaphoreArrivals1 = new Semaphore();
            semaphoreTaxiBetween1and2 = new Semaphore();
            semaphoreTaxiBetween2and3 = new Semaphore();
            semaphoreTaxiBetween3andToRunway = new Semaphore();
            semaphoreTaxiToRunway = new Semaphore();
            semaphoreRunway = new Semaphore();
            semaphoreTaxiFromRunway = new Semaphore();
            bufferTerminal1 = new Buffer();
            bufferTerminal2 = new Buffer();
            bufferTerminal3 = new Buffer();
            bufferArrivals1 = new Buffer();
            bufferTaxiBetween1and2 = new Buffer();
            bufferTaxiBetween2and3 = new Buffer();
            bufferTaxiBetween3andToRunway = new Buffer();
            bufferTaxiToRunway = new Buffer();
            bufferRunway = new Buffer();
            bufferTaxiFromRunway = new Buffer();

            terminal1 = new ButtonPanelThread(new Point(10, 40), planes[0], 150, true, false, false, pnlTerminal1, semaphoreTerminal1, semaphoreTaxiBetween1and2, bufferTerminal1, bufferTaxiBetween1and2, btnTerminal1, null, null, null, null, 11);
            terminal2 = new ButtonPanelThread(new Point(10, 40), planes[1], 150, true, false, false, pnlTerminal2, semaphoreTerminal2, semaphoreTaxiBetween2and3, bufferTerminal2, bufferTaxiBetween2and3, btnTerminal2, null, null, null, null, 11);
            terminal3 = new ButtonPanelThread(new Point(10, 40), planes[2], 150, true, false, false, pnlTerminal3, semaphoreTerminal3, semaphoreTaxiBetween3andToRunway, bufferTerminal3, bufferTaxiBetween3andToRunway, btnTerminal3, null, null, null, null, 11);
            arrivals1 = new ButtonPanelThread(new Point(30, 10), planes[3], 150, false, true, true, pnlArrivals1, semaphoreArrivals1, semaphoreRunway, null, bufferRunway, btnArrivals1, btnSendTo0, btnSendTo1, btnSendTo2, btnSendTo3, 4);

            taxiBetween1and2 = new WaitPanelThread(new Point(0, 10), 100, true, true, pnlTaxiBetween1and2, null, semaphoreTaxiBetween1and2, semaphoreTaxiBetween2and3, semaphoreTerminal2, bufferTaxiBetween1and2, bufferTaxiBetween2and3, bufferTerminal2, 13, 2);
            taxiBetween2and3 = new WaitPanelThread(new Point(0, 10), 100, true, true, pnlTaxiBetween2and3, null, semaphoreTaxiBetween2and3, semaphoreTaxiBetween3andToRunway, semaphoreTerminal3, bufferTaxiBetween2and3, bufferTaxiBetween3andToRunway, bufferTerminal3, 13, 3);
            taxiBetween3andToRunway = new WaitPanelThread(new Point(0, 10), 100, true, true, pnlTaxiBetween3andToRunway, null, semaphoreTaxiBetween3andToRunway, semaphoreTaxiToRunway, null, bufferTaxiBetween3andToRunway, bufferTaxiToRunway, null, 13, 4);
            taxiToRunway = new WaitPanelThread(new Point(10, 0), 100, true, false, pnlTaxiToRunway, null, semaphoreTaxiToRunway, semaphoreRunway, null, bufferTaxiToRunway, bufferRunway, null, 16, 4);
            runway = new WaitPanelThread(new Point(530, 10), 100, false, true, pnlRunway, null, semaphoreRunway, semaphoreTaxiFromRunway, null, bufferRunway, bufferTaxiFromRunway, null, 43, 0);
            taxiFromRunway = new WaitPanelThread(new Point(10, 150), 100, false, false, pnlTaxiFromRunway, null, semaphoreTaxiFromRunway, semaphoreTaxiBetween1and2, semaphoreTerminal1, bufferTaxiFromRunway, bufferTaxiBetween1and2, bufferTerminal1, 15, 1);

            semaphoreThreadTerminal1 = new Thread(new ThreadStart(semaphoreTerminal1.Start));
            semaphoreThreadTerminal2 = new Thread(new ThreadStart(semaphoreTerminal2.Start));
            semaphoreThreadTerminal3 = new Thread(new ThreadStart(semaphoreTerminal3.Start));
            semaphoreThreadArrivals1 = new Thread(new ThreadStart(semaphoreArrivals1.Start));
            semaphoreThreadTaxiBetween1and2 = new Thread(new ThreadStart(semaphoreTaxiBetween1and2.Start));
            semaphoreThreadTaxiBetween2and3 = new Thread(new ThreadStart(semaphoreTaxiBetween2and3.Start));
            semaphoreThreadTaxiBetween3andToRunway = new Thread(new ThreadStart(semaphoreTaxiBetween3andToRunway.Start));
            semaphoreThreadTaxiToRunway = new Thread(new ThreadStart(semaphoreTaxiToRunway.Start));
            semaphoreThreadRunway = new Thread(new ThreadStart(semaphoreRunway.Start));
            semaphoreThreadTaxiFromRunway = new Thread(new ThreadStart(semaphoreTaxiFromRunway.Start));
            bufferThreadTerminal1 = new Thread(new ThreadStart(bufferTerminal1.Start));
            bufferThreadTerminal2 = new Thread(new ThreadStart(bufferTerminal2.Start));
            bufferThreadTerminal3 = new Thread(new ThreadStart(bufferTerminal3.Start));
            bufferThreadArrivals1 = new Thread(new ThreadStart(bufferArrivals1.Start));
            bufferThreadTaxiBetween1and2 = new Thread(new ThreadStart(bufferTaxiBetween1and2.Start));
            bufferThreadTaxiBetween2and3 = new Thread(new ThreadStart(bufferTaxiBetween2and3.Start));
            bufferThreadTaxiBetween3andToRunway = new Thread(new ThreadStart(bufferTaxiBetween3andToRunway.Start));
            bufferThreadTaxiToRunway = new Thread(new ThreadStart(bufferTaxiToRunway.Start));
            bufferThreadRunway = new Thread(new ThreadStart(bufferRunway.Start));
            bufferThreadTaxiFromRunway = new Thread(new ThreadStart(bufferTaxiFromRunway.Start));

            thread1 = new Thread(new ThreadStart(terminal1.Start));
            thread2 = new Thread(new ThreadStart(terminal2.Start));
            thread3 = new Thread(new ThreadStart(terminal3.Start));
            thread4 = new Thread(new ThreadStart(arrivals1.Start));
            threadBetween1and2 = new Thread(new ThreadStart(taxiBetween1and2.Start));
            threadBetween2and3 = new Thread(new ThreadStart(taxiBetween2and3.Start));
            threadBetween3andToRunway = new Thread(new ThreadStart(taxiBetween3andToRunway.Start));
            threadTaxiToRunway = new Thread(new ThreadStart(taxiToRunway.Start));
            threadRunway = new Thread(new ThreadStart(runway.Start));
            threadTaxiFromRunway = new Thread(new ThreadStart(taxiFromRunway.Start));

            this.Closing += new CancelEventHandler(this.Form1_Closing);

            semaphoreThreadTerminal1.Start();
            semaphoreThreadTerminal2.Start();
            semaphoreThreadTerminal3.Start();
            semaphoreThreadArrivals1.Start();
            semaphoreThreadTaxiBetween1and2.Start();
            semaphoreThreadTaxiBetween2and3.Start();
            semaphoreThreadTaxiBetween3andToRunway.Start();
            semaphoreThreadTaxiToRunway.Start();
            semaphoreThreadRunway.Start();
            semaphoreThreadTaxiFromRunway.Start();
            bufferThreadTerminal1.Start();
            bufferThreadTerminal2.Start();
            bufferThreadTerminal3.Start();
            bufferThreadArrivals1.Start();
            bufferThreadTaxiBetween1and2.Start();
            bufferThreadTaxiBetween2and3.Start();
            bufferThreadTaxiBetween3andToRunway.Start();
            bufferThreadTaxiToRunway.Start();
            bufferThreadRunway.Start();
            bufferThreadTaxiFromRunway.Start();
            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
            threadBetween1and2.Start();
            threadBetween2and3.Start();
            threadBetween3andToRunway.Start();
            threadTaxiToRunway.Start();
            threadRunway.Start();
            threadTaxiFromRunway.Start();
        }