83 lines
2.1 KiB
Java
83 lines
2.1 KiB
Java
|
// Time-stamp: <08 Apr 2008 11:35 queinnec@enseeiht.fr>
|
||
|
|
||
|
import java.util.concurrent.locks.Condition;
|
||
|
import java.util.concurrent.locks.Lock;
|
||
|
import java.util.concurrent.locks.ReentrantLock;
|
||
|
|
||
|
/**
|
||
|
* Lecteurs/rédacteurs stratégie d'ordonnancement: priorité FIFO, implantation:
|
||
|
* avec un moniteur.
|
||
|
*/
|
||
|
public class LectRed_FIFO implements LectRed {
|
||
|
|
||
|
Lock lock;
|
||
|
Condition requestAccess;
|
||
|
Condition SAS;
|
||
|
boolean activeWriter;
|
||
|
int activeReaders;
|
||
|
boolean SASoccupied;
|
||
|
int FIFOsize;
|
||
|
|
||
|
public LectRed_FIFO() {
|
||
|
lock = new ReentrantLock();
|
||
|
requestAccess = lock.newCondition();
|
||
|
SAS = lock.newCondition();
|
||
|
activeWriter = false;
|
||
|
activeReaders = 0;
|
||
|
SASoccupied = false;
|
||
|
FIFOsize = 0;
|
||
|
}
|
||
|
|
||
|
public void demanderLecture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
if (activeWriter || FIFOsize > 0) {
|
||
|
FIFOsize++;
|
||
|
requestAccess.await();
|
||
|
FIFOsize--;
|
||
|
}
|
||
|
activeReaders++;
|
||
|
requestAccess.signal();
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public void terminerLecture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
activeReaders--;
|
||
|
if (activeReaders == 0) {
|
||
|
if (SASoccupied) {
|
||
|
SAS.signal();
|
||
|
} else {
|
||
|
requestAccess.signal();
|
||
|
}
|
||
|
}
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public void demanderEcriture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
if (activeWriter || activeReaders > 0 || FIFOsize > 0 || SASoccupied) {
|
||
|
FIFOsize++;
|
||
|
requestAccess.await();
|
||
|
FIFOsize--;
|
||
|
}
|
||
|
if (activeReaders > 0) {
|
||
|
SASoccupied = true;
|
||
|
SAS.await();
|
||
|
SASoccupied = false;
|
||
|
}
|
||
|
activeWriter = true;
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public void terminerEcriture() throws InterruptedException {
|
||
|
lock.lock();
|
||
|
activeWriter = false;
|
||
|
requestAccess.signal();
|
||
|
lock.unlock();
|
||
|
}
|
||
|
|
||
|
public String nomStrategie() {
|
||
|
return "Stratégie: Moniteur, prio FIFO.";
|
||
|
}
|
||
|
}
|