~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/org/jcsp/lang/CrewServer.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
7
    //  This library is free software; you can redistribute it and/or   //
 
8
    //  modify it under the terms of the GNU Lesser General Public      //
 
9
    //  License as published by the Free Software Foundation; either    //
 
10
    //  version 2.1 of the License, or (at your option) any later       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
13
    //  This library is distributed in the hope that it will be         //
 
14
    //  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
    //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
    //  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
19
    //  You should have received a copy of the GNU Lesser General       //
 
20
    //  Public License along with this library; if not, write to the    //
 
21
    //  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
    //  Boston, MA 02111-1307, USA.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
package org.jcsp.lang;
 
30
 
 
31
/**
 
32
 * @author P.H. Welch
 
33
 */
 
34
class CrewServer implements CSProcess
 
35
{
 
36
 
 
37
    public static final int READER = 0;
 
38
    public static final int WRITER = 1;
 
39
 
 
40
    private final AltingChannelInputInt request;
 
41
    private final AltingChannelInputInt writerControl;
 
42
    private final AltingChannelInputInt readerRelease;
 
43
    
 
44
    ///TODO change this to use poisoning of the above channels, once poison is added
 
45
    private final AltingChannelInputInt poison;
 
46
 
 
47
   public CrewServer(final AltingChannelInputInt request,
 
48
                     final AltingChannelInputInt writerControl,
 
49
                     final AltingChannelInputInt readerRelease,
 
50
                     final AltingChannelInputInt poison)
 
51
   {
 
52
       this.request = request;
 
53
       this.writerControl = writerControl;
 
54
       this.readerRelease = readerRelease;
 
55
       this.poison = poison;
 
56
   }
 
57
 
 
58
    public void run()
 
59
    {
 
60
        int nReaders = 0;
 
61
        final Alternative altMain =
 
62
            new Alternative(new Guard[] {readerRelease, request, poison});
 
63
        final int MAIN_READER_RELEASE = 0;
 
64
        final int MAIN_REQUEST = 1;
 
65
        final int MAIN_POISON = 2;
 
66
        final Alternative altWriteComplete =
 
67
            new Alternative(new Guard[] {writerControl, poison});
 
68
        final int WC_WRITER_CONTROL = 0;
 
69
        final int WC_POISON = 1;
 
70
        final Alternative altReadComplete =
 
71
            new Alternative(new Guard[] {readerRelease, poison});
 
72
        final int RC_READER_RELEASE = 0;
 
73
        final int RC_POISON = 1;
 
74
        while (true)
 
75
        {
 
76
            // invariant : (nReaders is the number of current readers) and (there are no writers)
 
77
            switch (altMain.priSelect())
 
78
            {
 
79
            case MAIN_READER_RELEASE:
 
80
                readerRelease.read();
 
81
                nReaders--;
 
82
                break;
 
83
            case MAIN_REQUEST:
 
84
                switch (request.read())
 
85
                {
 
86
                case READER:
 
87
                    nReaders++;
 
88
                    break;
 
89
                case WRITER:
 
90
                    int n = nReaders; // tmp
 
91
                    for (int i = 0; i < n; i++)
 
92
                    {
 
93
                        switch (altReadComplete.priSelect())
 
94
                        {
 
95
                        case RC_READER_RELEASE:
 
96
                            readerRelease.read();
 
97
                            nReaders--; // tmp
 
98
                            break;
 
99
                        case RC_POISON:
 
100
                            poison.read(); // let the finalizer complete
 
101
                            return;
 
102
                        }
 
103
                    }
 
104
                    nReaders = 0;
 
105
                    writerControl.read(); // let writer start writing
 
106
                    switch (altWriteComplete.priSelect())
 
107
                    {
 
108
                    case WC_WRITER_CONTROL:
 
109
                        writerControl.read(); // let writer finish writing
 
110
                        break;
 
111
                    case WC_POISON:
 
112
                        poison.read(); // let the finalizer complete
 
113
                        return;
 
114
                    }
 
115
                    break;
 
116
                }
 
117
                break;
 
118
            case MAIN_POISON:
 
119
                poison.read(); // let the finalizer complete
 
120
                return;
 
121
            }
 
122
        }
 
123
    }
 
124
}