Improve synchronization in sshd DispatchCommand.
The ssh commands have synchronization because destroy() can run in a different thread than start(). Simplify the DispatchCommand synchronization by using an AtomicReference. Change-Id: Ief57303f0f7215822584af746db0567b806612b0
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
|
||||
package com.google.gerrit.sshd;
|
||||
|
||||
import com.google.common.util.concurrent.Atomics;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.sshd.args4j.SubcommandHandler;
|
||||
import com.google.inject.Inject;
|
||||
@@ -28,6 +29,7 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* Command that dispatches to a subcommand from its command table.
|
||||
@@ -40,7 +42,7 @@ final class DispatchCommand extends BaseCommand {
|
||||
private final Provider<CurrentUser> currentUser;
|
||||
private final String prefix;
|
||||
private final Map<String, Provider<Command>> commands;
|
||||
private Command cmd;
|
||||
private final AtomicReference<Command> atomicCmd;
|
||||
|
||||
@Argument(index = 0, required = true, metaVar = "COMMAND", handler = SubcommandHandler.class)
|
||||
private String commandName;
|
||||
@@ -54,6 +56,7 @@ final class DispatchCommand extends BaseCommand {
|
||||
currentUser = cu;
|
||||
prefix = pfx;
|
||||
commands = all;
|
||||
atomicCmd = Atomics.newReference();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,10 +93,7 @@ final class DispatchCommand extends BaseCommand {
|
||||
}
|
||||
|
||||
provideStateTo(cmd);
|
||||
|
||||
synchronized (this) {
|
||||
this.cmd = cmd;
|
||||
}
|
||||
atomicCmd.set(cmd);
|
||||
cmd.start(env);
|
||||
|
||||
} catch (UnloggedFailure e) {
|
||||
@@ -113,11 +113,9 @@ final class DispatchCommand extends BaseCommand {
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
synchronized (this) {
|
||||
if (cmd != null) {
|
||||
Command cmd = atomicCmd.getAndSet(null);
|
||||
if (cmd != null) {
|
||||
cmd.destroy();
|
||||
cmd = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user