Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean setup() {
acqEngine_ = new AcquisitionEngineDispim(this);
break;
default:
final String message = "AcquisitionEngine not implemented for " + geometryType;
final String message = "Microscope geometry type " + geometryType + " is not supported yet.";
studio_.logs().logError(message);
errorText_ = message;
return false; // early exit => show error ui
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.micromanager.lightsheetmanager;

import com.google.common.eventbus.Subscribe;
import org.micromanager.events.ShutdownCommencingEvent;
import org.micromanager.lightsheetmanager.gui.utils.WindowUtils;
import org.micromanager.MenuPlugin;
import org.micromanager.Studio;
Expand Down Expand Up @@ -76,13 +78,21 @@ public void onPluginSelected() {
// clear references after the window is fully closed
// prevent memory leaks from closed plugin instances
WindowUtils.registerWindowClosedEvent(frame_, event -> {
if (isLoaded) {
studio_.events().unregisterForEvents(this);
}
frame_ = null;
model_ = null;
});

frame_.pack();
frame_.setVisible(true);
frame_.toFront();

// quitting Micro-Manager does not close this window, so save the settings on exit too
if (isLoaded) {
studio_.events().registerForEvents(this);
}
} catch (Exception e) {
// cleanup resources
frame_ = null;
Expand All @@ -93,6 +103,22 @@ public void onPluginSelected() {
}
}

/**
* Saves the settings when Micro-Manager shuts down, before it writes the user profile to disk.
*/
@Subscribe
public void onShutdownCommencing(final ShutdownCommencingEvent event) {
if (event.isCanceled()) {
return; // early exit => the shutdown was canceled
}
try {
model_.userSettings().save();
studio_.logs().logMessage("User settings saved because Micro-Manager is shutting down.");
} catch (RuntimeException e) {
studio_.logs().logError(e, "Failed to save user settings while Micro-Manager was shutting down.");
}
}

@Override
public String getName() {
return menuName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package org.micromanager.lightsheetmanager.model.positions;

import com.google.common.eventbus.Subscribe;
import mmcorej.DeviceType;
import org.micromanager.events.ShutdownCommencingEvent;
import org.micromanager.lightsheetmanager.LightSheetManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

public class PositionUpdater implements Publisher {

// each poll reads every stage and galvo over serial, so do not poll faster than this
private static final int MIN_POLLING_DELAY_MS = 500;

// the most the exit waits for a poll that is reading the devices, so a stuck poll
// cannot hang Micro-Manager's shutdown
private static final long EXIT_WAIT_MS = 2000;

// polling
private volatile int pollingDelayMs_;
private volatile boolean isPolling_;
Expand All @@ -24,6 +33,10 @@ public class PositionUpdater implements Publisher {
private final ScheduledExecutorService executor_;
private final AtomicBoolean isTicking_; // true once the task is scheduled

// stops the polling when Micro-Manager shuts down
private final ExitListener exitListener_ = new ExitListener();
private final AtomicBoolean isListeningForExit_ = new AtomicBoolean(false);

// data
private final HashMap<String, Object> positions_;
private final HashMap<String, ArrayList<Subscriber>> topics_;
Expand Down Expand Up @@ -73,6 +86,9 @@ public void startPolling() {
// the thread starts on first use: close() is skipped when the plugin fails to load,
// so a thread started any earlier could never be shut down
if (isTicking_.compareAndSet(false, true)) {
// the thread calls the core, which Micro-Manager deletes when it shuts down
model_.studio().events().registerForEvents(exitListener_);
isListeningForExit_.set(true);
executor_.schedule(this::tick, 0, TimeUnit.MILLISECONDS);
}
}
Expand All @@ -86,11 +102,15 @@ public boolean isPolling() {
}

/**
* Stops polling and ends the polling thread. Call when the plugin closes.
* Stops polling, ends the polling thread and stops listening for Micro-Manager's exit.
* Call when the plugin closes.
*/
public void shutdown() {
isPolling_ = false;
executor_.shutdown();
if (isListeningForExit_.compareAndSet(true, false)) {
model_.studio().events().unregisterForEvents(exitListener_);
}
}

public void setPollingDelayMs(final int delayMs) {
Expand Down Expand Up @@ -143,4 +163,46 @@ private void updateSubscribers() {
}
}

/**
* Waits until no poll is reading the devices, for at most {@code EXIT_WAIT_MS}.
*/
private void waitForPollInProgress() {
if (!isTicking_.get()) {
return; // the thread never started
}
try {
// the polling thread runs this once the poll in progress, if any, has ended
executor_.submit(() -> { }).get(EXIT_WAIT_MS, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
model_.studio().logs().logError("Position polling was still reading the devices "
+ EXIT_WAIT_MS + " ms after Micro-Manager started to shut down.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException | RejectedExecutionException e) {
// shutdown() already ran, so the polling is off
}
}

/**
* Stops the polling before Micro-Manager deletes the core: polling a deleted core crashes
* Micro-Manager. If a later subscriber cancels the exit, the polling stays off.
*/
private final class ExitListener {

@Subscribe
public void onShutdownCommencing(final ShutdownCommencingEvent event) {
// another subscriber canceled the exit before this one ran, so the core stays
if (event.isCanceled()) {
return;
}
final boolean wasPolling = isPolling_;
isPolling_ = false;
waitForPollInProgress();
if (wasPolling) {
model_.studio().logs().logMessage(
"Stopped position polling because Micro-Manager is shutting down.");
}
}
}

}
Loading