fix: linda parfait ?

This commit is contained in:
gdamms 2021-12-17 19:30:08 +01:00
parent 82c1db9858
commit c3cbb68709

View file

@ -15,7 +15,7 @@ import java.util.concurrent.locks.ReentrantLock;
class Reveil implements Callback {
static Lock lock = new ReentrantLock();
Lock lock = new ReentrantLock();
Tuple template;
Condition condition;
@ -31,7 +31,9 @@ class Reveil implements Callback {
void sleep() {
try {
lock.lock();
condition.await();
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
@ -39,7 +41,9 @@ class Reveil implements Callback {
void reveil(Tuple t) {
if (t.matches(this.template)) {
lock.lock();
condition.signal();
lock.unlock();
}
}
}
@ -61,8 +65,10 @@ public class CentralizedLinda implements Linda {
}
public Tuple take(Tuple template) {
Tuple result = null;
Reveil reveil = new Reveil(template);
reveils.add(reveil);
Tuple result = null;
result = tryTake(template);
while (result == null) {
@ -74,8 +80,10 @@ public class CentralizedLinda implements Linda {
}
public Tuple read(Tuple template) {
Tuple result = null;
Reveil reveil = new Reveil(template);
reveils.add(reveil);
Tuple result = null;
result = tryRead(template);
while (result == null) {
@ -87,17 +95,14 @@ public class CentralizedLinda implements Linda {
}
public Tuple tryTake(Tuple template) {
Tuple result = null;
// Extract the tuple from the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
if (tuples.get(i).matches(template)) {
result = tuples.remove(i);
break;
return tuples.remove(i);
}
}
return result;
return null;
}
public Tuple tryRead(Tuple template) {
@ -108,7 +113,7 @@ public class CentralizedLinda implements Linda {
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template)) {
break;
return result;
}
}
return result;
@ -145,96 +150,46 @@ public class CentralizedLinda implements Linda {
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
new Thread() {
public void run() {
Tuple result = null;
boolean found = false;
int index;
// Get known tuples for FUTURE timing
List<Tuple> knownTuples = (List<Tuple>) readAll(template);
try {
while (!found) {
// Waiting for access
switch (mode) {
case READ:
requestReading();
break;
case TAKE:
requestWriting();
break;
}
// Find the tuple in the tuple list
for (index = 0; index < tuples.size(); index++) {
if (tuples.get(index).matches(template)) {
// Tuple matching
if (timing == eventTiming.IMMEDIATE || knownTuples.isEmpty()) {
found = true;
} else {
for (Tuple knownTuple : knownTuples) {
if (knownTuple != tuples.get(index)) {
found = true;
break;
}
}
}
// Rebreak to end searching
if (found) {
break;
}
}
}
if (found) {
// Result found
switch (mode) {
case READ:
// Get it from the tuple list
result = tuples.get(index);
break;
case TAKE:
// Remove it from the tuple list
result = tuples.remove(index);
break;
}
}
// End access
switch (mode) {
case READ:
endReading();
break;
case TAKE:
endWriting();
break;
}
}
// Callback with the result tuple
callback.call(result);
} catch (InterruptedException e) {
e.printStackTrace();
switch (timing) {
case IMMEDIATE:
callback.call(mode == eventMode.READ ? read(template) : take(template));
return;
case FUTURE:
List<Tuple> knownTuples = (List<Tuple>) readAll(template);
callback.call(tryRTknown(template, knownTuples, mode));
return;
}
}
}.start();
}
public void debug(String prefix) {
try {
// Waiting for reading access
requestReading();
private Tuple tryRTknown(Tuple template, List<Tuple> knownTuples, eventMode mode) {
Reveil reveil = new Reveil(template);
reveils.add(reveil);
System.out.println(prefix + tuples);
Tuple result = null;
// End reading
endReading();
} catch (InterruptedException e) {
e.printStackTrace();
for (int i = 0; i < tuples.size(); i++) {
result = mode == eventMode.READ ? tuples.get(i) : tuples.remove(i);
if (result.matches(template)) {
return result;
}
}
while (result == null) {
reveil.sleep();
for (int i = 0; i < tuples.size(); i++) {
result = mode == eventMode.READ ? tuples.get(i) : tuples.remove(i);
if (result.matches(template)) {
return result;
}
}
}
return result;
}
public void debug(String prefix) {
System.out.println(prefix + tuples);
}
}