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