Skip to content

Commit

Permalink
Merge branch 'release/0.4.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
aherranz committed Jul 23, 2019
2 parents e37ccb0 + de9ad4f commit 51836b9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 60 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

## Next release

## v0.4.9

- [F] volatile modifier to every integer attribute in Semaphore: meanSleepTimeAfterAwait_ms.
- [F] volatile modifier to every integer attribute in Monitor: inPurgatory, pendingSignals and meanSleepTimeAfterAwait.
- [F] Fabrica and Consumo allow 0 mean times now.

## v0.4.8

- [T] Makefile added
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ cclib is a Java library used in concurrency related course at Universidad Polit

Run `make` or `./gradlew tasks` and follow instructions.

## TODO

- Tryer.java to take advantage from generics (result type) and lambda.
- Check of mean times are really the mean of the indicated value: for
instance, nextInt(2 * n) generates an integer from 0 to 2n-1, this
means the mean is (2n-1)/2 that is not n.
We are following the git workflow *git flow*: https://guides.github.com/introduction/flow/

## Classes in the cclib

Expand Down Expand Up @@ -51,3 +46,13 @@ The rest of the classes are used in the class assignments. Most names are in Spa
in a shot).
- Productor (producer): instances are threads that simulate
production and storage of products

## TODO

- The library should be split, one for concurrency mechanisms, another
for classes used in "Concurrencia" assignments at UPM.
- Tryer.java to take advantage from generics (result type) and lambda.
- Check of mean times are really the mean of the indicated value: for
instance, nextInt(2 * n) generates an integer from 0 to 2n-1, this
means the mean is (2n-1)/2 that is not n.

15 changes: 14 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
// // Support for building a distribution ZIP for a Java library (JAR file for the library and its dependencies)
// id 'java-library-distribution'
}

version = "0.4.8"
version = "0.4.9"

