Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite OutputController to use reentrant locks #918

Open
wants to merge 1 commit into
base: 1.8
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions netx/net/sourceforge/jnlp/util/logging/OutputController.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import net.sourceforge.jnlp.runtime.JNLPRuntime;
import net.sourceforge.jnlp.util.logging.headers.Header;
import net.sourceforge.jnlp.util.logging.headers.JavaMessage;
Expand Down Expand Up @@ -106,6 +110,8 @@ public boolean isInfo() {
private PrintStreamLogger outLog;
private PrintStreamLogger errLog;
private final List<MessageWithHeader> messageQue = new LinkedList<>();
private ReentrantLock mqLock = new ReentrantLock();
private Condition mqNotEmpty = mqLock.newCondition();
private final MessageQueConsumer messageQueConsumer = new MessageQueConsumer();
Thread consumerThread;
/*stdin reader for headless dialogues*/
Expand All @@ -118,37 +124,53 @@ private class MessageQueConsumer implements Runnable {
public void run() {
while (true) {
try {
synchronized (OutputController.this) {
OutputController.this.wait(1000);
if (!(OutputController.this == null || messageQue.isEmpty())) {
flush();
}
mqLock.lock();
try {
mqNotEmpty.await(1, TimeUnit.SECONDS);
} finally {
mqLock.unlock();
}

flush();
} catch (Throwable t) {
OutputController.getLogger().log(t);
}
}
}
};

public synchronized void flush() {
private boolean isMqEmpty() {
mqLock.lock();
try {
return messageQue.isEmpty();
} finally {
mqLock.unlock();
}
}

while (!messageQue.isEmpty()) {
public void flush() {
while(!isMqEmpty()) {
consume();
}
}

public void close() throws Exception {
flush();
if (LogConfig.getLogConfig().isLogToFile()){
getFileLog().close();
}
}

private MessageWithHeader getNextMessage() {
mqLock.lock();
try {
return messageQue.remove(0);
} finally {
mqLock.unlock();
}
}

private void consume() {
MessageWithHeader s = messageQue.get(0);
messageQue.remove(0);
MessageWithHeader s = getNextMessage();
//filtering is done in console during runtime
if (LogConfig.getLogConfig().isLogToConsole()) {
JavaConsole.getConsole().addMessage(s);
Expand Down Expand Up @@ -248,8 +270,11 @@ public void run() {
public void startConsumer() {
consumerThread.start();
//some messages were probably posted before start of consumer
synchronized (this) {
this.notifyAll();
mqLock.lock();
try {
mqNotEmpty.signalAll();
} finally {
mqLock.unlock();
}
}

Expand Down Expand Up @@ -335,9 +360,14 @@ private void log(Level level, Object o) {
log(new JavaMessage(new Header(level, false), s));
}

synchronized void log(MessageWithHeader l){
messageQue.add(l);
this.notifyAll();
void log(MessageWithHeader l){
mqLock.lock();
try {
messageQue.add(l);
mqNotEmpty.signalAll();
} finally {
mqLock.unlock();
}
}


Expand Down