Skip to content

Commit

Permalink
use ReadWriteLock instead of synchronized on class level
Browse files Browse the repository at this point in the history
this will avoid a dead-lock when trying to close the session from session state listener.
See opentelecoms-org#193

Signed-off-by: Christian Ambach <ambi@samba.org>
  • Loading branch information
der-ambi committed Dec 6, 2023
1 parent a036641 commit c4fc5c9
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions jsmpp/src/main/java/org/jsmpp/session/SMPPSessionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,43 @@
import org.jsmpp.extra.SessionState;
import org.jsmpp.session.state.SMPPSessionState;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* @author uudashr
*
*/
public class SMPPSessionContext extends AbstractSessionContext {
private final SMPPSession smppSession;
private SMPPSessionState stateProcessor = SMPPSessionState.CLOSED;

private ReadWriteLock stateProcessorLock = new ReentrantReadWriteLock();

public SMPPSessionContext(SMPPSession smppSession,
SessionStateListener sessionStateListener) {
super(sessionStateListener);
this.smppSession = smppSession;
}

public synchronized SMPPSessionState getStateProcessor() {
return stateProcessor;
public SMPPSessionState getStateProcessor() {
stateProcessorLock.readLock().lock();
SMPPSessionState currentStateProcessor = stateProcessor;
stateProcessorLock.readLock().unlock();
return currentStateProcessor;
}

public synchronized SessionState getSessionState() {
return stateProcessor.getSessionState();
public SessionState getSessionState() {
stateProcessorLock.readLock().lock();
SessionState sessionState = stateProcessor.getSessionState();
stateProcessorLock.readLock().unlock();
return sessionState;
}

@Override
protected void changeState(SessionState newState) {
if (!stateProcessor.getSessionState().equals(newState)) {
stateProcessorLock.writeLock().lock();
final SessionState oldState = stateProcessor.getSessionState();

// change the session state processor
Expand All @@ -58,6 +70,7 @@ protected void changeState(SessionState newState) {
} else if (newState == SessionState.CLOSED) {
stateProcessor = SMPPSessionState.CLOSED;
}
stateProcessorLock.writeLock().unlock();
fireStateChanged(newState, oldState, smppSession);
}
}
Expand Down

0 comments on commit c4fc5c9

Please sign in to comment.