Skip to content

Commit

Permalink
#52 Resolves issue in DiscoveredTunerModel causing shutdown to random…
Browse files Browse the repository at this point in the history
…ly hang.
  • Loading branch information
Denny Sheirer committed Dec 27, 2022
1 parent d0f58cf commit 7fc474e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 96 deletions.
53 changes: 3 additions & 50 deletions src/main/java/io/github/dsheirer/sample/Broadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/
package io.github.dsheirer.sample;

import io.github.dsheirer.source.tuner.ui.DiscoveredTunerModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Broadcasts an item to multiple listeners
Expand All @@ -34,21 +34,11 @@ public class Broadcaster<T> implements Listener<T>
private final static Logger mLog = LoggerFactory.getLogger(Broadcaster.class);
private ReentrantLock mLock = new ReentrantLock();
private List<Listener<T>> mListeners = new ArrayList<>();
private String mDebugName;

public Broadcaster()
{
}

/**
* Assigns a name or label to this instance to aid in debugging
* @param debugName to add to logging.
*/
public void setDebugName(String debugName)
{
mDebugName = debugName;
}

/**
* Implements the Listener<T> interface to receive an element and broadcast that element to all registered
* listeners.
Expand Down Expand Up @@ -129,42 +119,10 @@ public void removeListener(Listener<T> listener)
{
if(listener != null)
{
if(mLock.tryLock())
{
mLock.unlock();
}
else
{
List<Listener<?>> listeners = new ArrayList<>(mListeners);

StringBuilder sb = new StringBuilder();
sb.append("Broadcaster - unable to obtain lock\n");
for(Listener<?> listener1: listeners)
{
if(listener1.equals(listener1))
{
sb.append("Listener: ").append(listener1.getClass()).append("\t<<Attempting to remove\n");
}
else
{
sb.append("Listener: ").append(listener1.getClass()).append("\n");
}
}

sb.append("Lock Hold Count: ").append(mLock.getHoldCount()).append("\n");
sb.append("Lock Queue Length: ").append(mLock.getQueueLength()).append("\n");
sb.append("Current Thread: " ).append(Thread.currentThread().getName()).append("\n");
mLog.warn(sb.toString());
}

mLock.lock();

try
{
if(listener instanceof DiscoveredTunerModel)
{
mLog.info("Broadcaster - lock obtained - removing listener: " + listener.getClass());
}
mListeners.remove(listener);
}
catch(Exception e)
Expand All @@ -174,11 +132,6 @@ public void removeListener(Listener<T> listener)
finally
{
mLock.unlock();

if(listener instanceof DiscoveredTunerModel)
{
mLog.info("Broadcaster - lock released - removing listener: " + listener.getClass());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,12 @@ public void setSampleRate(RspSampleRate sampleRate) throws SDRPlayException
@Override
public void stop() throws SDRPlayException
{
mLog.info("Stopping Master Tuner 1");
mStreamingLock.lock();

mLog.info("Stopping Master Tuner 1 - lock acquired");
try
{
if(mMasterSlaveBridge != null)
{
mLog.info("Stopping Master Tuner 1 - stopping slave tuner 2");
mMasterSlaveBridge.stopSlave();
}
}
Expand All @@ -122,36 +119,29 @@ public void stop() throws SDRPlayException

try
{
mLog.info("Stopping Master Tuner 1 - setting continuous stream to false");
mContinuousStream = false;
mLog.info("Stopping Master Tuner 1 - calling super.stop()");
super.stop();
}
finally
{
mLog.info("Stopping Master Tuner 1 - releasing lock");
mStreamingLock.unlock();
}
}

@Override
public void stopStream()
{
mLog.info("Stopping Stream Master Tuner 1 - acquiring lock");
mStreamingLock.lock();

try
{
mLog.info("Stopping Stream Master Tuner 1 - continuous stream: " + mContinuousStream);
if(!mContinuousStream)
{
mLog.info("Stopping Stream Master Tuner 1 - calling super.stopStream()");
super.stopStream();
}
}
finally
{
mLog.info("Stopping Stream Master Tuner 1 - releasing lock");
mStreamingLock.unlock();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
import io.github.dsheirer.source.tuner.manager.DiscoveredUSBTuner;
import io.github.dsheirer.source.tuner.manager.IDiscoveredTunerStatusListener;
import io.github.dsheirer.source.tuner.manager.TunerStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.table.AbstractTableModel;
import java.awt.EventQueue;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.table.AbstractTableModel;

/**
* Model for discovered tuners
Expand Down Expand Up @@ -162,50 +162,32 @@ public void addDiscoveredTuner(DiscoveredTuner discoveredTuner)
*/
public void releaseDiscoveredTuners()
{
mLog.info("Outer - releasing discovered tuners ...");
mLock.lock();

List<DiscoveredTuner> discoveredTuners = new ArrayList<>(mDiscoveredTuners);

try
{
List<DiscoveredTuner> discoveredTuners = new ArrayList<>(mDiscoveredTuners);

for(DiscoveredTuner discoveredTuner: discoveredTuners)
{
mLog.info("Outer - removing discovered tuner: " + discoveredTuner.getId() + " of [" + discoveredTuners.size() + "]");
if(discoveredTuner.hasTuner())
{
Tuner tuner = discoveredTuner.getTuner();
if(tuner != null)
{
mLog.info("Outer - removing tuner event listener");
tuner.removeTunerEventListener(this);
mLog.info("Outer - removing tuner event listener - completed");
}
else
{
mLog.info("### tuner was null after hasTuner() reported true ###");
}
}

mLog.info("Outer - removing discovered tuner: " + discoveredTuner.getId() + " of [" + discoveredTuners.size() + "]");
removeDiscoveredTuner(discoveredTuner);
mLog.info("Outer - removing discovered tuner: " + discoveredTuner.getId() + " - complete!");
}
mDiscoveredTuners.clear();
fireTableDataChanged();
}
finally
{
mLock.unlock();
}

mLog.info("Outer - releasing discovered tuners ... complete!");
for(DiscoveredTuner discoveredTuner: discoveredTuners)
{
discoveredTuner.stop();
discoveredTuner.removeTunerStatusListener(this);
}
}

/**
* Removes the Tuner from this model
*/
public void removeDiscoveredTuner(DiscoveredTuner discoveredTuner)
{
mLog.info("Removing discovered tuner: " + discoveredTuner.getId());
mLock.lock();

try
Expand All @@ -216,13 +198,9 @@ public void removeDiscoveredTuner(DiscoveredTuner discoveredTuner)
mDiscoveredTuners.remove(discoveredTuner);
EventQueue.invokeLater(() ->
{
mLog.info("Firing table rows deleted ...");
fireTableRowsDeleted(index, index);
mLog.info("Firing table rows deleted ... complete!");
});
mLog.info("Stopping Discovered Tuner: " + discoveredTuner.getId());
discoveredTuner.stop();
mLog.info("Stopping Discovered Tuner: " + discoveredTuner.getId() + " - complete");
}
}
catch(Exception e)
Expand All @@ -233,7 +211,6 @@ public void removeDiscoveredTuner(DiscoveredTuner discoveredTuner)
{
mLock.unlock();
}
mLog.info("Removing discovered tuner: " + discoveredTuner.getId() + " - complete!");
}

/**
Expand Down

0 comments on commit 7fc474e

Please sign in to comment.