This commit is contained in:
gdamms 2021-11-27 20:24:42 +01:00
parent 1bee6bad4d
commit 089aa332ce
3 changed files with 275 additions and 7 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.class

View file

@ -4,12 +4,279 @@ import linda.Callback;
import linda.Linda;
import linda.Tuple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/** Shared memory implementation of Linda. */
public class CentralizedLinda implements Linda {
List<Tuple> tuples;
Lock moniteur;
Condition accessDemand;
Condition SAS;
int nbReaders;
int nbWaiting;
boolean writing;
boolean SASused;
public CentralizedLinda() {
nbReaders = 0;
nbWaiting = 0;
writing = false;
SASused = false;
moniteur = new ReentrantLock();
accessDemand = moniteur.newCondition();
SAS = moniteur.newCondition();
tuples = new ArrayList<Tuple>();
}
// TO BE COMPLETED
public void write(Tuple t) {
try {
// Wait for writing access
requestWriting();
// Add the new tuple
tuples.add(t);
// End writing
endWriting();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public Tuple take(Tuple template) {
Tuple result = null;
boolean found = false;
int index;
try {
while (!found) {
// Wait for writing access
requestWriting();
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
found = true;
break;
}
}
if (found) {
// Result found, remove it from the tuple list
result = tuples.remove(index);
}
// End writing
endWriting();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
public Tuple read(Tuple template) {
Tuple result = null;
boolean found = false;
int index;
try {
while (!found) {
// Waiting for reading access
requestReading();
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
found = true;
break;
}
}
if (found) {
// Result found, remove it from the tuple list
result = tuples.get(index);
}
// End reading
endReading();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
public Tuple tryTake(Tuple template) {
Tuple result = null;
int index;
try {
// Wait for writing access
requestWriting();
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
result = tuples.remove(index);
break;
}
}
// End writing
endWriting();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
public Tuple tryRead(Tuple template) {
Tuple result = null;
int index;
try {
// Waiting for reading access
requestReading();
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
result = tuples.get(index);
break;
}
}
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
public Collection<Tuple> takeAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
int index;
try {
// Wait for writing access
requestWriting();
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
results.add(tuples.get(index));
}
}
// End writing
endWriting();
} catch (InterruptedException e) {
e.printStackTrace();
}
return results;
}
public Collection<Tuple> readAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
int index;
try {
// Waiting for reading access
requestReading();
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
results.add(tuples.get(index));
}
}
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
}
return results;
}
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
}
public void debug(String prefix) {
System.out.println(prefix + tuples);
}
private void requestReading() throws InterruptedException {
moniteur.lock();
if (!(!writing && nbWaiting == 0)) {
nbWaiting++;
accessDemand.await();
nbWaiting--;
}
nbReaders++;
accessDemand.signal();
moniteur.unlock();
}
private void endReading() throws InterruptedException {
moniteur.lock();
nbReaders--;
if (nbReaders == 0) {
if (SASused) {
SAS.signal();
} else {
accessDemand.signal();
}
}
moniteur.unlock();
}
private void requestWriting() throws InterruptedException {
moniteur.lock();
if (!(!writing && nbReaders == 0 && !SASused && nbWaiting == 0)) {
nbWaiting++;
accessDemand.await();
nbWaiting--;
}
if (nbReaders > 0) {
SASused = true;
SAS.await();
SASused = false;
}
writing = true;
moniteur.unlock();
}
private void endWriting() throws InterruptedException {
moniteur.lock();
writing = false;
accessDemand.signal();
moniteur.unlock();
}
}

View file

@ -5,10 +5,10 @@ import linda.*;
public class BasicTest1 {
public static void main(String[] a) {
final Linda linda = new linda.shm.CentralizedLinda();
// final Linda linda = new linda.server.LindaClient("//localhost:4000/aaa");
new Thread() {
public void run() {
try {
@ -22,7 +22,7 @@ public class BasicTest1 {
linda.debug("(1)");
}
}.start();
new Thread() {
public void run() {
try {
@ -46,11 +46,11 @@ public class BasicTest1 {
Tuple t3 = new Tuple(4, "foo");
System.out.println("(2) write: " + t3);
linda.write(t3);
linda.debug("(2)");
}
}.start();
}
}