projet-donnees-reparties/linda/shm/CentralizedLinda.java
Laureηt 7b3fe8f998
fix: remplacement des for loop par des hasNext
chore: wrote some docstrings
2021-12-28 14:46:33 +01:00

233 lines
5.9 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.Iterator;
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>());
}
/**
* Insert a tuple into the tuple list. (non-blocking)
*
* @param template the Tuple template your looking for
*/
public void write(Tuple t) {
tuples.add(t);
reveils.forEach(r -> r.reveil(t));
}
/**
* Extract the tuple from the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
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;
}
/**
* Get the tuple from the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
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;
}
/**
* Extract the tuple from the tuple list. (non-blocking)
*
* @param template the Tuple template your looking for
*/
public Tuple tryTake(Tuple template) {
Iterator<Tuple> iterator = tuples.iterator();
Tuple next = null;
while (iterator.hasNext()) {
next = iterator.next();
if (next.matches(template)) {
iterator.remove();
break;
}
}
return next;
}
/**
* Get the tuple from the tuple list. (non-blocking)
*
* @param template the Tuple template your looking for
*/
public Tuple tryRead(Tuple template) {
Iterator<Tuple> iterator = tuples.iterator();
Tuple next = null;
while (iterator.hasNext()) {
next = iterator.next();
if (next.matches(template)) {
break;
}
}
return next;
}
/**
* Extract the tuples in the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
public Collection<Tuple> takeAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
Tuple result = new Tuple();
while (result != null) {
result = tryTake(template);
if (result != null) {
results.add(result);
}
}
return results;
}
/**
* Extract the tuples in the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
public Collection<Tuple> readAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
Iterator<Tuple> iterator = tuples.iterator();
Tuple next = null;
while (iterator.hasNext()) {
next = iterator.next();
if (next.matches(template)) {
results.add(next);
}
}
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);
}
}