1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
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 //
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 //
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. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
29
package org.jcsp.lang;
34
class CrewServer implements CSProcess
37
public static final int READER = 0;
38
public static final int WRITER = 1;
40
private final AltingChannelInputInt request;
41
private final AltingChannelInputInt writerControl;
42
private final AltingChannelInputInt readerRelease;
44
///TODO change this to use poisoning of the above channels, once poison is added
45
private final AltingChannelInputInt poison;
47
public CrewServer(final AltingChannelInputInt request,
48
final AltingChannelInputInt writerControl,
49
final AltingChannelInputInt readerRelease,
50
final AltingChannelInputInt poison)
52
this.request = request;
53
this.writerControl = writerControl;
54
this.readerRelease = readerRelease;
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;
76
// invariant : (nReaders is the number of current readers) and (there are no writers)
77
switch (altMain.priSelect())
79
case MAIN_READER_RELEASE:
84
switch (request.read())
90
int n = nReaders; // tmp
91
for (int i = 0; i < n; i++)
93
switch (altReadComplete.priSelect())
95
case RC_READER_RELEASE:
100
poison.read(); // let the finalizer complete
105
writerControl.read(); // let writer start writing
106
switch (altWriteComplete.priSelect())
108
case WC_WRITER_CONTROL:
109
writerControl.read(); // let writer finish writing
112
poison.read(); // let the finalizer complete
119
poison.read(); // let the finalizer complete