2021-11-27 16:50:33 +00:00
|
|
|
package linda.shm;
|
|
|
|
|
|
|
|
import linda.Callback;
|
|
|
|
import linda.Linda;
|
|
|
|
import linda.Tuple;
|
|
|
|
|
2021-11-27 19:24:42 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2021-12-17 08:33:01 +00:00
|
|
|
import java.util.Collections;
|
2021-12-28 13:46:33 +00:00
|
|
|
import java.util.Iterator;
|
2021-11-27 19:24:42 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.concurrent.locks.Condition;
|
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
class Reveil implements Callback {
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 18:30:08 +00:00
|
|
|
Lock lock = new ReentrantLock();
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
Tuple template;
|
|
|
|
Condition condition;
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
Reveil(Tuple template) {
|
|
|
|
this.template = template;
|
|
|
|
this.condition = lock.newCondition();
|
|
|
|
}
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
public void call(Tuple t) {
|
|
|
|
return;
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
void sleep() {
|
2021-11-27 19:24:42 +00:00
|
|
|
try {
|
2021-12-17 18:30:08 +00:00
|
|
|
lock.lock();
|
2021-12-17 08:33:01 +00:00
|
|
|
condition.await();
|
2021-12-17 18:30:08 +00:00
|
|
|
lock.unlock();
|
2021-11-27 19:24:42 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
void reveil(Tuple t) {
|
|
|
|
if (t.matches(this.template)) {
|
2021-12-17 18:30:08 +00:00
|
|
|
lock.lock();
|
2021-12-17 08:33:01 +00:00
|
|
|
condition.signal();
|
2021-12-17 18:30:08 +00:00
|
|
|
lock.unlock();
|
2021-12-17 08:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
/** Shared memory implementation of Linda. */
|
|
|
|
public class CentralizedLinda implements Linda {
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
List<Tuple> tuples;
|
|
|
|
List<Reveil> reveils;
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
public CentralizedLinda() {
|
|
|
|
this.tuples = Collections.synchronizedList(new ArrayList<Tuple>());
|
|
|
|
this.reveils = Collections.synchronizedList(new ArrayList<Reveil>());
|
|
|
|
}
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Insert a tuple into the tuple list. (non-blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-12-17 08:33:01 +00:00
|
|
|
public void write(Tuple t) {
|
|
|
|
tuples.add(t);
|
|
|
|
reveils.forEach(r -> r.reveil(t));
|
|
|
|
}
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Extract the tuple from the tuple list. (blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-12-17 08:33:01 +00:00
|
|
|
public Tuple take(Tuple template) {
|
|
|
|
Reveil reveil = new Reveil(template);
|
2021-12-17 18:30:08 +00:00
|
|
|
reveils.add(reveil);
|
|
|
|
|
|
|
|
Tuple result = null;
|
2021-12-17 08:33:01 +00:00
|
|
|
|
|
|
|
result = tryTake(template);
|
|
|
|
while (result == null) {
|
|
|
|
reveil.sleep();
|
|
|
|
result = tryTake(template);
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
2021-12-17 08:33:01 +00:00
|
|
|
|
2021-11-27 19:24:42 +00:00
|
|
|
return result;
|
2021-11-27 16:50:33 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Get the tuple from the tuple list. (blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-11-27 19:24:42 +00:00
|
|
|
public Tuple read(Tuple template) {
|
2021-12-17 08:33:01 +00:00
|
|
|
Reveil reveil = new Reveil(template);
|
2021-12-17 18:30:08 +00:00
|
|
|
reveils.add(reveil);
|
|
|
|
|
|
|
|
Tuple result = null;
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
result = tryRead(template);
|
|
|
|
while (result == null) {
|
|
|
|
reveil.sleep();
|
|
|
|
result = tryRead(template);
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Extract the tuple from the tuple list. (non-blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-11-27 19:24:42 +00:00
|
|
|
public Tuple tryTake(Tuple template) {
|
2021-12-28 13:46:33 +00:00
|
|
|
Iterator<Tuple> iterator = tuples.iterator();
|
|
|
|
Tuple next = null;
|
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
next = iterator.next();
|
|
|
|
if (next.matches(template)) {
|
|
|
|
iterator.remove();
|
|
|
|
break;
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-28 13:46:33 +00:00
|
|
|
return next;
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Get the tuple from the tuple list. (non-blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-11-27 19:24:42 +00:00
|
|
|
public Tuple tryRead(Tuple template) {
|
2021-12-28 13:46:33 +00:00
|
|
|
Iterator<Tuple> iterator = tuples.iterator();
|
|
|
|
Tuple next = null;
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
next = iterator.next();
|
|
|
|
if (next.matches(template)) {
|
|
|
|
break;
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-28 13:46:33 +00:00
|
|
|
return next;
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Extract the tuples in the tuple list. (blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-11-27 19:24:42 +00:00
|
|
|
public Collection<Tuple> takeAll(Tuple template) {
|
|
|
|
List<Tuple> results = new ArrayList<Tuple>();
|
2021-12-17 08:33:01 +00:00
|
|
|
Tuple result = new Tuple();
|
2021-11-27 19:24:42 +00:00
|
|
|
|
2021-12-17 08:33:01 +00:00
|
|
|
while (result != null) {
|
|
|
|
result = tryTake(template);
|
|
|
|
if (result != null) {
|
|
|
|
results.add(result);
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
/**
|
|
|
|
* Extract the tuples in the tuple list. (blocking)
|
|
|
|
*
|
|
|
|
* @param template the Tuple template your looking for
|
|
|
|
*/
|
2021-11-27 19:24:42 +00:00
|
|
|
public Collection<Tuple> readAll(Tuple template) {
|
|
|
|
List<Tuple> results = new ArrayList<Tuple>();
|
2021-12-28 13:46:33 +00:00
|
|
|
Iterator<Tuple> iterator = tuples.iterator();
|
|
|
|
Tuple next = null;
|
2021-12-17 08:33:01 +00:00
|
|
|
|
2021-12-28 13:46:33 +00:00
|
|
|
while (iterator.hasNext()) {
|
|
|
|
next = iterator.next();
|
|
|
|
if (next.matches(template)) {
|
|
|
|
results.add(next);
|
|
|
|
}
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void eventRegister(eventMode mode, eventTiming timing, Tuple template, Callback callback) {
|
2021-11-28 11:44:46 +00:00
|
|
|
new Thread() {
|
|
|
|
public void run() {
|
2021-12-17 18:30:08 +00:00
|
|
|
|
|
|
|
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;
|
2021-11-28 11:44:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 18:30:08 +00:00
|
|
|
private Tuple tryRTknown(Tuple template, List<Tuple> knownTuples, eventMode mode) {
|
|
|
|
Reveil reveil = new Reveil(template);
|
|
|
|
reveils.add(reveil);
|
2021-11-30 16:39:17 +00:00
|
|
|
|
2021-12-17 18:30:08 +00:00
|
|
|
Tuple result = null;
|
2021-11-30 16:39:17 +00:00
|
|
|
|
2021-12-17 18:30:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-11-30 16:39:17 +00:00
|
|
|
}
|
2021-12-17 18:30:08 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void debug(String prefix) {
|
|
|
|
System.out.println(prefix + tuples);
|
2021-11-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 16:50:33 +00:00
|
|
|
}
|