projet-donnees-reparties/linda/shm/CentralizedLinda.java
2021-12-17 19:30:08 +01:00

196 lines
5.1 KiB
Java

package linda.shm;
import linda.Callback;
import linda.Linda;
import linda.Tuple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Reveil implements Callback {
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 {
lock.lock();
condition.await();
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void reveil(Tuple t) {
if (t.matches(this.template)) {
lock.lock();
condition.signal();
lock.unlock();
}
}
}
/** Shared memory implementation of Linda. */
public class CentralizedLinda implements Linda {
List<Tuple> tuples;
List<Reveil> reveils;
public CentralizedLinda() {
this.tuples = Collections.synchronizedList(new ArrayList<Tuple>());
this.reveils = Collections.synchronizedList(new ArrayList<Reveil>());
}
public void write(Tuple t) {
tuples.add(t);
reveils.forEach(r -> r.reveil(t));
}
public Tuple take(Tuple template) {
Reveil reveil = new Reveil(template);
reveils.add(reveil);
Tuple result = null;
result = tryTake(template);
while (result == null) {
reveil.sleep();
result = tryTake(template);
}
return result;
}
public Tuple read(Tuple template) {
Reveil reveil = new Reveil(template);
reveils.add(reveil);
Tuple result = null;
result = tryRead(template);
while (result == null) {
reveil.sleep();
result = tryRead(template);
}
return result;
}
public Tuple tryTake(Tuple template) {
// 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)) {
return tuples.remove(i);
}
}
return null;
}
public Tuple tryRead(Tuple template) {
Tuple result = null;
// Get the tuple from the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template)) {
return result;
}
}
return result;
}
public Collection<Tuple> takeAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
Tuple result = new Tuple();
// Extract the tuples in the tuple list
while (result != null) {
result = tryTake(template);
if (result != null) {
results.add(result);
}
}
return results;
}
public Collection<Tuple> readAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
Tuple result;
// Extract the tuples in the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template))
results.add(result);
}
return results;
}
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
new Thread() {
public void run() {
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();
}
private Tuple tryRTknown(Tuple template, List<Tuple> knownTuples, eventMode mode) {
Reveil reveil = new Reveil(template);
reveils.add(reveil);
Tuple result = null;
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);
}
}