Skip to content

Commit

Permalink
Working in fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Jan 19, 2024
1 parent 17c660a commit 7c17988
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,8 +16,6 @@

package org.glassfish.examples.events.threaded.tests;

import java.util.concurrent.locks.ReentrantLock;

import javax.inject.Singleton;

import org.glassfish.hk2.api.messaging.MessageReceiver;
Expand All @@ -33,7 +31,7 @@
*/
@Service @Singleton @MessageReceiver
public class EventSubscriberService {
private final ReentrantLock lock = new ReentrantLock();
private final Object lock = new Object();
private Long eventThreadId = null;

/**
Expand All @@ -44,13 +42,10 @@ public class EventSubscriberService {
*/
@SuppressWarnings("unused")
private void eventSubscriber(@SubscribeTo Event event) {
try {
lock.lock();
synchronized (lock) {
eventThreadId = Thread.currentThread().getId();

lock.notifyAll();
} finally {
lock.unlock();
}

}
Expand All @@ -62,15 +57,12 @@ private void eventSubscriber(@SubscribeTo Event event) {
* @throws InterruptedException If the thread gets interrupted
*/
public long getEventThread() throws InterruptedException {
try {
lock.lock();
synchronized (lock) {
while (eventThreadId == null) {
lock.wait();
}

return eventThreadId;
} finally {
lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,7 +24,6 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand Down Expand Up @@ -86,7 +85,7 @@ public class ImmediateHelper implements DynamicConfigurationListener, Runnable,

private final HashSet<Long> tidsWithWork = new HashSet<Long>();

private final ReentrantLock queueLock = new ReentrantLock();
private final Object queueLock = new Object();
private boolean threadAvailable;
private boolean outstandingJob;
private boolean waitingForWork;
Expand Down Expand Up @@ -147,13 +146,10 @@ else if (waitingForWork) {

@Override
public void configurationChanged() {
try {
queueLock.lock();
synchronized (queueLock) {
if (currentState.equals(ImmediateServiceState.SUSPENDED)) return;

doWorkIfWeHaveSome();
} finally {
queueLock.unlock();
}
}

Expand All @@ -173,11 +169,9 @@ public void onFailure(ErrorInformation errorInformation)
if (!(ErrorType.DYNAMIC_CONFIGURATION_FAILURE.equals(errorInformation.getErrorType()))) {
// Only interested in dynamic configuration failures
long tid = Thread.currentThread().getId();
try {
queueLock.lock();

synchronized (queueLock) {
tidsWithWork.remove(tid);
} finally {
queueLock.unlock();
}

return;
Expand All @@ -190,11 +184,9 @@ public boolean validate(ValidationInformation info) {
if (info.getOperation().equals(Operation.BIND) ||
info.getOperation().equals(Operation.UNBIND)) {
long tid = Thread.currentThread().getId();
try {
queueLock.lock();

synchronized (queueLock) {
tidsWithWork.add(tid);
} finally {
queueLock.unlock();
}
}

Expand All @@ -208,8 +200,7 @@ public boolean validate(ValidationInformation info) {
@Override
public void run() {
for(;;) {
try {
queueLock.lock();
synchronized (queueLock) {
long decayTime = this.decayTime;

while (currentState.equals(ImmediateServiceState.RUNNING) &&
Expand Down Expand Up @@ -237,8 +228,6 @@ public void run() {
}

outstandingJob = false;
} finally {
queueLock.unlock();
}

immediateContext.doWork();
Expand All @@ -251,11 +240,8 @@ public void run() {
*/
@Override
public Executor getExecutor() {
try {
queueLock.lock();
synchronized (queueLock) {
return currentExecutor;
} finally {
queueLock.unlock();
}
}

Expand All @@ -264,15 +250,12 @@ public Executor getExecutor() {
*/
@Override
public void setExecutor(Executor executor) throws IllegalStateException {
try {
queueLock.lock();
synchronized (queueLock) {
if (currentState.equals(ImmediateServiceState.RUNNING)) {
throw new IllegalStateException("ImmediateSerivce attempt made to change executor while in RUNNING state");
}

currentExecutor = (executor == null) ? DEFAULT_EXECUTOR : executor ;
} finally {
queueLock.unlock();
}

}
Expand All @@ -282,11 +265,8 @@ public void setExecutor(Executor executor) throws IllegalStateException {
*/
@Override
public long getThreadInactivityTimeout() {
try {
queueLock.lock();
synchronized (queueLock) {
return decayTime;
} finally {
queueLock.unlock();
}
}

Expand All @@ -296,15 +276,12 @@ public long getThreadInactivityTimeout() {
@Override
public void setThreadInactivityTimeout(long timeInMillis)
throws IllegalStateException {
try {
queueLock.lock();
synchronized (queueLock) {
if (timeInMillis < 0) {
throw new IllegalArgumentException();
}

decayTime = timeInMillis;
} finally {
queueLock.unlock();
}

}
Expand All @@ -314,11 +291,8 @@ public void setThreadInactivityTimeout(long timeInMillis)
*/
@Override
public ImmediateServiceState getImmediateState() {
try {
queueLock.lock();
synchronized (queueLock) {
return currentState;
} finally {
queueLock.unlock();
}
}

Expand All @@ -327,8 +301,7 @@ public ImmediateServiceState getImmediateState() {
*/
@Override
public void setImmediateState(ImmediateServiceState state) {
try {
queueLock.lock();
synchronized (queueLock) {
if (state == null) throw new IllegalArgumentException();

if (state == currentState) return;
Expand All @@ -337,8 +310,6 @@ public void setImmediateState(ImmediateServiceState state) {
if (currentState.equals(ImmediateServiceState.RUNNING)) {
doWorkIfWeHaveSome();
}
} finally {
queueLock.unlock();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import javax.inject.Inject;
Expand Down Expand Up @@ -50,6 +51,7 @@
@Singleton @Visibility(DescriptorVisibility.LOCAL)
public class ImmediateContext implements Context<Immediate>{
private final ReentrantLock lock = new ReentrantLock();
private final Condition notEmpty = lock.newCondition();
private final HashMap<ActiveDescriptor<?>, HandleAndService> currentImmediateServices = new HashMap<ActiveDescriptor<?>, HandleAndService>();
private final HashMap<ActiveDescriptor<?>, Long> creating = new HashMap<ActiveDescriptor<?>, Long>();

Expand Down Expand Up @@ -94,7 +96,7 @@ public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor,
}

try {
this.wait();
notEmpty.await();
}
catch (InterruptedException ie) {
throw new MultiException(ie);
Expand Down Expand Up @@ -130,7 +132,7 @@ public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor,
}

creating.remove(activeDescriptor);
this.notifyAll();
notEmpty.signalAll();
} finally {
lock.unlock();
}
Expand Down Expand Up @@ -274,7 +276,7 @@ public void doWork() {
// First thing to do is wait until all the things in-flight have gone
while (creating.size() > 0) {
try {
this.wait();
notEmpty.await();
}
catch (InterruptedException ie) {
throw new RuntimeException(ie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor,
// retVal is null, and this is not an explicit null, so must actually do the creation
while (creating.contains(activeDescriptor)) {
try {
this.wait();
// FIXME Find other way to replace wait()
Thread.sleep(1);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -180,7 +181,6 @@ public <U> U findOrCreate(ActiveDescriptor<U> activeDescriptor,
}

creating.remove(activeDescriptor);
this.notifyAll();
} finally {
lock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import org.glassfish.hk2.api.ActiveDescriptor;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class SystemDescriptor<T> implements ActiveDescriptor<T>, Closeable {

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock cacheLock = new ReentrantLock();
private final Condition notReifyingCondition = lock.newCondition();
private boolean cacheSet = false;
private T cachedValue;

Expand Down Expand Up @@ -120,7 +122,7 @@ public class SystemDescriptor<T> implements ActiveDescriptor<T>, Closeable {
}
}
else {
activeDescriptor = null;
activeDescriptor = null;
preAnalyzed = true;

implClass = active.getImplementationClass();
Expand Down Expand Up @@ -295,7 +297,6 @@ public void setCache(T cacheMe) {
} finally {
cacheLock.unlock();
}

}

/* (non-Javadoc)
Expand All @@ -310,7 +311,6 @@ public void releaseCache() {
} finally {
cacheLock.unlock();
}

}

/* (non-Javadoc)
Expand Down Expand Up @@ -665,7 +665,7 @@ public String getName() {

while (reifying) {
try {
this.wait();
notReifyingCondition.await();
}
catch (InterruptedException e) {
collector.addThrowable(e);
Expand All @@ -690,7 +690,7 @@ public String getName() {
try {
lock.lock();
reifying = false;
this.notifyAll();
notReifyingCondition.signalAll();

if (!collector.hasErrors()) {
reified = true;
Expand Down

0 comments on commit 7c17988

Please sign in to comment.