fix: sans saturation du cpu c mi-E

This commit is contained in:
gdamms 2021-12-17 09:33:01 +01:00
parent 7b925cf1ea
commit 82c1db9858

View file

@ -6,113 +6,81 @@ import linda.Tuple;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
class Reveil implements Callback {
static Lock lock = new ReentrantLock();
Tuple template;
Condition condition;
Reveil(Tuple template) {
this.template = template;
this.condition = lock.newCondition();
}
public void call(Tuple t) {
return;
}
void sleep() {
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void reveil(Tuple t) {
if (t.matches(this.template)) {
condition.signal();
}
}
}
/** Shared memory implementation of Linda. */ /** Shared memory implementation of Linda. */
public class CentralizedLinda implements Linda { public class CentralizedLinda implements Linda {
List<Tuple> tuples; List<Tuple> tuples;
List<Reveil> reveils;
Lock moniteur;
Condition accessDemand;
Condition SAS;
int nbReaders;
int nbWaiting;
boolean writing;
boolean SASused;
public CentralizedLinda() { public CentralizedLinda() {
nbReaders = 0; this.tuples = Collections.synchronizedList(new ArrayList<Tuple>());
nbWaiting = 0; this.reveils = Collections.synchronizedList(new ArrayList<Reveil>());
writing = false;
SASused = false;
moniteur = new ReentrantLock();
accessDemand = moniteur.newCondition();
SAS = moniteur.newCondition();
tuples = new ArrayList<Tuple>();
} }
public void write(Tuple t) { public void write(Tuple t) {
try { tuples.add(t);
// Wait for writing access reveils.forEach(r -> r.reveil(t));
requestWriting();
// Add the new tuple
tuples.add(t);
// End writing
endWriting();
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public Tuple take(Tuple template) { public Tuple take(Tuple template) {
Tuple result = null; Tuple result = null;
boolean found = false; Reveil reveil = new Reveil(template);
int index;
try { result = tryTake(template);
while (result == null) {
while (!found) { reveil.sleep();
// Wait for writing access result = tryTake(template);
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; return result;
} }
public Tuple read(Tuple template) { public Tuple read(Tuple template) {
Tuple result = null; Tuple result = null;
boolean found = false; Reveil reveil = new Reveil(template);
int index;
try { result = tryRead(template);
while (!found) { while (result == null) {
// Waiting for reading access reveil.sleep();
requestReading(); result = tryRead(template);
// 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, get it from the tuple list
result = tuples.get(index);
}
// End reading
endReading();
}
} catch (InterruptedException e) {
e.printStackTrace();
} }
return result; return result;
@ -120,98 +88,57 @@ public class CentralizedLinda implements Linda {
public Tuple tryTake(Tuple template) { public Tuple tryTake(Tuple template) {
Tuple result = null; Tuple result = null;
int index;
try { // Extract the tuple from the tuple list
// Wait for writing access // TODO: faire avec iterator, hasNext, pour être thread safe :)
requestWriting(); for (int i = 0; i < tuples.size(); i++) {
if (tuples.get(i).matches(template)) {
// Extract the tuple in the tuple list result = tuples.remove(i);
for (index = 0; index < tuples.size(); index++) { break;
if (tuples.get(index).matches(template)) {
result = tuples.remove(index);
break;
}
} }
// End writing
endWriting();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return result; return result;
} }
public Tuple tryRead(Tuple template) { public Tuple tryRead(Tuple template) {
Tuple result = null; Tuple result = null;
int index;
try { // Get the tuple from the tuple list
// Waiting for reading access // TODO: faire avec iterator, hasNext, pour être thread safe :)
requestReading(); for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
// Find the tuple in the tuple list if (result.matches(template)) {
for (index = 0; index < tuples.size(); index++) { break;
if (tuples.get(index).matches(template)) {
result = tuples.get(index);
break;
}
} }
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return result; return result;
} }
public Collection<Tuple> takeAll(Tuple template) { public Collection<Tuple> takeAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>(); List<Tuple> results = new ArrayList<Tuple>();
int index; Tuple result = new Tuple();
try { // Extract the tuples in the tuple list
// Wait for writing access while (result != null) {
requestWriting(); result = tryTake(template);
if (result != null) {
// Extract the tuples in the tuple list results.add(result);
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
results.add(tuples.remove(index));
}
} }
// End writing
endWriting();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return results; return results;
} }
public Collection<Tuple> readAll(Tuple template) { public Collection<Tuple> readAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>(); List<Tuple> results = new ArrayList<Tuple>();
int index; Tuple result;
try { // Extract the tuples in the tuple list
// Waiting for reading access // TODO: faire avec iterator, hasNext, pour être thread safe :)
requestReading(); for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
// Find the tuples in the tuple list if (result.matches(template))
for (index = 0; index < tuples.size(); index++) { results.add(result);
if (tuples.get(index).matches(template)) {
results.add(tuples.get(index));
}
}
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
} }
return results; return results;
} }
@ -310,60 +237,4 @@ public class CentralizedLinda implements Linda {
} }
} }
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();
}
} }