Skip to content

Commit

Permalink
remove some synchronize (using Lock instead)
Browse files Browse the repository at this point in the history
  • Loading branch information
jomifred committed Sep 26, 2024
1 parent 8e0f697 commit 96a6549
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
23 changes: 17 additions & 6 deletions jason-interpreter/src/main/java/jason/asSemantics/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -707,7 +709,8 @@ public List<Option> relevantPlans(Trigger teP, Event evt) throws JasonException
}

public List<Option> applicablePlans(List<Option> rp) throws JasonException {
synchronized (getTS().getC().syncApPlanSense) {
getTS().getC().syncApPlanSense.lock();
try {
List<Option> ap = null;
if (rp != null) {
for (Option opt: rp) {
Expand Down Expand Up @@ -750,6 +753,8 @@ public List<Option> applicablePlans(List<Option> rp) throws JasonException {
}
}
return ap;
} finally {
getTS().getC().syncApPlanSense.unlock();
}
}

Expand Down Expand Up @@ -1135,8 +1140,9 @@ public boolean hasCustomSelectOption() {
static DocumentBuilder builder = null;


private Lock agStateLock = new ReentrantLock();
/** Gets the agent "mind" (beliefs, plans, and circumstance) as XML */
public synchronized Document getAgState() {
public Document getAgState() {
if (builder == null) {
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Expand All @@ -1148,11 +1154,16 @@ public synchronized Document getAgState() {
Document docDOM = builder.newDocument();
docDOM.appendChild(docDOM.createProcessingInstruction("xml-stylesheet", "href='http://jason.sf.net/xml/agInspection.xsl' type='text/xsl' "));

Element ag = getAsDOM(docDOM);
docDOM.appendChild(ag);
agStateLock.lock();
try {
Element ag = getAsDOM(docDOM);
docDOM.appendChild(ag);

ag.appendChild(ts.getC().getAsDOM(docDOM));
return docDOM;
ag.appendChild(ts.getC().getAsDOM(docDOM));
return docDOM;
} finally {
agStateLock.unlock();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ public class Circumstance implements Serializable, ToDOM {

private TransitionSystem ts = null;

public transient Object syncApPlanSense = new Object();
public Lock syncApPlanSense = new ReentrantLock();

public Circumstance() {
syncApPlanSense = new Object();
create();
reset();
}
Expand All @@ -74,9 +73,9 @@ public void resetIntentionsWithGoalCondition() {
}


@Serial
private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
inputStream.defaultReadObject();
syncApPlanSense = new Object();
}

public void setTS(TransitionSystem ts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1662,8 +1662,11 @@ public void sense() {

if (nrcslbr >= setts.nrcbp()) {
nrcslbr = 0;
synchronized (C.syncApPlanSense) {
C.syncApPlanSense.lock();
try {
ag.buf(getAgArch().perceive());
} finally {
C.syncApPlanSense.unlock();
}
getAgArch().checkMail();
}
Expand Down

0 comments on commit 96a6549

Please sign in to comment.