repositories {
// Use jcenter for resolving your dependencies.
Expand All @@ -29,3 +31,14 @@ dependencies {
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}

// Add source to jar files (by default jar tasks only include compiled classes)
jar {
from sourceSets.main.allSource
}

// Task to zip documentation
task buildDoc(type: Zip, dependsOn: javadoc) {
from 'build/docs'
archiveName rootProject.name+"-"+version+"-docs.zip"
}
48 changes: 24 additions & 24 deletions src/main/java/es/upm/babel/cclib/Consumo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@ static public void establecerTiempoMedioCons(int tmc_ms) {
* Simula el consumo de un producto.
*/
static public void consumir(Producto prod) {
int t;
ConcIO.printfnl("inicio consumo: " + prod + "...");
t = random.nextInt(2 * tiempo_medio_cons_ms);
try {
Thread.sleep(t);
} catch (Exception ex) {
ConcIO.printfnl("excepción capturada: " + ex);
ex.printStackTrace();
} finally {
ConcIO.printfnl("fin consumo en " + t + "ms.");
}
}
ConcIO.printfnl("inicio consumo: " + prod + "...");
simularConsumo(prod);
ConcIO.printfnl("fin consumo: " + prod);
}

/**
* Simula el consumo de un <em>paquete</em> de productos.
Expand All @@ -53,19 +45,27 @@ static public void consumir(Producto[] prods) {
ConcIO.printfnl("inicio consumo de "
+ prods.length + " productos...");
for (int i = 0; i < prods.length; i++) {
int t;
t = random.nextInt(2 * tiempo_medio_cons_ms);
try {
Thread.sleep(t);
} catch (Exception ex) {
ConcIO.printfnl("excepción capturada: " + ex);
ex.printStackTrace();
} finally {
ConcIO.printfnl("consumido producto "
+ prods[i] + " en " + t + "ms.");
}
simularConsumo(prods[i]);
ConcIO.printfnl("producto consumido: " + prods[i]);
}
ConcIO.printfnl("fin consumo del paquete de "
+ prods.length + " productos");
}
}

/**
* Simula el consumo de un producto.
*
* @return tiempo de consumo
*/
static public void simularConsumo(Producto prod) {
if (tiempo_medio_cons_ms > 0) {
int t = random.nextInt(2 * tiempo_medio_cons_ms);
try {
Thread.sleep(t);
} catch (Exception ex) {
ConcIO.printfnl("excepción capturada: " + ex);
ex.printStackTrace();
}
}
}
}
43 changes: 20 additions & 23 deletions src/main/java/es/upm/babel/cclib/Fabrica.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,9 @@ static public void establecerTiempoMedioProd(int tmp_ms) {
*/
static public Producto producir() {
Producto prod;
int t;
ConcIO.printfnl("inicio producción...");
t = random.nextInt(2 * tiempo_medio_prod_ms);
try {
Thread.sleep(t);
} catch (Exception ex) {
ConcIO.printfnl("excepción capturada: " + ex);
ex.printStackTrace();
} finally {
prod = new Producto();
ConcIO.printfnl("fin producción: " + prod + " en " + t + "ms.");
}
prod = simularProduccion();
ConcIO.printfnl("fin producción: " + prod);
return prod;
}

Expand All @@ -57,21 +48,27 @@ static public Producto[] producir(int n) {
ConcIO.printfnl("inicio producción de "
+ prods.length + " productos...");
for (int i = 0; i < prods.length; i++) {
int t;
t = random.nextInt(2 * tiempo_medio_prod_ms);
try {
Thread.sleep(t);
} catch (Exception ex) {
ConcIO.printfnl("excepción capturada: " + ex);
ex.printStackTrace();
} finally {
prods[i] = new Producto();
ConcIO.printfnl("producido producto: "
+ prods[i] + " en " + t + "ms.");
}
prods[i] = simularProduccion();
ConcIO.printfnl("producto producido: " + prods[i]);
}
ConcIO.printfnl("fin producción del paquete de "
+ prods.length + " productos");
return prods;
}

/**
* Simula una producción de un producto.
*/
static private Producto simularProduccion() {
if (tiempo_medio_prod_ms > 0) {
int t = random.nextInt(2 * tiempo_medio_prod_ms);
try {
Thread.sleep(t);
} catch (Exception ex) {
ConcIO.printfnl("excepción capturada: " + ex);
ex.printStackTrace();
}
}
return new Producto();
}
}
6 changes: 3 additions & 3 deletions src/main/java/es/upm/babel/cclib/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class Monitor {
* Number of threads that were moved to the purgatory and have not
* reached heaven yet (they may have been signalled).
*/
private int inPurgatory = 0;
private volatile int inPurgatory = 0;

/**
* Number of signalled threads which have not completed monitor
* re-entrance, not counting those signalled in purgatory.
*/
private int pendingSignals = 0;
private volatile int pendingSignals = 0;

/**
* In order to force more arbitrary interleavings on the use of
Expand All @@ -39,7 +39,7 @@ public class Monitor {
* no sleep will be executed and the interleaving will be the
* "natural" one.
*/
private int meanSleepTimeAfterAwait_ms = 0;
private volatile int meanSleepTimeAfterAwait_ms = 0;

/**
* Random number generator for sleeping time after await.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/es/upm/babel/cclib/Semaphore.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Semaphore {
* no sleep will be executed and the interleaving will be the
* "natural" one.
*/
static private int meanSleepTimeAfterAwait_ms = 0;
static private volatile int meanSleepTimeAfterAwait_ms = 0;

/**
* Random number generator for sleeping time after await.
Expand All @@ -29,7 +29,7 @@ public class Semaphore {
public Semaphore() {
n = new java.util.concurrent.Semaphore(0,true);
}

/**
* Semaphore constructor. Internal counter initizalised to n if n
* is positive, 0 otherwise.
Expand All @@ -38,7 +38,7 @@ public Semaphore(int n) {
int i = n > 0 ? n : 0;
this.n = new java.util.concurrent.Semaphore(i,true);
}

/**
* If the parameter is greater than 0 more arbitrary interleavings
* will be introduced.
Expand Down

0 comments on commit 51836b9

Please sign in to comment.